C++ mpf_class Precision

Henry henryxxll at gmail.com
Mon Jun 1 22:52:14 CEST 2009


Greetings GMP Discussion Group,

I am having some trouble setting the precision of mpf_class variables in
C++.

For example, when I compile and run the following code:
*
#include <iostream>**
**#include <gmp.h>
#include <gmpxx.h>
#include <math.h>
using namespace std;

int main(){
mpf_class x;
x=1.234567891234567899052395;
cout.precision(100);
cout<<x;
return 0;
}**
*
I get *1.23456789123456789348*. While the precision continues for some time,
the number suddenly becomes very inaccurate, and then suddenly cuts out all
together.

The cout precision should be all right, but the mpf_class precision would
seem not to be. I looked at
http://gmplib.org/manual/C_002b_002b-Interface-Floats.html, and changed my
code accordingly:

*#include <iostream>**
**#include <gmp.h>
#include <gmpxx.h>
#include <math.h>
using namespace std;

int main(){
mpf_class x(1.5, 512);
x=1.23456789123456789123456789;
cout.precision(100);
cout<<x;
return 0;
}**
*
This, however, still outputted a questionable number: *
1.2345678912345678934769921397673897445201873779296875*.

Can anyone shed some light on what I might be doing wrong?

Thanks,
Henry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gmplib.org/list-archives/gmp-discuss/attachments/20090601/6d5acefd/attachment.html>


More information about the gmp-discuss mailing list