Maintainer | Bas van Dijk <v.dijk.bas@gmail.com> |
---|---|
Safe Haskell | None |
Data.Scientific
provides a space efficient and arbitrary precision
scientific number type.
Scientific
numbers are represented using
scientific notation. It
uses an Integer
coefficient
c
and an Int
base10Exponent
e
(do
note that since we're using an Int
to represent the exponent these numbers
aren't truly arbitrary precision). 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 thegcd
of thenumerator
anddenominator
. Scientific numbers only need to be normalized, i.e.10000000
to1e7
. -
Scientific
is safe against numbers with huge exponents. For example:1e1000000000 ::
will fill up all space and crash your program. Scientific works as expected:Rational
> read "1e1000000000" :: Scientific 1.0e1000000000
- Also, the space usage of converting scientific numbers with huge exponents
to
(like:Integral
sInt
) or
(like:RealFloat
sDouble
orFloat
) will always be bounded by the target type.
This module is designed to be imported qualified:
import Data.Scientific as Scientific
- data Scientific
- scientific :: Integer -> Int -> Scientific
- coefficient :: Scientific -> Integer
- base10Exponent :: Scientific -> Int
- fromFloatDigits :: RealFloat a => a -> Scientific
- toRealFloat :: forall a. RealFloat a => Scientific -> a
- formatScientific :: FPFormat -> Maybe Int -> Scientific -> String
- data FPFormat
- toDecimalDigits :: Scientific -> ([Int], Int)
Documentation
data Scientific Source
An arbitrary-precision number represented using scientific notation.
This type describes the set of all
which have a finite
decimal expansion.
Real
s
A scientific number with coefficient
c
and base10Exponent
e
corresponds to the Fractional
number: fromInteger
c * 10 ^^
e
Eq Scientific | |
Fractional Scientific | WARNING: |
Data Scientific | |
Num Scientific | |
Ord Scientific | |
Read Scientific | |
Real Scientific | WARNING: Avoid applying |
RealFrac Scientific | |
Show Scientific | |
Typeable Scientific | |
NFData Scientific | |
Hashable Scientific |
Construction
scientific :: Integer -> Int -> ScientificSource
scientific c e
constructs a scientific number which corresponds
to the Fractional
number:
.
fromInteger
c * 10 ^^
e
Note that this function performs normalization, i.e. it divides out powers of
10 from c
and adds them to e
.
Projections
coefficient :: Scientific -> IntegerSource
The coefficient of a scientific number.
base10Exponent :: Scientific -> IntSource
The base-10 exponent of a scientific number.
Conversions
fromFloatDigits :: RealFloat a => a -> ScientificSource
Convert a RealFloat
(like a Double
or Float
) into a Scientific
number.
Note that this function uses floatToDigits
to compute the digits
and exponent of the RealFloat
number. Be aware that the algorithm used in
floatToDigits
doesn't work as expected for some numbers, e.g. as
the Double
1e23
is converted to 9.9999999999999991611392e22
, and that
value is shown as 9.999999999999999e22
rather than the shorter 1e23
; the
algorithm doesn't take the rounding direction for values exactly half-way
between two adjacent representable values into account, so if you have a
value with a short decimal representation exactly half-way between two
adjacent representable values, like 5^23*2^e
for e
close to 23, the
algorithm doesn't know in which direction the short decimal representation
would be rounded and computes more digits
toRealFloat :: forall a. RealFloat a => Scientific -> aSource
Convert a Scientific
number into a RealFloat
(like a Double
or a Float
).
Note that this function uses realToFrac
(
) internally but it guards against
computing huge Integer magnitudes (fromRational
. toRational
10^e
) that could fill up all
space and crash your program.
Always prefer toRealFloat
over realToFrac
when converting from
scientific numbers coming from an untrusted source.
Pretty printing
:: FPFormat | |
-> Maybe Int | Number of decimal places to render. |
-> Scientific | |
-> String |
Like show
but provides rendering options.
data FPFormat
Control the rendering of floating point numbers.
toDecimalDigits :: Scientific -> ([Int], Int)Source
Similar to floatToDigits
, toDecimalDigits
takes a
non-negative Scientific
number, and returns a list of digits and
a base-10 exponent. In particular, if x>=0
, and
toDecimalDigits x = ([d1,d2,...,dn], e)
then
n >= 1
x = 0.d1d2...dn * (10^^e)
0 <= di <= 9