mpz division to yield a double

Zimmermann Paul Paul.Zimmermann at loria.fr
Sun Jun 17 14:21:15 CEST 2012


       Sam,

> If, however, one of the numbers "isinf," (example: [2]) then a second
> method is called, which gets into the internals of the bignum
> implementation to perform the division. The obvious solution would be to
> initialize mpf_t's out of my divisor and dividend, but with (the current
> version of) mini-gmp, I only have mpz_*, and mpn_* methods.
> 
> [2] An example that falls into this "isinf" category would be something
> like: 3*10^100 / 2*10^100, and I want the result as a double: 1.5.

in that case, assume the numerator has n bits and the denominator has d bits,
multiply the numerator by 2^max(0,d-n+54), divide the scaled numerator by
the denominator (as mpz_t), then convert the integer quotient to double,
and scale back dividing by 2^max(0,d-n+54) using the ldexp function for
example.

In your example both the numerator and denominator have 334 bits, you scale the
numerator by 2^54, dividing by d gives 27021597764222976, dividing by 2^54
gives 1.5.

Paul Zimmermann


More information about the gmp-discuss mailing list