talira

scientific

Numbers in scientific notation for Haskell

open sourceBSD-3-Clausefree

scientific is a Haskell package by Bas van Dijk that provides the Scientific number type: space-efficient, arbitrary-precision numbers stored as an Integer coefficient and a base-10 exponent. It's made as the target for parsing numbers from untrusted input: it's cheaper to build than a Rational, and a number with a huge exponent such as 1e1000000000 doesn't fill memory and crash the program, while converting to Int, Double or Float uses space bounded by the target type.

Problems it solves

  • parse decimal numbers from untrusted JSON in Haskell without risking a crash on huge exponents
  • represent exact decimal values in Haskell instead of lossy Doubles
  • convert numbers with huge exponents to Int or Double in Haskell without unbounded memory use

Features

  • Scientific type: Integer coefficient with a base-10 exponent
  • Safe to parse from untrusted input: huge exponents don't exhaust memory
  • Cheaper to construct than Rational
  • Conversions to integral and floating types with bounded space

Limitations

  • Its exponent is an Int, so numbers aren't truly arbitrary precision1