# Using mpq_t for aggregating currencies with wildly varying ranges

David Gillies daggillies at gmail.com
Wed Apr 16 18:29:45 UTC 2014

```I coded this de-canonicalise code up very quickly and can confirm it works.
One caveat: if the denominator is not of the form 2^m 5^n then it will
fail. But checking this is trivial.

On Tue, Apr 15, 2014 at 11:38 PM, Zimmermann Paul
<Paul.Zimmermann at inria.fr>wrote:

>        Hi Donovan,
>
> > Date: Tue, 15 Apr 2014 22:36:18 +0100
> > From: Donovan Hide <donovanhide at gmail.com>
> >
> > 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...
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
>

--
David Gillies
San Jose
Costa Rica
```