mpq_get_d
Paul Zimmermann
Paul.Zimmermann at loria.fr
Wed Jan 11 19:34:44 CET 2006
The mpq_get_d documentation is the following:
- Function: double mpq_get_d (mpq_t OP)
Convert OP to a `double'.
A few more words would be helpful:
1) does the conversion depend on the machine rounding mode?
2) if yes/no, how is the conversion (mathematically) defined?
For gmp-4.1.4, the asnwer to 1) seems to be 'yes':
for q=-1/26530884065595459276087454401792, rnd=-Inf
mpq_get_d gets -3.7691921517865029e-32
for q=-1/26530884065595459276087454401792, rnd=+Inf
mpq_get_d gets -3.7691921517865035e-32
Unfortunately the rounding direction is the opposite to the one asked...
For 2), the conversion is not optimal. Here is an example with a rounding
precision of 53 bits, where the result of mpq_get_d is too large by 1 ulp
(but in the right direction):
for 1/3, rnd=+Inf
mpq_get_d gets 3.3333333333333343e-01
mpfr_set_q gets 3.3333333333333337e-01
and with a rounding precision of 64 bits (default on x86):
for q=26530884065595459276087454401793/5539952800230608, rnd=+Inf
mpq_get_d gets 4.7890090443534240e+15
mpfr_set_q gets 4.7890090443534250e+15
This last example gives a wrong directed rounding (not only it is not the
correct rounding, but also the wrong direction!).
Both problems can be fixed easily:
1) for negative numbers, put the sign right away (while accumulating the limbs)
2) for non-optimal results, it would be better to accumulate the limbs from
least to most significant, and to take into account the remainder.
Paul
More information about the gmp-discuss
mailing list