|
| mp_rational () |
| Default constructor. More...
|
|
| mp_rational (const mp_rational &other)=default |
| Copy constructor. More...
|
|
| mp_rational (mp_rational &&other) noexcept |
| Move constructor. More...
|
|
template<typename I0 , typename I1 , nd_ctor_enabler< I0, I1 > = 0> |
| mp_rational (const I0 &n, const I1 &d) |
| Constructor from numerator/denominator pair. More...
|
|
template<typename T , generic_ctor_enabler< T > = 0> |
| mp_rational (const T &x) |
| Generic constructor. More...
|
|
| mp_rational (const char *str) |
| Constructor from C string. More...
|
|
| mp_rational (const std::string &str) |
| Constructor from C++ string. More...
|
|
| ~mp_rational () |
| Destructor.
|
|
mp_rational & | operator= (const mp_rational &other)=default |
| Copy assignment operator. More...
|
|
mp_rational & | operator= (mp_rational &&other) noexcept |
| Move assignment operator. More...
|
|
template<typename T , generic_ctor_enabler< T > = 0> |
mp_rational & | operator= (const T &x) |
| Generic assignment operator. More...
|
|
mp_rational & | operator= (const char *str) |
| Assignment operator from C string. More...
|
|
mp_rational & | operator= (const std::string &str) |
| Assignment operator from C++ string. More...
|
|
const int_type & | num () const |
| Get const reference to the numerator. More...
|
|
const int_type & | den () const |
| Get const reference to the denominator. More...
|
|
mpq_view | get_mpq_view () const |
| Get an mpq view of this . More...
|
|
bool | is_canonical () const |
| Canonicality check. More...
|
|
void | canonicalise () |
| Canonicalise. More...
|
|
template<typename T , cast_enabler< T > = 0> |
| operator T () const |
| Conversion operator. More...
|
|
mp_rational | operator+ () const |
| Identity operator. More...
|
|
mp_rational & | operator++ () |
| Pre-increment operator. More...
|
|
mp_rational | operator++ (int) |
| Post-increment operator. More...
|
|
template<typename T > |
auto | operator+= (const T &x) -> decltype(this->in_place_add(x)) |
| In-place addition. More...
|
|
void | negate () |
| Negate in-place.
|
|
mp_rational | operator- () const |
| Negated copy. More...
|
|
mp_rational & | operator-- () |
| Pre-decrement operator. More...
|
|
mp_rational | operator-- (int) |
| Post-decrement operator. More...
|
|
template<typename T > |
auto | operator-= (const T &x) -> decltype(this->in_place_sub(x)) |
| In-place subtraction. More...
|
|
template<typename T > |
auto | operator*= (const T &x) -> decltype(this->in_place_mult(x)) |
| In-place multiplication. More...
|
|
template<typename T > |
auto | operator/= (const T &x) -> decltype(this->in_place_div(x)) |
| In-place division. More...
|
|
template<typename T , pow_enabler< T > = 0> |
mp_rational | pow (const T &exp) const |
| Exponentiation. More...
|
|
mp_rational | abs () const |
| Absolute value. More...
|
|
std::size_t | hash () const |
| Hash value. More...
|
|
template<typename T , enable_if_t< disjunction< std::is_integral< T >, std::is_same< T, int_type >>::value, int > = 0> |
mp_rational | binomial (const T &n) const |
| Binomial coefficient. More...
|
|
template<typename Stream , msgpack_pack_enabler< Stream > = 0> |
void | msgpack_pack (msgpack::packer< Stream > &p, msgpack_format f) const |
| Pack in msgpack format. More...
|
|
template<typename U = mp_rational, msgpack_convert_enabler< U > = 0> |
void | msgpack_convert (const msgpack::object &o, msgpack_format f) |
| Convert from msgpack object. More...
|
|
|
Low-level methods. These methods allow construction from mpq_t and direct mutable access to numerator and denominator, and they will not keep the rational in canonical form.
|
| mp_rational (const ::mpq_t q) |
| Constructor from mpq_t . More...
|
|
int_type & | _num () |
| Mutable reference to the numerator. More...
|
|
int_type & | _den () |
| Mutable reference to the denominator. More...
|
|
void | _set_den (const int_type &den) |
| Set denominator. More...
|
|
|
std::ostream & | operator<< (std::ostream &os, const mp_rational &q) |
| Stream operator. More...
|
|
std::istream & | operator>> (std::istream &is, mp_rational &q) |
| Overload input stream operator for piranha::mp_rational. More...
|
|
template<typename T , generic_in_place_enabler< T > = 0> |
T & | operator+= (T &x, const mp_rational &q) |
| Generic in-place addition with piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator+ (const T &x, const U &y) -> decltype(mp_rational::binary_plus(x, y)) |
| Generic binary addition involving piranha::mp_rational. More...
|
|
template<typename T , generic_in_place_enabler< T > = 0> |
T & | operator-= (T &x, const mp_rational &q) |
| Generic in-place subtraction with piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator- (const T &x, const U &y) -> decltype(mp_rational::binary_minus(x, y)) |
| Generic binary subtraction involving piranha::mp_rational. More...
|
|
template<typename T , generic_in_place_enabler< T > = 0> |
T & | operator*= (T &x, const mp_rational &q) |
| Generic in-place multiplication with piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator* (const T &x, const U &y) -> decltype(mp_rational::binary_mult(x, y)) |
| Generic binary multiplication involving piranha::mp_rational. More...
|
|
template<typename T , generic_in_place_enabler< T > = 0> |
T & | operator/= (T &x, const mp_rational &q) |
| Generic in-place division with piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator/ (const T &x, const U &y) -> decltype(mp_rational::binary_div(x, y)) |
| Generic binary division involving piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator== (const T &x, const U &y) -> decltype(mp_rational::binary_eq(x, y)) |
| Generic equality operator involving piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator!= (const T &x, const U &y) -> decltype(!mp_rational::binary_eq(x, y)) |
| Generic inequality operator involving piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator< (const T &x, const U &y) -> decltype(mp_rational::binary_less_than(x, y)) |
| Generic less-than operator involving piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator>= (const T &x, const U &y) -> decltype(!mp_rational::binary_less_than(x, y)) |
| Generic greater-than or equal operator involving piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator> (const T &x, const U &y) -> decltype(mp_rational::binary_greater_than(x, y)) |
| Generic greater-than operator involving piranha::mp_rational. More...
|
|
template<typename T , typename U > |
auto | operator<= (const T &x, const U &y) -> decltype(!mp_rational::binary_greater_than(x, y)) |
| Generic less-than or equal operator involving piranha::mp_rational. More...
|
|
template<std::size_t SSize>
class piranha::mp_rational< SSize >
Multiple precision rational class.
This class encapsulates two instances of piranha::mp_integer to represent an arbitrary-precision rational number in terms of a numerator and a denominator. The meaning of the SSize
template parameter is the same as in piranha::mp_integer, that is, it represents the number of limbs stored statically in the numerator and in the denominator.
Unless otherwise specified, rational numbers are always kept in the usual canonical form in which numerator and denominator are coprime, and the denominator is always positive. Zero is uniquely represented by 0/1.
Interoperability with other types
This class interoperates with the same types as piranha::mp_integer, plus piranha::mp_integer itself.
Exception safety guarantee
This class provides the strong exception safety guarantee for all operations. In case of memory allocation errors by GMP routines, the program will terminate.
Move semantics
Move construction and move assignment will leave the moved-from object in an unspecified but valid state.
Definition at line 39 of file mp_rational_fwd.hpp.
template<std::size_t SSize>
Get an mpq
view of this
.
This method will return an object of an unspecified type mpq_view
which is implicitly convertible to a const pointer to an mpq
struct (and which can thus be used as a const mpq_t
parameter in GMP functions). In addition to the implicit conversion operator, the mpq
struct pointer can also be retrieved via the get()
method of the mpq_view
class. The pointee will represent a GMP rational whose value is equal to this
.
Note that the returned mpq_view
instance can only be move-constructed (the other constructors and the assignment operators are disabled). Additionally, the returned object and the pointer might reference internal data belonging to this
, and they can thus be used safely only during the lifetime of this
. Any modification to this
will also invalidate the view and the pointer.
- Returns
- an
mpq
view of this
.
Definition at line 913 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator!= |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(!mp_rational::binary_eq(x, y))
|
|
friend |
Generic inequality operator involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the comparison will be returned.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and compared to f
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
true
if x != y
, false
otherwise.
- Exceptions
-
unspecified | any exception thrown by the equality operator. |
Definition at line 1453 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator* |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(mp_rational::binary_mult(x, y))
|
|
friend |
Generic binary multiplication involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the operation will be returned as a piranha::mp_rational.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and multiplied by f
to generate the return value, which will then be of type F
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
x * y
.
- Exceptions
-
unspecified | any exception thrown by:
- the corresponding in-place operator,
- the invoked constructor or the conversion operator, if used.
|
Definition at line 1320 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator+ |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(mp_rational::binary_plus(x, y))
|
|
friend |
Generic binary addition involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the operation will be returned as a piranha::mp_rational.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and added to f
to generate the return value, which will then be of type F
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
x + y
.
- Exceptions
-
unspecified | any exception thrown by:
- the corresponding in-place operator,
- the invoked constructor or the conversion operator, if used.
|
Definition at line 1135 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator- |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(mp_rational::binary_minus(x, y))
|
|
friend |
Generic binary subtraction involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the operation will be returned as a piranha::mp_rational.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and subtracted from (or to) f
to generate the return value, which will then be of type F
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
x - y
.
- Exceptions
-
unspecified | any exception thrown by:
- the corresponding in-place operator,
- the invoked constructor or the conversion operator, if used.
|
Definition at line 1246 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator/ |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(mp_rational::binary_div(x, y))
|
|
friend |
Generic binary division involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the operation will be returned as a piranha::mp_rational.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and divided by f
(or viceversa) to generate the return value, which will then be of type F
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
x / y
.
- Exceptions
-
piranha::zero_division_error | in case of division by zero. |
unspecified | any exception thrown by:
- the corresponding in-place operator,
- the invoked constructor or the conversion operator, if used.
|
Definition at line 1399 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator< |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(mp_rational::binary_less_than(x, y))
|
|
friend |
Generic less-than operator involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the comparison will be returned.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and compared to f
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
true
if x < y
, false
otherwise.
- Exceptions
-
unspecified | any exception thrown by:
|
Definition at line 1481 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator<= |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(!mp_rational::binary_greater_than(x, y))
|
|
friend |
Generic less-than or equal operator involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the comparison will be returned.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and compared to f
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
true
if x <= y
, false
otherwise.
- Exceptions
-
unspecified | any exception thrown by the greater-than operator. |
Definition at line 1561 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator== |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(mp_rational::binary_eq(x, y))
|
|
friend |
Generic equality operator involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the comparison will be returned.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and compared to f
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
true
if x == y
, false
otherwise.
- Exceptions
-
unspecified | any exception thrown by:
|
Definition at line 1427 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator> |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(mp_rational::binary_greater_than(x, y))
|
|
friend |
Generic greater-than operator involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the comparison will be returned.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and compared to f
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
true
if x > y
, false
otherwise.
- Exceptions
-
unspecified | any exception thrown by:
|
Definition at line 1535 of file mp_rational.hpp.
template<std::size_t SSize>
template<typename T , typename U >
auto operator>= |
( |
const T & |
x, |
|
|
const U & |
y |
|
) |
| -> decltype(!mp_rational::binary_less_than(x, y))
|
|
friend |
Generic greater-than or equal operator involving piranha::mp_rational.
- Note
- This template operator is enabled only if either:
If no floating-point types are involved, the exact result of the comparison will be returned.
If one of the arguments is a floating-point value f
of type F
, the other argument will be converted to an instance of type F
and compared to f
.
- Parameters
-
x | first argument |
y | second argument. |
- Returns
true
if x >= y
, false
otherwise.
- Exceptions
-
unspecified | any exception thrown by the less-than operator. |
Definition at line 1507 of file mp_rational.hpp.