Skip to content

Arbitrary-precision floating-point numbers represented using scientific notation

License

Notifications You must be signed in to change notification settings

basvandijk/scientific

Folders and files

NameName
Last commit message
Last commit date

Latest commit

224f38f · May 11, 2021
May 11, 2021
May 11, 2018
May 11, 2021
May 11, 2021
May 11, 2021
Oct 10, 2015
Oct 22, 2013
Mar 8, 2016
Oct 22, 2013
May 11, 2021
May 11, 2021
Jul 8, 2019
Dec 5, 2014
May 11, 2021
Aug 30, 2017
Sep 27, 2015

Repository files navigation

Hackage Build Status

Data.Scientific provides a space efficient and arbitrary precision scientific number type.

Scientific numbers are represented using scientific notation. It uses a coefficient c :: Integer and a base-10 exponent e :: Int (do note that since we're using an Int to represent the exponent these numbers aren't truly arbitrary precision. I intend to change this to Integer in the future!). A scientific number corresponds to the Fractional number: fromInteger c * 10 ^^ e.

The main application of Scientific is to be used as the target of parsing arbitrary precision numbers coming from an untrusted source. The advantages over using Rational for this are that:

  • A Scientific is more efficient to construct. Rational numbers need to be constructed using % which has to compute the gcd of the numerator and denominator.

  • Scientific is safe against numbers with huge exponents. For example: 1e1000000000 :: Rational will fill up all space and crash your program. Scientific works as expected:

 > read "1e1000000000" :: Scientific
 1.0e1000000000
  • Also, the space usage of converting scientific numbers with huge exponents to Integral's (like: Int) or RealFloats (like: Double or Float) will always be bounded by the target type.