piranha
0.10
|
Arbitrary precision floating-point class. More...
#include <piranha/real.hpp>
Inherits real_base<>.
Public Member Functions | |
real () | |
Default constructor. More... | |
real (const real &other) | |
Copy constructor. More... | |
real (real &&other) noexcept | |
Move constructor. More... | |
real (const char *str, const ::mpfr_prec_t &prec=default_prec) | |
Constructor from C string. More... | |
real (const std::string &str, const ::mpfr_prec_t &prec=default_prec) | |
Constructor from C++ string. More... | |
real (const real &other, const ::mpfr_prec_t &prec) | |
Copy constructor with different precision. More... | |
template<typename T , typename = generic_ctor_enabler<T>> | |
real (const T &x, const ::mpfr_prec_t &prec=default_prec) | |
Generic constructor. More... | |
~real () | |
Destructor. More... | |
real & | operator= (const real &other) |
Copy assignment operator. More... | |
real & | operator= (real &&other) noexcept |
Move assignment operator. More... | |
real & | operator= (const std::string &str) |
Assignment operator from C++ string. More... | |
real & | operator= (const char *str) |
Assignment operator from C string. More... | |
template<typename T , typename = generic_ctor_enabler<T>> | |
real & | operator= (const T &x) |
Generic assignment operator. More... | |
template<typename T , typename = cast_enabler<T>> | |
operator T () const | |
Conversion operator. More... | |
void | swap (real &other) |
Swap. More... | |
int | sign () const |
Sign. More... | |
bool | is_zero () const |
Test for zero. More... | |
bool | is_nan () const |
Test for NaN. More... | |
bool | is_inf () const |
Test for infinity. More... | |
::mpfr_prec_t | get_prec () const |
Get precision. More... | |
void | set_prec (const ::mpfr_prec_t &prec) |
Set precision. More... | |
void | negate () |
Negate in-place. More... | |
void | truncate () |
Truncate in-place. More... | |
real | truncated () const |
Truncated copy. More... | |
template<typename T > | |
auto | operator+= (const T &x) -> decltype(this->in_place_add(x)) |
In-place addition. More... | |
real | operator+ () const |
Identity operator. More... | |
real & | operator++ () |
Prefix increment. More... | |
real | operator++ (int) |
Suffix increment. More... | |
template<typename T > | |
auto | operator-= (const T &x) -> decltype(this->in_place_sub(x)) |
In-place subtraction. More... | |
real | operator- () const |
Negated copy. More... | |
real & | operator-- () |
Prefix decrement. More... | |
real | operator-- (int) |
Suffix decrement. More... | |
template<typename T > | |
auto | operator*= (const T &x) -> decltype(this->in_place_mul(x)) |
In-place multiplication. More... | |
template<typename T > | |
auto | operator/= (const T &x) -> decltype(this->in_place_div(x)) |
In-place division. More... | |
real & | multiply_accumulate (const real &r1, const real &r2) |
Combined multiply-add. More... | |
real | pow (const real &exp) const |
Exponentiation. More... | |
real | gamma () const |
Gamma function. More... | |
real | lgamma () const |
Logarithm of the gamma function. More... | |
real | exp () const |
Exponential. More... | |
real | binomial (const real &) const |
Binomial coefficient. More... | |
real | abs () const |
Absolute value. More... | |
real | sin () const |
Sine. More... | |
real | cos () const |
Cosine. More... | |
real | pi () const |
Pi constant. 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 = real, 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 direct access to the internal MPFR instance. | |
std::remove_extent<::mpfr_t >::type * | get_mpfr_t () |
Get a mutable reference to the internal mpfr_t instance. | |
const std::remove_extent<::mpfr_t >::type * | get_mpfr_t () const |
Get a const reference to the internal mpfr_t instance. | |
Friends | |
template<typename T , generic_in_place_enabler< T > = 0> | |
T & | operator+= (T &x, const real &r) |
Generic in-place addition with piranha::real. More... | |
template<typename T , typename U > | |
auto | operator+ (const T &x, const U &y) -> decltype(real::binary_add(x, y)) |
Generic binary addition involving piranha::real. More... | |
template<typename T , generic_in_place_enabler< T > = 0> | |
T & | operator-= (T &x, const real &r) |
Generic in-place subtraction with piranha::real. More... | |
template<typename T , typename U > | |
auto | operator- (const T &x, const U &y) -> decltype(real::binary_sub(x, y)) |
Generic binary subtraction involving piranha::real. More... | |
template<typename T , generic_in_place_enabler< T > = 0> | |
T & | operator*= (T &x, const real &r) |
Generic in-place multiplication by piranha::real. More... | |
template<typename T , typename U > | |
auto | operator* (const T &x, const U &y) -> decltype(real::binary_mul(x, y)) |
Generic binary multiplication involving piranha::real. More... | |
template<typename T , generic_in_place_enabler< T > = 0> | |
T & | operator/= (T &x, const real &r) |
Generic in-place division by piranha::real. More... | |
template<typename T , typename U > | |
auto | operator/ (const T &x, const U &y) -> decltype(real::binary_div(x, y)) |
Generic binary division involving piranha::real. More... | |
template<typename T , typename U > | |
auto | operator== (const T &x, const U &y) -> decltype(real::binary_equality(x, y)) |
Generic equality operator involving piranha::real. More... | |
template<typename T , typename U > | |
auto | operator!= (const T &x, const U &y) -> decltype(!real::binary_equality(x, y)) |
Generic inequality operator involving piranha::real. More... | |
template<typename T , typename U > | |
auto | operator< (const T &x, const U &y) -> decltype(real::binary_less_than(x, y)) |
Generic less-than operator involving piranha::real. More... | |
template<typename T , typename U > | |
auto | operator<= (const T &x, const U &y) -> decltype(real::binary_leq(x, y)) |
Generic less-than or equal operator involving piranha::real. More... | |
template<typename T , typename U > | |
auto | operator> (const T &x, const U &y) -> decltype(real::binary_less_than(y, x)) |
Generic greater-than operator involving piranha::real. More... | |
template<typename T , typename U > | |
auto | operator>= (const T &x, const U &y) -> decltype(real::binary_leq(y, x)) |
Generic greater-than or equal operator involving piranha::real. More... | |
std::ostream & | operator<< (std::ostream &os, const real &r) |
Overload output stream operator for piranha::real. More... | |
std::istream & | operator>> (std::istream &is, real &r) |
Overload input stream operator for piranha::real. More... | |
Arbitrary precision floating-point class.
This class represents floating-point ("real") numbers of arbitrary size (i.e., the size is limited only by the available memory). The implementation consists of a C++ wrapper around the mpfr_t
type from the multiprecision MPFR library. Real numbers are represented in binary format and they consist of an arbitrary-size significand coupled to a fixed-size exponent.
Unless noted otherwise, this implementation always uses the MPFR_RNDN
(round to nearest) rounding mode for all operations.
This class interoperates with the same types as piranha::mp_integer and piranha::mp_rational, plus piranha::mp_integer and piranha::mp_rational themselves.
Unless noted otherwise, this class provides the strong exception safety guarantee for all operations. In case of memory allocation errors by GMP/MPFR routines, the program will terminate.
Move construction and move assignment will leave the moved-from object in a state that is destructible and assignable.
|
inline |
|
inline |
|
inlinenoexcept |
|
inlineexplicit |
Constructor from C string.
Will use the string str
and precision prec
to initialize the number. The expected string format, assuming representation in base 10, is described in the MPFR documentation.
str | string representation of the real number. |
prec | desired significand precision. |
std::invalid_argument | if the conversion from string fails or if the requested significand precision is not within the range allowed by the MPFR library. |
|
inlineexplicit |
|
inlineexplicit |
Copy constructor with different precision.
this
will be first initialised with precision prec
, and then other
will be assigned to this
.
other | real to be copied. |
prec | desired significand precision. |
std::invalid_argument | if the requested significand precision is not within the range allowed by the MPFR library. |
|
inlineexplicit |
Generic constructor.
T
is an interoperable type.x | object used to construct this . |
prec | desired significand precision. |
std::invalid_argument | if the requested significand precision is not within the range allowed by the MPFR library. |
|
inline |
|
inline |
Binomial coefficient.
This method will return this
choose y
. Any combination of real values is supported. The implementation uses the logarithm of the gamma function, thus the result will not be in general exact (even if this
and y
are integral values).
The returned value will have the maximum precision between this
and y
.
y | bottom value. |
this
choose y
.std::invalid_argument | if either this or y is not finite. |
unspecified | any exception resulting from arithmetic operations on piranha::real. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Convert from msgpack object.
std::string
and all the integral types used to define the internal representation of piranha::real satisfy piranha::has_msgpack_convert.This method will convert the object o
into this
. If f
is piranha::msgpack_format::binary, this method offers the basic exception safety guarantee and it performs minimal checking on the input data. Calling this method in binary mode will result in undefined behaviour if o
does not contain an integer serialized via msgpack_pack().
o | source object. |
f | the desired piranha::msgpack_format. |
std::invalid_argument | if, in binary mode, the number of serialized limbs is inconsistent with the precision. |
unspecified | any exception thrown by:
|
|
inline |
Pack in msgpack format.
Stream
satisfies piranha::is_msgpack_stream,std::string
and the integral types in terms of which the internal representation of piranha::real is defined satisfy piranha::has_msgpack_pack,mpfr_prec_t
can be safely converted to std::uint32_t
.This method will pack this
into p
. If f
is msgpack_format::portable, then the precision of this
and a decimal string representation of this
are packed in an array. Otherwise, an array of 4 elements storing the internal MPFR representation of this
is packed.
p | target msgpack::packer . |
f | the desired piranha::msgpack_format. |
unspecified | any exception thrown by:
|
Combined multiply-add.
Sets this
to this + (r1 * r2)
. If the precision of this
is less than the maximum precision max_prec
of the two operands r1
and r2
, the precision of this
will be set to max_prec
before performing the operation.
r1 | first argument. |
r2 | second argument. |
this
.
|
inline |
|
inlineexplicit |
Conversion operator.
T
is an interoperable type.Extract an instance of type T
from this
.
Conversion to bool
is always successful, and returns sign() != 0
. Conversion to the other integral types is truncated (i.e., rounded to zero), its success depending on whether or not the target type can represent the truncated value.
Conversion to floating point types is exact if the target type can represent exactly the current value. If that is not the case, the output value will be the nearest adjacent. If this
is not finite, corresponding non-finite values will be produced if the floating-point type supports them, otherwise an error will be produced.
Conversion of finite values to piranha::mp_rational will be exact. Conversion of non-finite values will result in runtime errors.
std::overflow_error | if the conversion fails in one of the ways described above. |
|
inline |
In-place multiplication.
T
is an interoperable type or piranha::real.Multiply by x
the current value of the real object.
If the precision prec
of x
is greater than the precision of this
, the precision of this
is changed to prec
before the operation takes place.
x | argument for the multiplication. |
this
.unspecified | any exception thrown by the contructor of piranha::mp_integer, if invoked. |
|
inline |
|
inline |
|
inline |
|
inline |
In-place addition.
T
is an interoperable type or piranha::real.Add x
to the current value of the real object.
If the precision prec
of x
is greater than the precision of this
, the precision of this
is changed to prec
before the operation takes place.
x | argument for the addition. |
this
.unspecified | any exception thrown by the contructor of piranha::mp_integer, if invoked. |
|
inline |
|
inline |
|
inline |
|
inline |
In-place subtraction.
T
is an interoperable type or piranha::real.Subtract x
from the current value of the real object.
If the precision prec
of x
is greater than the precision of this
, the precision of this
is changed to prec
before the operation takes place.
x | argument for the subtraction. |
this
.unspecified | any exception thrown by the contructor of piranha::mp_integer, if invoked. |
|
inline |
In-place division.
T
is an interoperable type or piranha::real.Divide by x
the current value of the real object.
If the precision prec
of x
is greater than the precision of this
, the precision of this
is changed to prec
before the operation takes place.
x | argument for the division. |
this
.unspecified | any exception thrown by the contructor of piranha::mp_integer, if invoked. |
|
inline |
Assignment operator from C++ string.
The implementation is equivalent to the assignment operator from C string.
str | string representation of the real to be assigned. |
this
.unspecified | any exception thrown by the assignment operator from C string. |
|
inline |
Assignment operator from C string.
The parsing rules are the same as in the constructor from string. The precision of this
will not be changed by the assignment operation, unless this
was the target of a move operation that left it in an uninitialised state. In that case, this
will be re-initialised with the default precision.
In case str
is malformed, before an exception is thrown the value of this
will be reset to zero.
str | string representation of the real to be assigned. |
this
.std::invalid_argument | if the conversion from string fails. |
|
inline |
Generic assignment operator.
T
is an interoperable type.The precision of this
will not be changed by the assignment operation, unless this
was the target of a move operation that left it in an uninitialised state. In that case, this
will be re-initialised with the default precision.
x | object that will be assigned to this . |
this
.
|
inline |
|
inline |
Set precision.
Will set the significand precision of this
to exactly prec
bits, and reset the value of this
to NaN.
prec | desired significand precision. |
std::invalid_argument | if the requested significand precision is not within the range allowed by the MPFR library. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
friend |
Generic inequality operator involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.Note that in all comparison operators, apart from piranha::real::operator!=(), if any operand is NaN false
will be returned.
x | first argument |
y | second argument. |
true
if x != y
, false
otherwise.
|
friend |
Generic binary multiplication involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.The return type is always piranha::real.
x | first argument |
y | second argument. |
x * y
.unspecified | any exception thrown by the corresponding in-place operator. |
|
friend |
Generic in-place multiplication by piranha::real.
T
is a non-const interoperable type.Multiply by a piranha::real in-place. This method will first compute x * r
, cast it back to T
via static_cast
and finally assign the result to x
.
x | first argument. |
r | second argument. |
x
.unspecified | any exception resulting from the binary operator or by casting piranha::real to T . |
|
friend |
Generic binary addition involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.The return type is always piranha::real.
x | first argument |
y | second argument. |
x + y
.unspecified | any exception thrown by the corresponding in-place operator. |
|
friend |
Generic in-place addition with piranha::real.
T
is a non-const interoperable type.Add a piranha::real in-place. This method will first compute r + x
, cast it back to T
via static_cast
and finally assign the result to x
.
x | first argument. |
r | second argument. |
x
.unspecified | any exception resulting from the binary operator or by casting piranha::real to T . |
|
friend |
Generic binary subtraction involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.The return type is always piranha::real.
x | first argument |
y | second argument. |
x - y
.unspecified | any exception thrown by the corresponding in-place operator. |
|
friend |
Generic in-place subtraction with piranha::real.
T
is a non-const interoperable type.Subtract a piranha::real in-place. This method will first compute x - r
, cast it back to T
via static_cast
and finally assign the result to x
.
x | first argument. |
r | second argument. |
x
.unspecified | any exception resulting from the binary operator or by casting piranha::real to T . |
|
friend |
Generic binary division involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.The return type is always piranha::real.
x | first argument |
y | second argument. |
x / y
.unspecified | any exception thrown by the corresponding in-place operator. |
|
friend |
Generic in-place division by piranha::real.
T
is a non-const interoperable type.Divide by a piranha::real in-place. This method will first compute x / r
, cast it back to T
via static_cast
and finally assign the result to x
.
x | first argument. |
r | second argument. |
x
.unspecified | any exception resulting from the binary operator or by casting piranha::real to T . |
|
friend |
Generic less-than operator involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.Note that in all comparison operators, apart from piranha::real::operator!=(), if any operand is NaN false
will be returned.
x | first argument |
y | second argument. |
true
if x < y
, false
otherwise.
|
friend |
Overload output stream operator for piranha::real.
The output format for finite numbers is normalised scientific notation, where the exponent is signalled by the letter 'e' and suppressed if null.
For non-finite numbers, the string representation is one of "nan", "inf" or "-inf".
os | output stream. |
r | piranha::real to be directed to stream. |
os
.std::invalid_argument | if the conversion to string via the MPFR API fails. |
std::overflow_error | if the exponent is smaller than an implementation-defined minimum. |
unspecified | any exception thrown by memory allocation errors in standard containers. |
|
friend |
Generic less-than or equal operator involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.Note that in all comparison operators, apart from piranha::real::operator!=(), if any operand is NaN false
will be returned.
x | first argument |
y | second argument. |
true
if x <= y
, false
otherwise.
|
friend |
Generic equality operator involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.Note that in all comparison operators, apart from piranha::real::operator!=(), if any operand is NaN false
will be returned.
x | first argument |
y | second argument. |
true
if x == y
, false
otherwise.
|
friend |
Generic greater-than operator involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.Note that in all comparison operators, apart from piranha::real::operator!=(), if any operand is NaN false
will be returned.
x | first argument |
y | second argument. |
true
if x > y
, false
otherwise.
|
friend |
Generic greater-than or equal operator involving piranha::real.
T
is piranha::real and U
is an interoperable type,U
is piranha::real and T
is an interoperable type,T
and U
are piranha::real.Note that in all comparison operators, apart from piranha::real::operator!=(), if any operand is NaN false
will be returned.
x | first argument |
y | second argument. |
true
if x >= y
, false
otherwise.
|
friend |
Overload input stream operator for piranha::real.
Equivalent to extracting a line from the stream and then assigning it to r
.
is | input stream. |
r | real to which the contents of the stream will be assigned. |
is
.unspecified | any exception thrown by the assignment operator from string of piranha::real. |