Binary output of mpf_get_str() has too many bits?

Rick Regan exploringbinary at gmail.com
Wed May 19 17:18:50 CEST 2010


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:
11001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011
Precision = 128:
1100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011

Those outputs are 66, 98, and 130 bits, respectively. Why are they not 64,
96, and 128 bits?

Thanks.

Rick Regan


More information about the gmp-discuss mailing list