mpz_get_ld

Zimmermann Paul Paul.Zimmermann at loria.fr
Mon Jun 18 22:46:05 CEST 2012


       Marc,

> If the function mpz_get_ld is provided, I expect it to work for the whole 
> (exponent) range of long double value, not just the range of double. 
> Should we first do something about large z, then apply your method and fix 
> the result with ldexpl?

you are right. Here is an improved version.

long double
mpz_get_ld (mpz_t z)
{
  long double h, l;
  long int e;
  mpz_t tmp;
  
  h = (long double) mpz_get_d_2exp (&e, z); /* 0.5 <= |h| < 1 */
  h = ldexpl (h, 53); /* now h is an integer */
  e -= 53;
  mpz_init (tmp);
  mpz_set_d (tmp, (double) h); /* exact */
  if (e >= 0)
    mpz_mul_2exp (tmp, tmp, e);
  else
    mpz_div_2exp (tmp, tmp, -e);
  mpz_sub (tmp, z, tmp);
  h = ldexpl (h, e);
  l = (long double) mpz_get_d_2exp (&e, tmp);
  l = ldexpl (l, e);
  mpz_clear (tmp);
  return h + l;
}

Paul


More information about the gmp-devel mailing list