scientific

Numbers represented using scientific notation

https://github.com/basvandijk/scientific

Version on this page:0.3.7.0@rev:7
LTS Haskell 22.37:0.3.7.0@rev:7
Stackage Nightly 2024-10-11:0.3.8.0@rev:1
Latest on Hackage:0.3.8.0@rev:1

See all snapshots scientific appears in

BSD-3-Clause licensed by Bas van Dijk
Maintained by Bas van Dijk
This version can be pinned in stack with:scientific-0.3.7.0@sha256:909755ab19b453169ff85281323da1488407776b2360bd9f7afdd219fd306ef2,4869

Module documentation for 0.3.7.0

Data.Scientific provides the number type Scientific. Scientific numbers are arbitrary precision and space efficient. They are represented using scientific notation. The implementation uses a coefficient c :: Integer and a base-10 exponent e :: Int. A scientific number corresponds to the Fractional number: fromInteger c * 10 ^^ e.

Note that since we're using an Int to represent the exponent these numbers aren't truly arbitrary precision. I intend to change the type of the exponent to Integer in a future release.

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 Integrals (like: Int) or RealFloats (like: Double or Float) will always be bounded by the target type.