mpf_class bug in attempting to set precision of floats?

Randall randallrathbun at gmail.com
Mon Mar 22 17:39:21 CET 2010


Hello there:

After a couple of days, compiling the gmp++ code under both cygwin and 
linux environments (latest gmp 5.0.1 and gcc 4.3.4-3), I have decided 
that the code below shows a problem in converting from std::string to 
the mpf_class.  Please verify that problems exist with accuracy of the 
mpf_class.  Additionally there appears to be a problem with precision 
changing dynamically but unfortunately towards less precision.

--------- example.cpp -------------------
#include <iostream>
#include <iomanip>
#include <string>
#include <gmpxx.h>
using namespace std;

int main(void) {

  const static long precision = 211;
  mpf_set_default_prec(precision);  // necessary to create floats with 
high precision
  cout << setprecision(precision);

  string 
value("0.0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");

  cout << "value = " << value << endl;

  mpf_class tiny(value);
  cout << "tiny =  " << tiny << endl;

  mpf_class reciprocal;
  reciprocal.set_prec(precision);
  reciprocal = 1.0 / 81.0;

  cout << "reciprocal = " << reciprocal << endl;

  return 0;
}
------- end example.cpp -----------------------

As seen from both my 64 bit linux machine and my 32 dual-core Windows 
XP, I am not getting the full precision that I expected.

Could you please look into this?

Please use "g++ example.cpp -lgmp -lgmpxx" for the compile line.

Thank you.

Randall



More information about the gmp-bugs mailing list