mpf_t and mpf_get_str

Paul Zimmermann Paul.Zimmermann at loria.fr
Mon Nov 19 19:53:53 CET 2007


       Dear Carlos,

> Am I doing something wrong? Can anyone explain?

No. Yes. GMP's mpf uses base 2^32 or 2^64 internally, depending on your
computer. This means that when you request 256 bits, mpf uses internally
more extra bits. On a 32-bit computer, it uses 10 words on your example,
and the second least significant words differ by 1 unit (the least
significant words completely differ). Here you get a "real" precision
of 292 bits (note that this depends on the number being represented).

Alternatively, you can use MPFR (www.mpfr.org). Then if you request 256
bits, you will get *exactly* 256 bits, and mpfr_get_str (NULL, &e, 2, 0, n)
will print all those bits.

For example, if I add the following two lines in your program:

#include "mpfr.h"
#include "mpf2mpfr.h"

and link it with -lmpfr, I get:

n_mant10 = 1875000026575627008461288879635329944698826788978426034113560035351407471672011
n_exp10  = -8

n_mant2  = 1000000011011001010110010110101111000111000001101111010111011001010010111101000011011101100111101110011101100111100111000001110000000110000101011111100000011011101011000010001000001011010100101111111011000001100101100010110010010101101110100100100001010100
n_exp2   = -28

m_mant10 = 1875000026575627008461288879635329944698826788978426034113560035351407471672011
m_exp10  = -8

m_mant2  = 1000000011011001010110010110101111000111000001101111010111011001010010111101000011011101100111101110011101100111100111000001110000000110000101011111100000011011101011000010001000001011010100101111111011000001100101100010110010010101101110100100100001010100
m_exp2   = -28

Paul Zimmermann



More information about the gmp-discuss mailing list