piranha  0.10
piranha::mp_rational< SSize > Class Template Reference

Multiple precision rational class. More...

#include <piranha/mp_rational.hpp>

## Public Types

using int_type = mp_integer< SSize >
The underlying piranha::mp_integer type used to represent numerator and denominator.

## Public Member Functions

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_rationaloperator= (const mp_rational &other)=default
Copy assignment operator. More...

mp_rationaloperator= (mp_rational &&other) noexcept
Move assignment operator. More...

template<typename T , generic_ctor_enabler< T > = 0>
mp_rationaloperator= (const T &x)
Generic assignment operator. More...

mp_rationaloperator= (const char *str)
Assignment operator from C string. More...

mp_rationaloperator= (const std::string &str)
Assignment operator from C++ string. More...

const int_typenum () const
Get const reference to the numerator. More...

const int_typeden () 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_rationaloperator++ ()
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))

void negate ()
Negate in-place.

mp_rational operator- () const
Negated copy. More...

mp_rationaloperator-- ()
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 interface

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...

## Friends

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...

## Detailed Description

### 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.

## ◆ mp_rational() [1/8]

template<std::size_t SSize>
 piranha::mp_rational< SSize >::mp_rational ( )
inline

Default constructor.

This constructor will initialise the rational to zero (that is, the numerator is set to zero, the denominator to 1).

Definition at line 670 of file mp_rational.hpp.

## ◆ mp_rational() [2/8]

template<std::size_t SSize>
 piranha::mp_rational< SSize >::mp_rational ( const mp_rational< SSize > & other )
default

Copy constructor.

Parameters
 other the construction argument.

## ◆ mp_rational() [3/8]

template<std::size_t SSize>
 piranha::mp_rational< SSize >::mp_rational ( mp_rational< SSize > && other )
inlinenoexcept

Move constructor.

Parameters
 other the construction argument.

Definition at line 680 of file mp_rational.hpp.

## ◆ mp_rational() [4/8]

template<std::size_t SSize>
template<typename I0 , typename I1 , nd_ctor_enabler< I0, I1 > = 0>
 piranha::mp_rational< SSize >::mp_rational ( const I0 & n, const I1 & d )
inlineexplicit

Constructor from numerator/denominator pair.

Note
This constructor is enabled only if I0 and I1 are either integral types or piranha::integer.
Parameters
 n numerator. d denominator.
Exceptions
 piranha::zero_division_error if the denominator is zero. unspecified any exception thrown by the invoked constructor of piranha::mp_integer.

Definition at line 699 of file mp_rational.hpp.

## ◆ mp_rational() [5/8]

template<std::size_t SSize>
template<typename T , generic_ctor_enabler< T > = 0>
 piranha::mp_rational< SSize >::mp_rational ( const T & x )
inlineexplicit

Generic constructor.

Note
This constructor is enabled only if T is an interoperable type.
Parameters
 x object used to construct this.
Exceptions
 std::invalid_argument if the construction fails (e.g., construction from a non-finite floating-point value).

Definition at line 717 of file mp_rational.hpp.

## ◆ mp_rational() [6/8]

template<std::size_t SSize>
 piranha::mp_rational< SSize >::mp_rational ( const char * str )
inlineexplicit

Constructor from C string.

The string must represent either a valid single piranha::mp_integer, or two valid piranha::mp_integer separated by "/". The rational will be put in canonical form by this constructor.

Note that if the string is not null-terminated, undefined behaviour will occur.

Parameters
 str C string used for construction.
Exceptions
 std::invalid_argument if the string is not formatted correctly. piranha::zero_division_error if the denominator, if present, is zero. unspecified any exception thrown by the constructor from string of piranha::mp_integer or by memory errors in std::string.

Definition at line 735 of file mp_rational.hpp.

## ◆ mp_rational() [7/8]

template<std::size_t SSize>
 piranha::mp_rational< SSize >::mp_rational ( const std::string & str )
inlineexplicit

Constructor from C++ string.

Equivalent to the constructor from C string.

Parameters
 str C string used for construction.
Exceptions
 unspecified any exception thrown by the constructor from C string.

Definition at line 760 of file mp_rational.hpp.

## ◆ mp_rational() [8/8]

template<std::size_t SSize>
 piranha::mp_rational< SSize >::mp_rational ( const ::mpq_t q )
inlineexplicit

Constructor from mpq_t.

This constructor will construct the numerator from the numerator of q, the denominator from the denominator of q. This constructor assumes that q is already in canonical form. If that is not the case, the behaviour will be undefined.

Parameters
 q input GMP rational.
Exceptions
 piranha::zero_division_error if the denominator is zero.

Definition at line 997 of file mp_rational.hpp.

## ◆ _den()

template<std::size_t SSize>
 int_type& piranha::mp_rational< SSize >::_den ( )
inline

Mutable reference to the denominator.

Returns
mutable reference to the denominator.

Definition at line 1015 of file mp_rational.hpp.

## ◆ _num()

template<std::size_t SSize>
 int_type& piranha::mp_rational< SSize >::_num ( )
inline

Mutable reference to the numerator.

Returns
mutable reference to the numerator.

Definition at line 1007 of file mp_rational.hpp.

## ◆ _set_den()

template<std::size_t SSize>
 void piranha::mp_rational< SSize >::_set_den ( const int_type & den )
inline

Set denominator.

This method will set the denominator to den without canonicalising the rational.

Parameters
 den desired value for the denominator.
Exceptions
 std::invalid_argument if den is not positive.

Definition at line 1027 of file mp_rational.hpp.

## ◆ abs()

template<std::size_t SSize>
 mp_rational piranha::mp_rational< SSize >::abs ( ) const
inline

Absolute value.

Returns
absolute value of this.

Definition at line 1613 of file mp_rational.hpp.

## ◆ binomial()

template<std::size_t SSize>
template<typename T , enable_if_t< disjunction< std::is_integral< T >, std::is_same< T, int_type >>::value, int > = 0>
 mp_rational piranha::mp_rational< SSize >::binomial ( const T & n ) const
inline

Binomial coefficient.

Note
This method is enabled only if T is an integral type or piranha::mp_integer.

Will return this choose n.

Parameters
 n bottom argument for the binomial coefficient.
Returns
this choose n.
Exceptions
 unspecified any exception thrown by piranha::mp_integer::binomial() or by arithmetic operations on piranha::mp_rational.

Definition at line 1684 of file mp_rational.hpp.

## ◆ canonicalise()

template<std::size_t SSize>
 void piranha::mp_rational< SSize >::canonicalise ( )
inline

Canonicalise.

This method will convert this to the canonical form, if needed.

piranha::mp_rational::is_canonical().

Definition at line 942 of file mp_rational.hpp.

## ◆ den()

template<std::size_t SSize>
 const int_type& piranha::mp_rational< SSize >::den ( ) const
inline

Get const reference to the denominator.

Returns
a const reference to the denominator.

Definition at line 893 of file mp_rational.hpp.

## ◆ get_mpq_view()

template<std::size_t SSize>
 mpq_view piranha::mp_rational< SSize >::get_mpq_view ( ) const
inline

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.

## ◆ hash()

template<std::size_t SSize>
 std::size_t piranha::mp_rational< SSize >::hash ( ) const
inline

Hash value.

The hash value is calculated by combining the hash values of numerator and denominator.

Returns
a hash value for this.

Definition at line 1627 of file mp_rational.hpp.

## ◆ is_canonical()

template<std::size_t SSize>
 bool piranha::mp_rational< SSize >::is_canonical ( ) const
inline

Canonicality check.

A rational number is in canonical form when numerator and denominator are coprime. A zero numerator must be paired to a 1 denominator.

If low-level methods are not used, this function will always return true.

Returns
true if this is in canonical form, false otherwise.

Definition at line 926 of file mp_rational.hpp.

## ◆ msgpack_convert()

template<std::size_t SSize>
template<typename U = mp_rational, msgpack_convert_enabler< U > = 0>
 void piranha::mp_rational< SSize >::msgpack_convert ( const msgpack::object & o, msgpack_format f )
inline

Convert from msgpack object.

Note
This method is enabled only if the type representing the numerator and denominator satisfies piranha::has_msgpack_convert.

This method will convert o into this. If f is piranha::msgpack_format::portable this method will check that the deserialized rational is in canonical form before assigning it to this, otherwise no check will be performed and the behaviour will be undefined if the deserialized denominator is zero.

Parameters
 o the source msgpack::object. f the desired piranha::msgpack_format.
Exceptions
 unspecified any exception thrown by: the public interface of msgpack::object, piranha::msgpack_convert(), the constructor of piranha::mp_rational from numerator and denominator.

Definition at line 1750 of file mp_rational.hpp.

## ◆ msgpack_pack()

template<std::size_t SSize>
template<typename Stream , msgpack_pack_enabler< Stream > = 0>
 void piranha::mp_rational< SSize >::msgpack_pack ( msgpack::packer< Stream > & p, msgpack_format f ) const
inline

Pack in msgpack format.

Note
This method is enabled only if Stream satisfies piranha::is_msgpack_stream and the type representing the numerator and denominator satisfies piranha::has_msgpack_pack.

This method will pack this into p. Rationals are packed as a numerator-denominator pairs.

Parameters
 p the target msgpack::packer. f the desired piranha::msgpack_format.
Exceptions
 unspecified any exception thrown by: the public interface of msgpack::packer, piranha::msgpack_pack().

Definition at line 1725 of file mp_rational.hpp.

## ◆ num()

template<std::size_t SSize>
 const int_type& piranha::mp_rational< SSize >::num ( ) const
inline

Get const reference to the numerator.

Returns
a const reference to the numerator.

Definition at line 885 of file mp_rational.hpp.

## ◆ operator T()

template<std::size_t SSize>
template<typename T , cast_enabler< T > = 0>
 piranha::mp_rational< SSize >::operator T ( ) const
inlineexplicit

Conversion operator.

Note
This operator is enabled only if T is an interoperable type.

The conversion to piranha::mp_integer is computed by dividing the numerator by the denominator. The conversion to integral types is computed by casting first to piranha::mp_integer, then to the target integral type. The conversion to floating-point types might generate non-finite values.

Returns
the value of this converted to type T.
Exceptions
 std::overflow_error if the conversion fails (e.g., the range of the target integral type is insufficient to represent the value of this).

Definition at line 977 of file mp_rational.hpp.

## ◆ operator*=()

template<std::size_t SSize>
template<typename T >
 auto piranha::mp_rational< SSize >::operator*= ( const T & x ) -> decltype(this->in_place_mult(x))
inline

In-place multiplication.

Note
This operator is enabled only if T is an interoperable type or piranha::mp_rational.

If T is not a float, the exact result will be computed. If T is a floating-point type, the following sequence of operations takes place:

• this is converted to an instance f of type T via the conversion operator,
• f is multiplied by x,
• the result is assigned back to this.
Parameters
 x argument for the multiplication.
Returns
reference to this.
Exceptions
 unspecified any exception thrown by the conversion operator, the generic constructor of piranha::mp_integer, or the generic assignment operator, if used.

Definition at line 1271 of file mp_rational.hpp.

## ◆ operator+()

template<std::size_t SSize>
 mp_rational piranha::mp_rational< SSize >::operator+ ( ) const
inline

Identity operator.

Returns
a copy of this.

Definition at line 1039 of file mp_rational.hpp.

## ◆ operator++() [1/2]

template<std::size_t SSize>
 mp_rational& piranha::mp_rational< SSize >::operator++ ( )
inline

Pre-increment operator.

Returns
reference to this after the increment.
Exceptions
 unspecified any exception thrown by in-place addition.

Definition at line 1049 of file mp_rational.hpp.

## ◆ operator++() [2/2]

template<std::size_t SSize>
 mp_rational piranha::mp_rational< SSize >::operator++ ( int )
inline

Post-increment operator.

Returns
copy of this before the increment.
Exceptions
 unspecified any exception thrown by the pre-increment operator.

Definition at line 1059 of file mp_rational.hpp.

## ◆ operator+=()

template<std::size_t SSize>
template<typename T >
 auto piranha::mp_rational< SSize >::operator+= ( const T & x ) -> decltype(this->in_place_add(x))
inline

Note
This operator is enabled only if T is an interoperable type or piranha::mp_rational.

If T is not a float, the exact result will be computed. If T is a floating-point type, the following sequence of operations takes place:

• this is converted to an instance f of type T via the conversion operator,
• f is added to x,
• the result is assigned back to this.
Parameters
Returns
reference to this.
Exceptions
 unspecified any exception thrown by the conversion operator, the generic constructor of piranha::mp_integer, or the generic assignment operator, if used.

Definition at line 1086 of file mp_rational.hpp.

## ◆ operator-()

template<std::size_t SSize>
 mp_rational piranha::mp_rational< SSize >::operator- ( ) const
inline

Negated copy.

Returns
a negated copy of this.

Definition at line 1148 of file mp_rational.hpp.

## ◆ operator--() [1/2]

template<std::size_t SSize>
 mp_rational& piranha::mp_rational< SSize >::operator-- ( )
inline

Pre-decrement operator.

Returns
reference to this after the decrement.
Exceptions
 unspecified any exception thrown by in-place subtraction.

Definition at line 1160 of file mp_rational.hpp.

## ◆ operator--() [2/2]

template<std::size_t SSize>
 mp_rational piranha::mp_rational< SSize >::operator-- ( int )
inline

Post-decrement operator.

Returns
copy of this before the decrement.
Exceptions
 unspecified any exception thrown by the pre-decrement operator.

Definition at line 1170 of file mp_rational.hpp.

## ◆ operator-=()

template<std::size_t SSize>
template<typename T >
 auto piranha::mp_rational< SSize >::operator-= ( const T & x ) -> decltype(this->in_place_sub(x))
inline

In-place subtraction.

Note
This operator is enabled only if T is an interoperable type or piranha::mp_rational.

If T is not a float, the exact result will be computed. If T is a floating-point type, the following sequence of operations takes place:

• this is converted to an instance f of type T via the conversion operator,
• x is subtracted from f,
• the result is assigned back to this.
Parameters
 x argument for the subtraction.
Returns
reference to this.
Exceptions
 unspecified any exception thrown by the conversion operator, the generic constructor of piranha::mp_integer, or the generic assignment operator, if used.

Definition at line 1197 of file mp_rational.hpp.

## ◆ operator/=()

template<std::size_t SSize>
template<typename T >
 auto piranha::mp_rational< SSize >::operator/= ( const T & x ) -> decltype(this->in_place_div(x))
inline

In-place division.

Note
This operator is enabled only if T is an interoperable type or piranha::mp_rational.

If T is not a float, the exact result will be computed. If T is a floating-point type, the following sequence of operations takes place:

• this is converted to an instance f of type T via the conversion operator,
• f is divided by x,
• the result is assigned back to this.
Parameters
 x argument for the division.
Returns
reference to this.
Exceptions
 piranha::zero_division_error if x is zero. unspecified any exception thrown by the conversion operator, the generic constructor of piranha::mp_integer, or the generic assignment operator, if used.

Definition at line 1346 of file mp_rational.hpp.

## ◆ operator=() [1/5]

template<std::size_t SSize>
 mp_rational& piranha::mp_rational< SSize >::operator= ( const mp_rational< SSize > & other )
default

Copy assignment operator.

Parameters
 other the assignment argument.
Returns
a reference to this.

## ◆ operator=() [2/5]

template<std::size_t SSize>
 mp_rational& piranha::mp_rational< SSize >::operator= ( mp_rational< SSize > && other )
inlinenoexcept

Move assignment operator.

Parameters
 other the assignment argument.
Returns
a reference to this.

Definition at line 781 of file mp_rational.hpp.

## ◆ operator=() [3/5]

template<std::size_t SSize>
template<typename T , generic_ctor_enabler< T > = 0>
 mp_rational& piranha::mp_rational< SSize >::operator= ( const T & x )
inline

Generic assignment operator.

Note
This assignment operator is enabled only if T is an interoperable type.

This operator will construct a temporary piranha::mp_rational from x and will then move-assign it to this.

Parameters
 x assignment target.
Returns
reference to this.
Exceptions
 unspecified any exception thrown by the generic constructor from interoperable type.

Definition at line 807 of file mp_rational.hpp.

## ◆ operator=() [4/5]

template<std::size_t SSize>
 mp_rational& piranha::mp_rational< SSize >::operator= ( const char * str )
inline

Assignment operator from C string.

This assignment operator will construct a piranha::mp_rational from the string str and will then move-assign the result to this.

Parameters
 str C string.
Returns
reference to this.
Exceptions
 unspecified any exception thrown by the constructor from string.

Definition at line 822 of file mp_rational.hpp.

## ◆ operator=() [5/5]

template<std::size_t SSize>
 mp_rational& piranha::mp_rational< SSize >::operator= ( const std::string & str )
inline

Assignment operator from C++ string.

This assignment operator will construct a piranha::mp_rational from the string str and will then move-assign the result to this.

Parameters
 str C++ string.
Returns
reference to this.
Exceptions
 unspecified any exception thrown by the constructor from string.

Definition at line 837 of file mp_rational.hpp.

## ◆ pow()

template<std::size_t SSize>
template<typename T , pow_enabler< T > = 0>
 mp_rational piranha::mp_rational< SSize >::pow ( const T & exp ) const
inline

Exponentiation.

Note
This method is enabled only if piranha::mp_rational::int_type can be raised to the power of exp, yielding piranha::mp_rational::int_type as a result.

This method computes this raised to the integral power exp. Internally, the piranha::math::pow() function is used on numerator and denominator. Negative powers will raise an error if the numerator of this is zero.

Parameters
 exp exponent.
Returns
this ** exp.
Exceptions
 piranha::zero_division_error if exp is negative and the numerator of this is zero. unspecified any exception thrown by piranha::math::pow().

Definition at line 1583 of file mp_rational.hpp.

## ◆ operator!=

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.

## ◆ operator*

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.

## ◆ operator*=

template<std::size_t SSize>
template<typename T , generic_in_place_enabler< T > = 0>
 T& operator*= ( T & x, const mp_rational< SSize > & q )
friend

Generic in-place multiplication with piranha::mp_rational.

Note
This operator is enabled only if T is a non-const interoperable type.

Multiply by a piranha::mp_rational in-place. This method will first compute x * q, cast it back to T via static_cast and finally assign the result to x.

Parameters
 x first argument. q second argument.
Returns
reference to x.
Exceptions
 unspecified any exception thrown by the binary operator or by casting piranha::mp_rational to T.

Definition at line 1291 of file mp_rational.hpp.

## ◆ operator+

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

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.

## ◆ operator+=

template<std::size_t SSize>
template<typename T , generic_in_place_enabler< T > = 0>
 T& operator+= ( T & x, const mp_rational< SSize > & q )
friend

Note
This operator is enabled only if T is a non-const interoperable type.

Add a piranha::mp_rational in-place. This method will first compute q + x, cast it back to T via static_cast and finally assign the result to x.

Parameters
 x first argument. q second argument.
Returns
reference to x.
Exceptions
 unspecified any exception thrown by the binary operator or by casting piranha::mp_rational to T.

Definition at line 1106 of file mp_rational.hpp.

## ◆ operator-

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.

## ◆ operator-=

template<std::size_t SSize>
template<typename T , generic_in_place_enabler< T > = 0>
 T& operator-= ( T & x, const mp_rational< SSize > & q )
friend

Generic in-place subtraction with piranha::mp_rational.

Note
This operator is enabled only if T is a non-const interoperable type.

Subtract a piranha::mp_rational in-place. This method will first compute x - q, cast it back to T via static_cast and finally assign the result to x.

Parameters
 x first argument. q second argument.
Returns
reference to x.
Exceptions
 unspecified any exception thrown by the binary operator or by casting piranha::mp_rational to T.

Definition at line 1217 of file mp_rational.hpp.

## ◆ operator/

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.

## ◆ operator/=

template<std::size_t SSize>
template<typename T , generic_in_place_enabler< T > = 0>
 T& operator/= ( T & x, const mp_rational< SSize > & q )
friend

Generic in-place division with piranha::mp_rational.

Note
This operator is enabled only if T is a non-const interoperable type.

Divide by a piranha::mp_rational in-place. This method will first compute x / q, cast it back to T via static_cast and finally assign the result to x.

Parameters
 x first argument. q second argument.
Returns
reference to x.
Exceptions
 unspecified any exception thrown by the binary operator or by casting piranha::mp_rational to T.

Definition at line 1369 of file mp_rational.hpp.

## ◆ operator<

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: the less-than operator of piranha::mp_integer, the invoked conversion operator, if used.

Definition at line 1481 of file mp_rational.hpp.

## ◆ operator<<

template<std::size_t SSize>
 std::ostream& operator<< ( std::ostream & os, const mp_rational< SSize > & q )
friend

Stream operator.

The printing format is as follows:

• only the numerator is printed if the denominator is 1,
• otherwise, numerator and denominator are printed separated by a '/' sign.
Parameters
 os target stream. q rational to be printed.
Returns
reference to os.
Exceptions
 unspecified any exception thrown by the streaming operator of piranha::mp_integer.

Definition at line 854 of file mp_rational.hpp.

## ◆ operator<=

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.

## ◆ operator==

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: the comparison operator of piranha::mp_integer, the invoked conversion operator, if used.

Definition at line 1427 of file mp_rational.hpp.

## ◆ operator>

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: the greater-than operator of piranha::mp_integer, the invoked conversion operator, if used.

Definition at line 1535 of file mp_rational.hpp.

## ◆ operator>=

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.

## ◆ operator>>

template<std::size_t SSize>
 std::istream& operator>> ( std::istream & is, mp_rational< SSize > & q )
friend

Overload input stream operator for piranha::mp_rational.

Equivalent to extracting a line from the stream and then assigning it to q.

Parameters
 is input stream. q rational to which the contents of the stream will be assigned.
Returns
reference to is.
Exceptions
 unspecified any exception thrown by the constructor from string of piranha::mp_rational.

Definition at line 874 of file mp_rational.hpp.

The documentation for this class was generated from the following files: