Numbers represented using scientific notation

Version on this page:
LTS Haskell 8.2:
Stackage Nightly 2017-02-20:
Latest on Hackage:
BSD3 licensed by Bas van Dijk
Maintained by Bas van Dijk

Module documentation for

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

* Tighten lower bound on vector from 0.5 to 0.7 because building with
vector < 0.7 results in a build error.

* Move the internal modules Math.NumberTheory.Logarithms and
GHC.Integer.Logarithms.Compat to their own package integer-logarithms so
other people can share that code.
* Support QuickCheck-2.9.
* Make bytestring-builder's installation conditional based on a Cabal flag.
* Unconditionally export Data.ByteString.Builder.Scientific.
The bytestring-builder cabal flag has been removed.
Depend on bytestring-builder for backwards compatibility for GHC < 7.8.
* Made toDecimalDigits more similar to floatToDigits


toDecimalDigits 0 == ([0],1)


toDecimalDigits 0 == ([0],0)


Numeric.floatToDigits 10 (0 :: Double) == ([0],0)

* Introduce a special case for 0 in fromFloatDigits

fromFloatDigits 0 = 0

This should fix
The following are all a courtesy of Oleg Grenrus (phadej):

* Support GHC-8.0.1
* Support binary-0.8
* Enable Travis continuous integration
* Improved performance of toDecimalDigits by 13%.
* Fix build with integer-simple.
* Fix build on GHC-7.4.
Courtesy of Adam Bergmark..
* Fix build on GHC-7.0.4
* Added fromRationalRepetend & toRationalRepetend
for safely converting from and to rationals
which have a repeating decimal representation like:
1 % 28 = 0.03(571428).
* Added a Binary instance.
* Various performance improvements.
* Support vector-0.11
* Support tasty-0.11
* Support criterion-
* Support QuickCheck-2.8.
* Fixed both the

Prelude Data.Scientific> reads "0.0" :: [(Data.Scientific.Scientific,String)]

problem and the

read " 8" :: Scientific fails, while read " 8" :: Double succeeds


Courtesy of neongreen.
* Fixed bug in the x / y method for Scientific. Since I was using
the default implementation: `x * recip y` the operation would
diverge when `recip y` had an infinite decimal output.
This shouldn't happen when the result of / is finite again.
For example: 0.6 / 0.3 should yield 2.0.

This is now fixed by using the following implementation:

`x / y = fromRational $ toRational x / toRational y`
* Fixed bug when converting the Scientific:
`scientific 0 someBigExponent` to a bounded Integral using toBoundedInteger
or to a bounded RealFloat using toBoundedRealFloat.
If someBigExponent was big enough to trigger the big-exponent protection
the beforementioned functions didn't return 0.

This is fixed by explicitly handling a coefficient of 0.
* Relax upper version bounds of base and deepseq
for the test suite and benchmarks.
* Add support for `deepseq-1.4`.
* Fix parsing of empty digit string (#21).
* Allow newer tasty, tasty-hunit and criterion.
* Add the isFloating or isInteger predicates.
Courtesy of Zejun Wu (@watashi).
* Add the toRealFloat' and toBoundedInteger functions.
Courtesy of Fujimura Daisuke (@fujimura).
* Enable package to link with integer-simple instead of
integer-gmp using the -finteger-simple cabal flag.
Courtesy of @k0ral.

* Parameterize inclusion of the Data.ByteString.Builder.Scientific
module using the bytestring-builder flag. Disabling this flag
allows building on GHC-7.0.4 which has bytestring-0.9 installed
by default.

* Add the floatingOrInteger function
* Fix build on GHC-7.0.4
* More efficient and better behaving magnitude computation
* Lower the number of cached magnitudes to 324 (same as GHC.Float)

* Don't normalize on construction but do it when pretty-printing
instead. Also provide a manual normalize function.
* Improve efficiency of toRealFloat
* Added note about caching magnitudes
* Dropped dependency on arithmoi
* Make benchmark easier to build
* Add junit XML output support (for Jenkins)

* Lower the minimal QuickCheck version.
* Make sure sized exponents are generated in the QuickCheck tests.

* Fix build for bytestring-0.10.0.*

* Fix a DoS vulnerability that allowed an attacker to crash the
process by sending a scientific with a huge exponent like
* Fix various RealFrac methods.
* Cache some powers of 10 to speed up the magnitude computation.
* Normalize scientific numbers on construction.
* Move the Text Builder to its own module &
provide a ByteString builder
* Added more documentation

* Widen the dreaded pointlessly tight upper bounds

* Support the latest versions of smallcheck and tasty

* added deriving data

* Loosen upper bounds on package versions

* Fixed bugs & Changed API

* Support building the library on GHC >= 7.0.1

* Simplification in the Show instance
* Optimization in fromRealFloat

* Initial commit
Used by 183 packages:
Deadpan-DDP, HSmarty, QuickPlot, abeson, aeson, aeson-better-errors, aeson-compat, aeson-diff, aeson-extra, aeson-parsec-picky, aeson-pretty, aeson-qq, aeson-schema, aeson-utils, aeson-value-parser, amazonka-core, amby, anki-tools, ansi-pretty, attoparsec, attoparsec-data, aws, aws-dynamodb-streams, aws-ec2, binary-orphans, binary-parsers, binary-tagged, biohazard, bitcoin-payment-channel, bitx-bitcoin, bloodhound, bolt, bond, bond-haskell, bower-json, buffer-builder-aeson, bytes, clarifai, coinbase-exchange, configurator-ng, css-syntax, descriptive, dfrac, diversity, docker, doctemplates, dom-parser, dozens, dynamodb-simple, ede, elm-export-persistent, formatting, ghc-prof, ginger, gipeda, gogol-core, google-cloud, graphql-api, graql, graylog, groundhog, haiji, hakyll, handa-geodata, haskell-formatter, haskell-kubernetes, haskell-neo4j-client, hasql, hasql-generic, hasql-postgres, hastache-aeson, hdbc-aeson, hedis-config, hedn, hjsonschema, hops, hruby, hsdev, hslua-aeson, hsoz, hw-json-lens, hypher, ihaskell-widgets, influxdb, jmacro-rpc, jsaddle, json-ast, json-ast-json-encoder, json-autotype, json-bytes-builder, json-encoder, json-incremental-decoder, json-rpc-generic, json-schema, json-stream, json-togo, jsonschema-gen, jsonsql, jsontsv, jwt, katip-elasticsearch, kawhi, keenser, krapsh, language-conf, language-hcl, language-puppet, language-thrift, large-hashable, lendingclub, lens-aeson, liquid, matrix-market-attoparsec, megaparsec, microlens-aeson, msgpack-aeson, mustache, mustache-haskell, myo, mysql-haskell, mysql-haskell-nem, oanda-rest-api, pandoc, parsers, pdf-toolbox-core, persistent, persistent-redis, persistent-zookeeper, pg, pg-store, pgstream, picoparsec, postgresql-binary, postgresql-simple, postgresql-typed, postgrest, presto-hdbc, profiteur, purescript, pushbullet-types, pusher-ws, quick-schema, quickcheck-instances, quickcheck-special, raft, rakhana, rasterific-svg, rebase, respond, rest-gen, rethinkdb, rethinkdb-client-driver, rison, roundtrip-aeson, scholdoc, serokell-util, servant-pushbullet-client, servant-smsc-ru, sexp-grammar, shakespeare, simple-templates, snaplet-auth-acid, sprinkles, stratux-types, sunroof-server, svg-tree, swagger2, syntax, syntax-attoparsec, syntax-example, syntax-example-json, syntax-pretty, syntax-printer, tianbar, twilio, unjson, viewprof, webdriver, webdriver-snoy, xlsior, yaml, yate, yst
comments powered byDisqus