mpq_get_d_nearest
Zimmermann Paul
Paul.Zimmermann at inria.fr
Fri Apr 12 11:43:35 CEST 2013
Marc,
> Date: Fri, 12 Apr 2013 11:28:01 +0200 (CEST)
> From: Marc Glisse <marc.glisse at inria.fr>
>
> 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
note that in the above code, the return value of mpfr_set_q (which CGAL
ignores) is enough to use only one call to mpfr_get_d, then a call to
nextafter is enough.
But this is now off-topic, sorry...
Paul
More information about the gmp-devel
mailing list