Using mpq_t for aggregating currencies with wildly varying ranges

Donovan Hide donovanhide at
Tue Apr 15 15:28:32 UTC 2014


I've been doing some work on making the Ripple Currency format aggregatable
in MySQL by writing a parser and summation functions for this currency

The docs aren't great! The issue I've got is that I have an mpq_t variable
to which I'm adding various transaction amounts which have hugely variable
exponents, which works well and gives exact results. All denominators are
always powers of ten. However when it comes to outputting the value as a
decimal string, I've been doing something like:

mpq_class total=make_rational(bigendian_decode(args->args[0]));

which suffers from floating point drift and isn't accurate. I've been
looking at how pgmp for postgres solve the same problem and it's a bit
messy, using integers rather than rationals:

Does anyone have any suggestions on how to format rationals as exact
decimals, with the proviso that the denominator is always a power of ten
and thus the number should be non-recurring?


More information about the gmp-discuss mailing list