exact-real-0.12.2: Exact real arithmetic

Safe HaskellNone
LanguageHaskell2010

Data.CReal

Description

This module exports everything you need to use exact real numbers

Synopsis

Documentation

data CReal n Source

The type CReal represents a fast binary Cauchy sequence. This is a Cauchy sequence with the invariant that the pth element divided by 2^p will be within 2^-p of the true value. Internally this sequence is represented as a function from Ints to Integers.

Instances

KnownNat n => Eq (CReal n) Source

Values of type CReal p are compared for equality at precision p. This may cause values which differ by less than 2^-p to compare as equal.

>>> 0 == (0.1 :: CReal 1)
True
Floating (CReal n) Source 
Fractional (CReal n) Source

Taking the reciprocal of zero will not terminate

Num (CReal n) Source

signum (x :: CReal p) returns the sign of x at precision p. It's important to remember that this may not represent the actual sign of x if the distance between x and zero is less than 2^-p.

This is a little bit of a fudge, but it's probably better than failing to terminate when trying to find the sign of zero. The class still respects the abs-signum law though.

>>> signum (0.1 :: CReal 2)
0.0
>>> signum (0.1 :: CReal 3)
1.0
KnownNat n => Ord (CReal n) Source

Like equality values of type CReal p are compared at precision p.

KnownNat n => Read (CReal n) Source

The instance of Read will read an optionally signed number expressed in decimal scientific notation

KnownNat n => Real (CReal n) Source

toRational returns the CReal n evaluated at a precision of 2^-n

KnownNat n => RealFloat (CReal n) Source

Several of the functions in this class (floatDigits, floatRange, exponent, significand) only make sense for floats represented by a mantissa and exponent. These are bound to error.

atan2 y x atPrecision p performs the comparison to determine the quadrant at precision p. This can cause atan2 to be slightly slower than atan

KnownNat n => RealFrac (CReal n) Source 
KnownNat n => Show (CReal n) Source

A CReal with precision p is shown as a decimal number d such that d is within 2^-p of the true value.

>>> show (47176870 :: CReal 0)
"47176870"
>>> show (pi :: CReal 230)
"3.1415926535897932384626433832795028841971693993751058209749445923078164"
KnownNat n => Random (CReal n) Source

The Random instance for 'CReal' p will return random number with at least p digits of precision, every digit after that is zero.

Converge [CReal n] Source

The overlapping instance for CReal n has a slightly different behavior. The instance for Eq will cause converge to return a value when the list converges to within 2^-n (due to the Eq instance for CReal n) despite the precision the value is requested at by the surrounding computation. This instance will return a value approximated to the correct precision.

It's important to note when the error function reaches zero this function behaves like converge as it's not possible to determine the precision at which the error function should be evaluated at.

Find where log x = π using Newton's method

>>> let initialGuess = 1
>>> let improve x = x - x * (log x - pi)
>>> let Just y = converge (iterate improve initialGuess)
>>> showAtPrecision 10 y
"23.1406"
>>> showAtPrecision 50 y
"23.1406926327792686"
type Element [CReal n] = CReal n Source 

atPrecision :: CReal n -> Int -> Integer Source

x `atPrecision` p returns the numerator of the pth element in the Cauchy sequence represented by x. The denominator is 2^p.

>>> 10 `atPrecision` 10
10240

crealPrecision :: KnownNat n => CReal n -> Int Source

crealPrecision x returns the type level parameter representing x's default precision.

>>> crealPrecision (1 :: CReal 10)
10