HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulenumeric-prelude-0.4.4Haskell98

Number.Positional

Exact Real Arithmetic - Computable reals. Inspired by ''The most unreliable technique for computing pi.'' See also http://www.haskell.org/haskellwiki/Exact_real_arithmetic .

  • 7 types
  • 121 values

types

6 declarations

basic helpers

18 declarations
valuenonNegative :: Basis -> T -> T
#

Converts all digits to non-negative digits, that is the usual positional representation. However the conversion will fail when the remaining digits are all zero. (This cannot be improved!)

valuetrim :: T -> T
#

Eliminate leading zero digits. This will fail for zero.

valuedecreaseExp :: Basis -> T -> T
#

Accept a high leading digit for the sake of a reduced exponent. This eliminates one leading digit. Like pumpFirst but with exponent management.

valuenegativeExp :: Basis -> T -> T
#

Make sure that a number with absolute value less than 1 has a (small) negative exponent. Also works with zero because it chooses an heuristic exponent for stopping.

conversions

0 declarations

integer

rational

fixed point

floating point

valuefromDoubleApprox :: Basis -> Double -> T
#

Only return as much digits as are contained in Double. This will speedup further computations.

text

basis

valuepowerBasis :: Basis -> Exponent -> T -> T
#

Convert from a b basis representation to a b^e basis.

Works well with every exponent.

valuerootBasis :: Basis -> Exponent -> T -> T
#

Convert from a b^e basis representation to a b basis.

Works well with every exponent.

valuefromBasis :: Basis -> Basis -> T -> T
#

Convert between arbitrary bases. This conversion is expensive (quadratic time).

comparison

11 declarations
valuecmp :: Basis -> T -> T -> Ordering
#

The basis must be at least ***. Note: Equality cannot be asserted in finite time on infinite precise numbers. If you want to assert, that a number is below a certain threshold, you should not call this routine directly, because it will fail on equality. Better round the numbers before comparison.

valueifLazy :: Basis -> Bool -> T -> T -> T
#

If all values are completely defined, then it holds

if b then x else y == ifLazy b x y

However if b is undefined, then it is at least known that the result is between x and y.

valuemean2 :: Basis -> (Digit, Digit) -> (Digit, Digit) -> Digit
#
mean2 b (x0,x1) (y0,y1)

computes round ((x0.x1 + y0.y1)/2) , where x0.x1 and y0.y1 are positional rational numbers with respect to basis b

valuealignMant :: Basis -> Exponent -> T -> Mantissa
#

Get the mantissa in such a form that it fits an expected exponent.

x and (e, alignMant b e x) represent the same number.

arithmetic

27 declarations
valueadd :: Basis -> T -> T -> T
#

Add two numbers but do not eliminate leading zeros.

valueaddSome :: Basis -> [T] -> T
#

Add at most basis summands. More summands will violate the allowed digit range.

valueaddMany :: Basis -> [T] -> T
#

Add many numbers efficiently by computing sums of sub lists with only little carry propagation.

valueseries :: Basis -> Series -> T
#

Add an infinite number of numbers. You must provide a list of estimate of the current remainders. The estimates must be given as exponents of the remainder. If such an exponent is too small, the summation will be aborted. If exponents are too big, computation will become inefficient.

valuemul :: Basis -> T -> T -> T
#

For obtaining n result digits it is mathematically sufficient to know the first (n+1) digits of the operands. However this implementation needs (n+2) digits, because of calls to compress in both scale and series. We should fix that.

valuedivide :: Basis -> T -> T -> T
#

Undefined if the divisor is zero - of course. Because it is impossible to assert that a real is zero, the routine will not throw an error in general.

ToDo: Rigorously derive the minimal required magnitude of the leading divisor digit.

algebraic functions

7 declarations
valuesqrtFP :: Basis -> FixedPoint -> Mantissa
#

Square root.

We need a leading digit of type Integer, because we have to collect up to 4 digits. This presentation can also be considered as FixedPoint.

ToDo: Rigorously derive the minimal required magnitude of the leading digit of the root.

Mathematically the nth digit of the square root depends roughly only on the first n digits of the input. This is because sqrt (1+eps) equalApprox 1 + eps/2. However this implementation requires 2*n input digits for emitting n digits. This is due to the repeated use of compressMant. It would suffice to fully compress only every basisth iteration (digit) and compress only the second leading digit in each iteration.

Can the involved operations be made lazy enough to solve y = (x+frac)^2 by frac = (y-x^2-frac^2) / (2*x) ?

Newton iteration doubles the number of correct digits in every step. Thus we process the data in chunks of sizes of powers of two. This way we get fastest computation possible with Newton but also more dependencies on input than necessary. The question arises whether this implementation still fits the needs of computational reals. The input is requested as larger and larger chunks, and the input itself might be computed this way, e.g. a repeated square root. Requesting one digit too much, requires the double amount of work for the input computation, which in turn multiplies time consumption by a factor of four, and so on.

Optimal fast implementation of one routine does not preserve fast computation of composed computations.

The routine assumes, that the integer parts is at least b^2.

valuelazyInits :: [a] -> [[a]]
#

List.inits is defined by inits = foldr (x ys -> [] : map (x:) ys) [[]]

This is too strict for our application.

Prelude> List.inits (0:1:2:undefined)
[[],[0],[0,1]*** Exception: Prelude.undefined

The following routine is more lazy than inits and even lazier than inits from utility-ht package, but it is restricted to infinite lists. This degree of laziness is needed for sqrtFP.

Prelude> lazyInits (0:1:2:undefined)
[[],[0],[0,1],[0,1,2],[0,1,2,*** Exception: Prelude.undefined

transcendent functions

0 declarations

exponential functions

valueexpSmall :: Basis -> T -> T
#

Absolute value of argument should be below 1.

valuepowerSeries :: Basis -> Rational -> T -> Series
#

Residue estimates will only hold for exponents with absolute value below one.

The computation is based on Int, thus the denominator should not be too big. (Say, at most 1000 for 1000000 digits.)

It is not optimal to split the power into pure root and pure power (that means, with integer exponents). The root series can nicely handle all exponents, but for exponents above 1 the series summands rises at the beginning and thus make the residue estimate complicated. For powers with integer exponents the root series turns into the binomial formula, which is just a complicated way to compute a power which can also be determined by simple multiplication.

valuecosSinFourth :: Basis -> T -> (T, T)
#

Like cosSinSmall but converges faster. It calls cosSinSmall with reduced arguments using the trigonometric identities cos (4*x) = 8 * cos x ^ 2 * (cos x ^ 2 - 1) + 1 sin (4*x) = 4 * sin x * cos x * (1 - 2 * sin x ^ 2)

Note that the faster convergence is hidden by the overhead.

The same could be achieved with a fourth power of a complex number.

logarithmic functions

valuelnNewton :: Basis -> T -> T
#
x' = x - (exp x - y) / exp x
   = x + (y * exp (-x) - 1)

First, the dependencies on low-significant places are currently much more than mathematically necessary. Check *Number.Positional> expSmall 1000 (-1,100 : replicate 16 0 ++ [undefined]) (0,[1,105,171,-82,76*** Exception: Prelude.undefined Every multiplication cut off two trailing digits. *Number.Positional> nest 8 (mul 1000 (-1,repeat 1)) (-1,100 : replicate 16 0 ++ [undefined]) (-9,[101,*** Exception: Prelude.undefined

Possibly the dependencies of expSmall could be resolved by not computing mul immediately but computing mul series which are merged and subsequently added. But this would lead to an explosion of series.

Second, even if the dependencies of all atomic operations are reduced to a minimum, the mathematical dependencies of the whole iteration function are less than the sums of the parts. Lets demonstrate this with the square root iteration. It is (1.4140 + 21.4140) 2 == 1.414213578500707 (1.4149 + 21.4149) 2 == 1.4142137288854335 That is, the digits 213 do not depend mathematically on x of 1.414x, but their computation depends. Maybe there is a glorious trick to reduce the computational dependencies to the mathematical ones.

valueangle :: Basis -> (T, T) -> T
#

This is an inverse of cosSin, also known as atan2 with flipped arguments. It's very slow because of the computation of sinus and cosinus. However, because it uses the atan2 implementation as estimator, the final application of arctan series should converge rapidly.

It could be certainly accelerated by not using cosSin and its fiddling with pi. Instead we could analyse quadrants before calling atan2, then calling cosSinSmall immediately.

valuearctanStem :: Basis -> Digit -> T
#

Efficient computation of Arcus tangens of an argument of the form 1/n.

valuearctan :: Basis -> T -> T
#

This implementation gets the first decimal place for free by calling the arcus tangens implementation for Doubles.

valuearctanClassic :: Basis -> T -> T
#

A classic implementation without 'cheating' with floating point implementations.

For x < 1 / sqrt 3 (1 / sqrt 3 == tan (pi/6)) use arctan power series. (sqrt 3 is approximately 19/11.)

For x > sqrt 3 use arctan x = pi/2 - arctan (1/x)

For other x use

arctan x = pi/4 - 0.5*arctan ((1-x^2)/2*x) (which follows from arctan x + arctan y == arctan ((x+y) / (1-x*y)) which in turn follows from complex multiplication (1:+x)*(1:+y) == ((1-x*y):+(x+y))

If x is close to sqrt 3 or 1 / sqrt 3 the computation is quite inefficient.

constants

0 declarations

elementary

transcendental

auxilary functions

1 declaration