aern2-real: Real numbers as sequences of MPBalls

[ bsd3, library, math ] [ Propose Tags ]
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.1.0, 0.1.2, 0.2.0.0, 0.2.1.0, 0.2.4.0, 0.2.4.1, 0.2.4.2, 0.2.4.3, 0.2.5.0, 0.2.6.0, 0.2.7.0, 0.2.8.0, 0.2.9.0, 0.2.9.1, 0.2.9.2, 0.2.10.0, 0.2.11.0, 0.2.12.0, 0.2.13.0, 0.2.14, 0.2.14.1, 0.2.15
Change log changelog.md
Dependencies aern2-mp (>=0.2), base (>=4 && <5), collect-errors (>=0.1), hspec, integer-logarithms, mixed-types-num (>=0.5.1), QuickCheck [details]
License BSD-3-Clause
Copyright 2015-2021 Michal Konecny
Author Michal Konecny
Maintainer mikkonecny@gmail.com
Category Math
Home page https://github.com/michalkonecny/aern2#readme
Bug tracker https://github.com/michalkonecny/aern2/issues
Source repo head: git clone https://github.com/michalkonecny/aern2
Uploaded by MichalKonecny at 2021-05-17T13:01:13Z
Distributions LTSHaskell:0.2.15, NixOS:0.2.15, Stackage:0.2.15
Reverse Dependencies 1 direct, 3 indirect [details]
Downloads 6769 total (76 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-05-17 [all 1 reports]

Readme for aern2-real-0.2.0.0

[back to package description]

aern2-real

Exact real arithmetic

Numeric data types

This package provides the following two data types:

  • CReal: Exact real numbers via lazy sequences of interval approximations

  • CKleenean: Lazy Kleeneans, naturally arising from comparisons of CReals

The type CReal has instances of both mixed-types-num type classes such as CanAdd, CanSqrt as well as with traditional Prelude type classes such as Ord, Num and Floating. The type CKleenean supports the usual Boolean operations.

Examples

First, let us test exact real arithmetic with Prelude operations:

$ stack ghci aern2-real:lib --no-load --ghci-options AERN2.Real
*AERN2.MP> import Prelude hiding (pi)
*AERN2.MP Prelude>

...> pi
{?(prec 36): [3.141592653584666550159454345703125 ± ~1.4552e-11 ~2^(-36)]}

...> pi ? (bits 1000)
[3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117... ± ~0.0000 ~2^(-1230)]

...> pi ? (bits 1000000)
[3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117... ± ~0.0000 ~2^(-1028468)]
(4.12 secs, 270,972,152 bytes)

...> pi ^ 2
[9.8696044010893586188344909998725639610631902560... ± ~8.1120e-30 ~2^(-96)]
{?(prec 36): [9.8696044009993784129619598388671875 ± ~1.4964e-10 ~2^(-32)]}

...> pi ^ pi
<interactive>:18:1: error:
    • No instance for (Integral CReal) arising from a use of ‘^’

...> sin pi
{?(prec 36): [0.000000000005126565838509122841060161590576171875 ± ~1.4559e-11 ~2^(-35)]}

...> (sin pi) ? (bits 10000) -- guaranteed accuracy at least 10000
[-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000... ± ~0.0000 ~2^(-13539)]
(0.21 secs, 196,580,192 bytes)

...> (sin pi) ? (prec 10000) -- no guaranteed accuracy
[-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000... ± ~0.0000 ~2^(-13539)]
(0.13 secs, 107,844,784 bytes)

...> pi > 0
True

...> pi == pi
*** Exception: Failed to decide equality of Sequences.  If you switch to MixedTypesNumPrelude instead of Prelude, comparison of Sequences returns CSequence Kleenean or similar instead of Bool.

Some things do not work with Prelude. Let us try using MixedTypesNumPrelude operations:

$ stack ghci aern2-real:lib --no-load --ghci-options AERN2.Real
*AERN2.MP> import MixedTypesNumPrelude
*AERN2.MP MixedTypesNumPrelude>

...> pi ^ pi
{?(prec 36): [36.462159605538498632520418490483602438877178488347481362195878876490337527904728176508797332644462... ± ~2.7112e-9 ~2^(-28)]}

...> (pi ^ pi) ? (bits 10000)
[36.462159607207911770990826022692123666365508402228818738709335922934074368881699904620079875706774... ± ~0.0000 ~2^(-13532)]
(0.81 secs, 631,865,912 bytes)

...> pi > 0
{?(prec 36): CertainTrue}

...> pi == pi
{?(prec 36): TrueOrFalse}

...> pi == pi + 2^(-100)
{?(prec 36): TrueOrFalse}

...> (pi == pi + 2^(-100)) ? (prec 1000)
CertainFalse

...> 2^0.5
{?(prec 36): [1.414213562371930730340852514178195642186126256312482171419747717302107387071785637999710161238908... ± ~1.0305e-11 ~2^(-36)]}

Partial functions and error handling

Errors due to invalid input, such as division by zero or logarithm of a negative number can be only semi-detected in the same way as comparisons can be only semi-decided. Therefore, an invalid input gives a CReal leads to errors or potential errors only when extracting an approximation:

...> bad1 = pi/0
...> bad1 ? (prec 100)
{{ERROR: division by 0}}}

...> bad2 = 1/(pi-pi)
...> bad2 ? (prec 100)
{{POTENTIAL ERROR: division by 0}}

When we are sure that potential errors are harmless, we can clear them:

...> ok3 = sqrt (pi-pi)
...> ok3 ? (prec 10)
[0.022097086912079610143710452219156792352805496193468570709228515625 ± ~2.2097e-2 ~2^(-5)]{{POTENTIAL ERROR: out of domain: negative sqrt argument}}
...> clearPotentialErrors $ ok3 ? (prec 10)
[0.022097086912079610143710452219156792352805496193468570709228515625 ± ~2.2097e-2 ~2^(-5)]

Specification and tests

The approximations obtained using ? (bits n) or ? (prec p) are intervals of type CN MPBall from package aern2-mp. This type is also used internally for all CReal arithmetic. The MPBall arithmetic is tested against a fairly complete hspec/QuickCheck specification of algebraic properties.