Marc Glisse marc.glisse at
Fri Apr 12 11:28:01 CEST 2013

On Fri, 12 Apr 2013, Zimmermann Paul wrote:

> the mpq_get_d function rounds towards zero (i.e., truncates).
> In several applications, people usually prefer rounding to nearest.
> Is it planned to provide a function (say mpq_get_d_nearest) for that?
> I could contribute it, based on the code below (which does not deal with
> subnormal numbers yet).
> We could also have rounding towards -infinity and +infinity, which would be
> useful for people doing interval arithmetic.

Preferably with a single function that returns both roundings :-)
That would allow us to remove this slow code from CGAL (and stop linking
with mpfr in many cases):

   operator()( const mpq_class& x ) const {
     mpfr_t y;
     mpfr_init2 (y, 53); /* Assume IEEE-754 */
     mpfr_set_q (y, x.get_mpq_t(), GMP_RNDD);
     double i = mpfr_get_d (y, GMP_RNDD); /* EXACT but can overflow */
     mpfr_set_q (y, x.get_mpq_t(), GMP_RNDU);
     double s = mpfr_get_d (y, GMP_RNDU); /* EXACT but can overflow */
     mpfr_clear (y);
     return std::pair<double, double>(i, s);

(I hadn't looked at that code in a while, it could at least use 
MPFR_DECL_INIT I guess, but best would be not needing mpfr)

Marc Glisse

More information about the gmp-devel mailing list