3

What is the best type to use to represent financial amounts in Haskell?

I am doing some heavy calculations on the price of a financial product and would like to know which is the best type to use when there could potentially be thousands of arithmetic operations done on the price and a list of prices could be averaged.

At the moment I am using Double but I don't think that may be the best choice. Others types I have come across are RealFrac and Fractional.

Update

For the person that has marked this post to be closed, the link provided debates whether to use Double or Float. My post is concerned with discounting both these types in favour of a potentially better suited type to represent financial amounts and calculations using financial amounts. Moreover, as @Carsten indicates Double is not a good idea.

  • 4
    whatever you use - `Double` is usually no good idea if you are working with money and interest: [see this question](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Random Dev Apr 05 '16 at 10:03
  • 4
    I think [Decimal](https://hackage.haskell.org/package/Decimal) might be a good way to start – Random Dev Apr 05 '16 at 10:04
  • 2
    Usually, it is sufficient to use an integral type, for example, store $19.99 as 1999, and convert to $19.99 for display purposes. This assumes you know ahead of time what the smallest unit of currency is. If you need to deal with arbitrary fractions of a unit, then something with fixed precision (like Decimal) is more appropriate. Under no circumstances should you use a floating-point type. – chepner Apr 05 '16 at 10:18
  • `Fractional` is a type class, not a type. – Bartek Banachewicz Apr 05 '16 at 10:40
  • 1
    It depends. All valuation, risk management and P&L software I saw uses Doubles because lot of formulas contain transcendental operations which renders precise math useless. On other hand precise math makes sense for accounting. But I never seen accounting software which uses precise math, for some reason people stick to fixed-point math in such software. – schernichkin Apr 05 '16 at 11:02
  • Use `Integer` (or `Int`) to count the cents (or whatever the smallest denomination of your currency is.) – PyRulez Apr 05 '16 at 11:57
  • A quote from the Decimal library @Carsten was talking about "While (/) is defined, you don't normally want to use it. Instead The functions "divide" and "allocate" will split a decimal amount into lists of results which are guaranteed to sum to the original number. **This is a useful property when doing financial arithmetic**" (emphasis added). – PyRulez Apr 05 '16 at 11:59
  • Has anyone used [cyclotomic](http://hackage.haskell.org/package/cyclotomic)? –  Apr 05 '16 at 12:17

0 Answers0