When an expression requires the use of temporary intermediate mpf_class
values, like f=g*h+x*y
, those temporaries will have the same precision
as the destination f
. Explicit constructors can be used if this
doesn’t suit.
(type op)
¶(type op, mp_bitcnt_t prec)
¶Construct an mpf_class
. Any standard C++ type can be used, except
long long
and long double
, and any of the GMP C++ classes can be
used.
If prec is given, the initial precision is that value, in bits. If
prec is not given, then the initial precision is determined by the type
of op given. An mpz_class
, mpq_class
, or C++
builtin type will give the default mpf
precision (see Initialization Functions). An mpf_class
or expression will give the precision of that
value. The precision of a binary expression is the higher of the two
operands.
mpf_class f(1.5); // default precision mpf_class f(1.5, 500); // 500 bits (at least) mpf_class f(x); // precision of x mpf_class f(abs(x)); // precision of x mpf_class f(-g, 1000); // 1000 bits (at least) mpf_class f(x+y); // greater of precisions of x and y
explicit
mpf_class::mpf_class (const mpf_t f)
¶(const mpf_t f, mp_bitcnt_t prec)
¶Construct an mpf_class
from an mpf_t
. The value in f is
copied into the new mpf_class
, there won’t be any permanent association
between it and f.
If prec is given, the initial precision is that value, in bits. If prec is not given, then the initial precision is that of f.
explicit
mpf_class::mpf_class (const char *s)
¶(const char *s, mp_bitcnt_t prec, int base = 0)
¶explicit
mpf_class::mpf_class (const string& s)
¶(const string& s, mp_bitcnt_t prec, int base = 0)
¶Construct an mpf_class
converted from a string using mpf_set_str
(see Assignment Functions). If prec is given, the initial precision is
that value, in bits. If not, the default mpf
precision
(see Initialization Functions) is used.
If the string is not a valid float, an std::invalid_argument
exception
is thrown. The same applies to operator=
.
mpf_class
operator"" _mpf (const char *str)
¶With C++11 compilers, floats can be constructed with the syntax
1.23e-1_mpf
which is equivalent to mpf_class("1.23e-1")
.
mpf_class&
mpf_class::operator= (type op)
¶Convert and store the given op value to an mpf_class
object. The
same types are accepted as for the constructors above.
Note that operator=
only stores a new value, it doesn’t copy or change
the precision of the destination, instead the value is truncated if necessary.
This is the same as mpf_set
etc. Note in particular this means for
mpf_class
a copy constructor is not the same as a default constructor
plus assignment.
mpf_class x (y); // x created with precision of y mpf_class x; // x created with default precision x = y; // value truncated to that precision
Applications using templated code may need to be careful about the assumptions
the code makes in this area, when working with mpf_class
values of
various different or non-default precisions. For instance implementations of
the standard complex
template have been seen in both styles above,
though of course complex
is normally only actually specified for use
with the builtin float types.
mpf_class
abs (mpf_class op)
¶mpf_class
ceil (mpf_class op)
¶int
cmp (mpf_class op1, type op2)
¶int
cmp (type op1, mpf_class op2)
¶bool
mpf_class::fits_sint_p (void)
¶bool
mpf_class::fits_slong_p (void)
¶bool
mpf_class::fits_sshort_p (void)
¶bool
mpf_class::fits_uint_p (void)
¶bool
mpf_class::fits_ulong_p (void)
¶bool
mpf_class::fits_ushort_p (void)
¶mpf_class
floor (mpf_class op)
¶mpf_class
hypot (mpf_class op1, mpf_class op2)
¶double
mpf_class::get_d (void)
¶long
mpf_class::get_si (void)
¶string
mpf_class::get_str (mp_exp_t& exp, int base = 10, size_t digits = 0)
¶unsigned long
mpf_class::get_ui (void)
¶int
mpf_class::set_str (const char *str, int base)
¶int
mpf_class::set_str (const string& str, int base)
¶int
sgn (mpf_class op)
¶mpf_class
sqrt (mpf_class op)
¶void
mpf_class::swap (mpf_class& op)
¶void
swap (mpf_class& op1, mpf_class& op2)
¶mpf_class
trunc (mpf_class op)
¶These functions provide a C++ class interface to the corresponding GMP C routines.
cmp
can be used with any of the classes or the standard C++ types,
except long long
and long double
.
The accuracy provided by hypot
is not currently guaranteed.
mp_bitcnt_t
mpf_class::get_prec ()
¶void
mpf_class::set_prec (mp_bitcnt_t prec)
¶void
mpf_class::set_prec_raw (mp_bitcnt_t prec)
¶Get or set the current precision of an mpf_class
.
The restrictions described for mpf_set_prec_raw
(see Initialization Functions) apply to mpf_class::set_prec_raw
. Note in particular that the
mpf_class
must be restored to its allocated precision before being
destroyed. This must be done by application code, there’s no automatic
mechanism for it.