GMP precision with mpf_set_d
Torbjorn Granlund
tg at gmplib.org
Tue Jun 21 19:35:46 CEST 2011
Jørgen Fulsebakke <jorgen.fulsebakke at gmail.com> writes:
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?
This question allows for many interpretations. The precision of an mpf
operation is documented:
"All calculations are performed to the precision of the destination
variable."
Ref: http://gmplib.org/manual/Floating_002dpoint-Functions.html
Does this answer your question.
As a test I use this program:
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;
This is not a complete example, so we cannot compile and run it.
(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.
"Add more digits"?
Thus it seems that although I initialized the variables to 1000000
bits the precision is still around 15 digits.
GMP will not be able to discern that 3.3124365842566 is actually
supposed to be sqrt(10). In fact, if it assigned *anything* but the
*exact* value of the IEEE 53-bit *approximation* of 3.3124365842566,
then that would be a GMP bug (or perhaps a bug in the compiler that read
the digit sequence "3 . 3 1 2 ..." and converted these into a IEEE
double).
--
Torbjörn
More information about the gmp-discuss
mailing list