27

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).

hammar
  • 138,522
  • 17
  • 304
  • 385
MYV
  • 4,294
  • 6
  • 28
  • 24
  • 2
    Well, there's always `Rational` in `Data.Ratio` for abitrary precision fractions. But generally, the answer depends on what operations do you need it to support. – Vitus Jun 01 '13 at 23:28
  • 1
    I suppose [`Rational`](http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#t:Rational) is not ok for your purposes, i.e. you need the `Floating` instance? – leftaroundabout Jun 01 '13 at 23:28
  • 2
    For the highest possible precision: http://www.haskell.org/haskellwiki/Exact_real_arithmetic (AERN implementation hasn't worked for a while though... I don't know if there are other candidates right now) – luqui Jun 02 '13 at 00:09

4 Answers4

21

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.
Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
  • 4
    There's also [Data.Fixed](http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Fixed.html) in `base`. – hammar Jun 02 '13 at 01:09
  • 5
    There's also a library for [cyclotomic numbers](http://hackage.haskell.org/package/cyclotomic) which are still exact, include all the rationals and more, and have more decidable predicates than the computable reals do. – Daniel Wagner Jun 02 '13 at 01:37
  • I am wondering why the precision is not lazy in a lazy language like Haskell. I think it would be convenient, to get as much precision as needed for the result. – ceving Dec 08 '22 at 14:48
  • In a word, performance. Languages use doubles and machine words because that is what performs well. You can likely find lazy numbers implemented in Haskell but the performance will be much worse. – Thomas M. DuBuisson Dec 14 '22 at 22:41
9

Haskell does not have high-precision floating-point numbers naitively.

For a package/module/library for this purpose, I'd refer to this answer to another post. There's also an example which shows how to use this package, called numbers.

Community
  • 1
  • 1
Mekeor Melire
  • 502
  • 5
  • 15
1

If you need a high precision /fast/ floating point calculations, you may need to use FFI and long doubles, as the native Haskell type is not implemented yet (see https://ghc.haskell.org/trac/ghc/ticket/3353).

L29Ah
  • 268
  • 1
  • 16
1

I believe the standard package for arbitrary precision floating point numbers is now https://hackage.haskell.org/package/scientific

jberryman
  • 16,334
  • 5
  • 42
  • 83
  • 1
    It is certainly used quite widely, probably in part due to aeson's reliance on it. But it isn't really floating-point at all in the standard sense (being base-10), and as a consequence very badly suited for any actual computations. Sure it avoids the “problems” of representing long decimal numbers, but for many applications it just doesn't make sense to even consider decimal. – leftaroundabout Sep 02 '22 at 10:10