Binary output of mpf_get_str() has too many bits?

John Westwood john at westwoodsolutions.com
Wed May 19 18:06:27 CEST 2010


Hi,

I'm not an expert on GMP / MPIR, but I believe the answer to this question is 
the same as the answer to a similar question I asked a few days ago.

When a specific precision is requested that is just the minimum precision that 
GMP will use. Internally, GMP may use any number of bits in excess of the 
specified amount.

The description of mpf_get_str in the manual states:

"If n_digits is 0 then that accurate maximum number of digits are generated."

So you could try specifying n_digits as 64, 96 and 128 and you should receive 
the correct number of bits when using a base of two.

John


On Wednesday 19 May 2010 16:18:50 Rick Regan wrote:
> I am using the C++ interface of GMP to print the binary value of an mpf
> variable's mantissa. I am getting more bits than I expect, given the
> precision I ask for. Here's a program that demonstrates this:
> 
> #include <iostream>
> using namespace std;
> #include <mpirxx.h> //Sorry ;)
> int main (void)
> {
>  mpf_class f;
>  mp_exp_t exponent;
> 
>  f.set_prec(64);
>  f = "0.1";
>  cout << "Precision = " << f.get_prec() << ": " << f.get_str(exponent,2) <<
> endl;
> 
>  f.set_prec(96);
>  f = "0.1";
>  cout << "Precision = " << f.get_prec() << ": " << f.get_str(exponent,2) <<
> endl;
> 
>  f.set_prec(128);
>  f = "0.1";
>  cout << "Precision = " << f.get_prec() << ": " << f.get_str(exponent,2) <<
> endl;
> }
> Output:
> 
> Precision = 64:
> 110011001100110011001100110011001100110011001100110011001100110011
> Precision = 96:
> 110011001100110011001100110011001100110011001100110011001100110011001100110
> 01100110011001100110011 Precision = 128:
> 110011001100110011001100110011001100110011001100110011001100110011001100110
> 0110011001100110011001100110011001100110011001100110011
> 
> Those outputs are 66, 98, and 130 bits, respectively. Why are they not 64,
> 96, and 128 bits?
> 
> Thanks.
> 
> Rick Regan
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
> 


More information about the gmp-discuss mailing list