[Haskell-cafe] Accuracy of Data.Number.Fixed

Lyle Kopnicky lists at qseep.net
Sat Mar 5 18:26:21 UTC 2016

Yes, that is similar to what the Decimal library does. It maintains a
precision value and a scaled integer.

So € 10.02 could be represented as:

Decimal 2 1002

It also has nice functions for dividing out currency into equal parts, or
ratios, while preserving the property that the total of the parts should
add up to the original value.

I just thought it might be interesting to encode the precision in the type,
and just use an integer in the representation. Then it's even more
efficient at runtime. Maybe I'll write my own library. Or perhaps there's
another one that does that already.

The Data.Number.Fixed library encodes the precision in the type, but it
uses rational number for the values, and only guarantees the precision
within an epsilon range, so that "two digits of precision" means it could
be +/- .01, which is not good for currency.

On Sat, Mar 5, 2016 at 12:26 AM, Imants Cekusins <imantc at gmail.com> wrote:

> > I prefer the Decimal library for currency.
> here is another option to consider:
> data Cur = Eur | ...
> data Money = Mint Cur Int | Mtup Cur (Int,Int)
> precision::Cur -> Int
> precision Eur = 2
> precision _ = 2
> € 10.02 would be:
> Mint Eur 1002
> Mtup Eur (10,2)
> This adds overhead to computations. However precision and correct currency
> is guaranteed.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160305/51e3da72/attachment.html>

More information about the Haskell-Cafe mailing list