GMP precision with mpf_set_d
Jørgen Fulsebakke
jorgen.fulsebakke at gmail.com
Tue Jun 21 19:19:55 CEST 2011
Hello! Someone at a forum advised me to ask my question here, so I hope I'm
following the rules etc. Anyway:
When using the gmp library for high precision calculations, is it right that
if I use mpf_set_d(...) to assign a double value to an mp float, the
precision of other mp floats used together with this one is truncated to
double precision, potentially losing information?
As a test I use this program:
1
2
3
4
5
6
7
8
9
10
11
12
mpf_t x, y, z;
mpf_init2(x, 1000000);
mpf_init2(y, 1000000);
mpf_init2(z, 1000000);
mpf_set_str(x, "1e0", 10);
mpf_set_d(y, 3.3124365842565);
mpf_set_d(z, 3.3124365842566);
mpf_mul(y, x, y);
mpf_mul(z, x, z);
mpf_sub(x, y, z);
cout << setprecision(100) << x << endl;
(Notice the difference in the last digits of y and z), and get the output
-9.9920...e-14. If I add even more digits to y and z I get output 0. Thus it
seems that although I initialized the variables to 1000000 bits the
precision is still around 15 digits.
(Also: according to the tutorial at http://gmplib.org/manual/ it should not
be necessary to use init2 like this but only init, however to get high
enough precision in other small test programs I have to use init2)
Thanks!
More information about the gmp-discuss
mailing list