Using mpq_t for aggregating currencies with wildly varying ranges

Zimmermann Paul Paul.Zimmermann at
Wed Apr 16 05:38:42 UTC 2014

       Hi Donovan,

> Date: Tue, 15 Apr 2014 22:36:18 +0100
> From: Donovan Hide <donovanhide at>
> Hi Paul,
> thanks very much for this clever tip! Almost too clever for the
> bit-twiddling part of my brain to deal with :-)
> Unfortunately it does look like gmp is simplifying the fractions in some
> cases, even though I'm not calling canonicalize() anywhere...
> Should I be looking at mpfr instead, or can I hack this some other way?
> Feels like there's a missing method on the rational type for this set of
> cases...
> Cheers,
> Donovan.

then you can de-canonicalize() the fraction as follows:

- with mpz_scan1, get the power of 2 in the denominator, say m
- with mpz_remove, get the power of 5 in the denominator, say n
- if m > n, multiply the *numerator* by 5^(m-n)
- otherwise multiply the *numerator* by 2^(n-m)

Then you can use the method I suggested with n = max(n,m).

You can use MPFR of course, but this is off-topic for this list.

Paul Zimmermann

PS: a mpz_remove_ui function would be most welcome...

More information about the gmp-discuss mailing list