Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High precision floating point numbers in Haskell?

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

like image 829
MYV Avatar asked Jun 01 '13 23:06

MYV


People also ask

How many digits is a precision float?

A single-precision float only has about 7 decimal digits of precision (actually the log base 10 of 223, or about 6.92 digits of precision). The greater the integer part is, the less space is left for floating part precision.

What is 32-bit floating point precision?

Single-precision floating-point format (sometimes called FP32 or float32) is a computer number format, usually occupying 32 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point.

How do you find the precision of a floating point?

Similarly, in case of double precision numbers the precision is log(10) (252) = 15.654 = 16 decimal digits. Accuracy: Accuracy in floating point representation is governed by number of significant bits, whereas range is limited by exponent. Not all real numbers can exactly be represented in floating point format.

Are floating point numbers precise?

A single-precision, floating-point number is a 32-bit approximation of a real number. The number can be zero or can range from -3.40282347E+38 to -1.17549435E-38, or from 1.17549435E-38 to 3.40282347E+38.


Video Answer


3 Answers

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.
like image 93
Thomas M. DuBuisson Avatar answered Oct 03 '22 22:10

Thomas M. DuBuisson


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.

like image 39
Mekeor Melire Avatar answered Oct 03 '22 22:10

Mekeor Melire


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

like image 44
L29Ah Avatar answered Oct 03 '22 21:10

L29Ah