exact-real: Exact real arithmetic

[ library, math, mit ] [ Propose Tags ] [ Report a vulnerability ]

A type to represent exact real numbers using fast binary Cauchy sequences.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.2.0.0, 0.2.1.0, 0.3.0.0, 0.4.0.0, 0.5.0.0, 0.7.1.0, 0.8.0.0, 0.8.0.2, 0.9.0, 0.10.0, 0.11.0, 0.11.1, 0.11.2, 0.11.3, 0.12.0, 0.12.1, 0.12.2, 0.12.3, 0.12.4, 0.12.4.1, 0.12.5, 0.12.5.1
Change log changelog.md
Dependencies base (>=4.8 && <5), integer-gmp, random (>=1.0) [details]
License MIT
Copyright 2020 Ellie Hermaszewska
Author Ellie Hermaszewska
Maintainer Ellie Hermaszewska <keep.it.real@monoid.al>
Category Math
Home page https://github.com/expipiplus1/exact-real#readme
Bug tracker https://github.com/expipiplus1/exact-real/issues
Source repo head: git clone https://github.com/expipiplus1/exact-real
Uploaded by jophish at 2021-12-13T08:06:10Z
Distributions NixOS:0.12.5.1
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 12789 total (27 in the last 30 days)
Rating 2.25 (votes: 2) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for exact-real-0.12.5.1

[back to package description]

exact-real

Exact real arithmetic implemented by fast binary Cauchy sequences.

Motivating Example

Compare evaluating Euler's identity with a Float:

Note that you'll need the DataKinds extension turned on to evaluate the examples in this readme.

λ> let i = 0 :+ 1
λ> exp (i * pi) + 1 :: Complex Float
0.0 :+ (-8.742278e-8)

... and with a CReal:

λ> import Data.CReal
λ> let i = 0 :+ 1
λ> exp (i * pi) + 1 :: Complex (CReal 0)
0 :+ 0

Or:

λ> let f :: ∀ a. Fractional a => (a, a); f = iterate (\(x0, x1) -> let x2 = 111 - (1130-3000/x0) / x1 in (x1, x2)) (11/2, 61/11) !! 100
λ> f @Double
(100.0,100.0)
λ> f @(CReal 10)
(6.0000,6.0000)
λ> f @(CReal 50)
(5.9999999879253263,5.9999999899377725)

Implementation

CReal's phantom type parameter n :: Nat represents the precision at which values should be evaluated at when converting to a less precise representation. For instance the definition of x == y in the instance for Eq evaluates x - y at precision n and compares the resulting Integer to zero. I think that this is the most reasonable solution to the fact that lots of of operations (such as equality) are not computable on the reals but we want to pretend that they are for the sake of writing useful programs. Please see the Caveats section for more information.

The CReal type is an instance of Num, Fractional, Floating, Real, RealFrac, RealFloat, Eq, Ord, Show and Read. The only functions not implemented are a handful from RealFloat which assume the number is implemented with a mantissa and exponent.

There is a comprehensive test suite to test the properties of these classes.

The performance isn't terrible on most operations but it's obviously not nearly as speedy as performing the operations on Float or Double. The only two super slow functions are asinh and atanh at the moment.

Caveats

The implementation is not without its caveats however. The big gotcha is that although internally the CReal ns are represented exactly, whenever a value is extracted to another type such as a Rational or Float it is evaluated to within 2^-p of the true value.

For example when using the CReal 0 type (numbers within 1 of the true value) one can produce the following:

λ> 0.5 == (1 :: CReal 0)
True
λ> 0.5 * 2 == (1 :: CReal 0) * 2
False

Contributing

Contributions and bug reports are welcome!