High precision floating point numbers in Haskell?

MYV picture MYV · Jun 2, 2013 · Viewed 12k times · Source

I know Haskell has native data types which allow you to have really big integers so things like

>> let x = 131242358045284502395482305
>> x
131242358045284502395482305

work as expected. I was wondering if there was a similar "large precision float" native structure I could be using, so things like

>> let x = 5.0000000000000000000000001
>> x
5.0000000000000000000000001

could be possible. If I enter this in Haskell, it truncates down to 5 if I go beyond 15 decimal places (double precision).

Answer

Thomas M. DuBuisson picture Thomas M. DuBuisson · Jun 2, 2013

Depending on exactly what you are looking for:

  • Float and Double - pretty much what you know and "love" from Floats and Doubles in all other languages.
  • Rational which is a Ratio of Integers
  • FixedPoint - This package provides arbitrary sized fixed point values. For example, if you want a number that is represented by 64 integral bits and 64 fractional bits you can use FixedPoint6464. If you want a number that is 1024 integral bits and 8 fractional bits then use $(mkFixedPoint 1024 8) to generate type FixedPoint1024_8.
  • EDIT: And yes, I just learned about the numbers package mentioned above - very cool.