-
Notifications
You must be signed in to change notification settings - Fork 34
Variable precision data types
A few words on multiprecision variables using Boost.Multiprecision's mpfr_float class.
First, new variables always are created at whatever the current default_precision
is. This is gettable and settable by calling boost::multiprecision::mpfr_float::default_precision()
to get, or by passing in a desired number of digits to set. The number you get or set is the number of digits, not the number of bits.
So, if one creates a variable x
at default precision
50, x will have precision 50 until you change it. Subtly, say x
is still precision 50. Now you change default_precision(30)
, and create a new variable y
, by writing mp::mpfr_float y = x;
. The newly created y
will have precision 30, and carry the first 30 digits of x
. The old variable x
still has precision 50.
Setting a previously instantiated variable will use the precision of the right hand side. So, since y
already exists, even though it is currently precision 30, x is precision 50, so y = x;
will change y
's precision to 50 to match, and y really is x.
So, there are some things you need to pay attention to with respect to object lifetime and precision.
For more information about mixing precisions of objects using Boost.Multiprecision, please see the documentation.