# Exact real arithmetic

### From HaskellWiki

(Theory itemized) |
(inserted content from Hawiki) |
||

Line 38: | Line 38: | ||

=== [http://www.haskell.org/hawiki/ExactRealArithmetic ExactRealArithmetic] === |
=== [http://www.haskell.org/hawiki/ExactRealArithmetic ExactRealArithmetic] === |
||

This HaWiki article provides links to many implementations. |
This HaWiki article provides links to many implementations. |
||

+ | |||

+ | |||

+ | = Exact Real Arithmetic = |
||

+ | |||

+ | Exact real arithmetic refers to an implementation of the computable real numbers. |
||

+ | There are several implementations of exact real arithmetic in Haskell. |
||

+ | |||

+ | == BigFloat == |
||

+ | |||

+ | [http://medialab.freaknet.org/bignum/ BigFloat] is an implementation by Martin Guy. |
||

+ | It works with streams of decimal digits (strictly in the range from 0 to 9) and a separate sign. |
||

+ | The produced digits are always correct. |
||

+ | Output is postponed until the code is certain what the next digit is. |
||

+ | This sometimes means that [http://medialab.freaknet.org/bignum/dudeney.html no more data is output]. |
||

+ | |||

+ | == COMP == |
||

+ | |||

+ | COMP is an implementation by Yann Kieffer. The work is in beta, and the library isn't available yet. |
||

+ | |||

+ | == Era == |
||

+ | |||

+ | [http://www.cs.man.ac.uk/arch/dlester/exact.html Era] is an implementation (in Haskell 1.2) by David Lester. It is quite fast, possibly the fastest Haskell implementation. At 220 lines it is also the shortest. Probably the shortest implementation of exact real arithmetic in any language. |
||

+ | |||

+ | Here is a patch to get Era 1.0 to compile in Haskell 98. |
||

+ | |||

+ | {{{ |
||

+ | --- Era.hs 2005-10-26 12:16:05.835361616 +0200 |
||

+ | +++ Era.hs 2005-10-26 12:15:28.396053256 +0200 |
||

+ | @@ -8,6 +8,10 @@ |
||

+ | |||

+ | module Era where |
||

+ | |||

+ | +import Ratio |
||

+ | +import Char |
||

+ | +import Numeric (readDec, readSigned) |
||

+ | + |
||

+ | data CR = CR_ (Int -> Integer) |
||

+ | |||

+ | instance Eq CR where |
||

+ | @@ -179,8 +183,10 @@ |
||

+ | digits :: Int |
||

+ | digits = 40 |
||

+ | |||

+ | -instance Text CR where |
||

+ | +instance Read CR where |
||

+ | readsPrec p = readSigned readFloat |
||

+ | + |
||

+ | +instance Show CR where |
||

+ | showsPrec p x = let xs = get_str digits x in |
||

+ | if head xs == '-' then showParen (p > 6) (showString xs) |
||

+ | else showString xs |
||

+ | }}} |
||

+ | |||

+ | == Few Digits == |
||

+ | |||

+ | [http://r6.ca/ Few Digits] is an implementation by Russell O'Connor. This is a prototype of the implementation he intendeds to write in [http://coq.inria.fr/ Coq]. Once the Coq implementation is complete, the Haskell code could be extracted producing an implementation that would be proved correct. |
||

+ | |||

+ | == IC-Reals == |
||

+ | |||

+ | [http://www.doc.ic.ac.uk/~ae/exact-computation/#bm:implementations IC-Reals] is an implementation by Abbas Edalat, Marko Krznarć and Peter J. Potts. This implementation uses linear fractional transformations. |
||

+ | |||

+ | == NumericPrelude/Positional == |
||

+ | |||

+ | Represents a real number as pair {{{(exponent,[digit])}}}, where the digits are {{{Int}}}s in the open range {{{(-basis,basis)}}}. |
||

+ | There is no need for an extra sign item in the number data structure. |
||

+ | The {{{basis}}} can range from {{{10}}} to {{{1000}}}. |
||

+ | (Binary representations can be derived from the hexadecimal representation.) |
||

+ | Showing the numbers in traditional format (non-negative digits) |
||

+ | fails for fractions ending with a run of zeros. |
||

+ | However the internal representation with negative digits can always be shown |
||

+ | and is probably more useful for further processing. |
||

+ | An interface for the numeric type hierarchy of the NumericPrelude project is provided. |
||

+ | |||

+ | It features |
||

+ | * basis conversion |
||

+ | * basic arithmetic: addition, subtraction, multiplication, division |
||

+ | * algebraic arithmetic: square root, other roots (no general polynomial roots) |
||

+ | * transcendental arithmetic: pi, exponential, logarithm, trigonometric and inverse trigonometric functions |
||

+ | |||

+ | [http://darcs.haskell.org/numericprelude/src/Number/Positional.hs NumericPrelude: positional numbers] |
||

+ | |||

+ | |||

+ | http://darcs.augustsson.net/Darcs/CReal/ |
||

[[Category:theoretical foundations]] |
[[Category:theoretical foundations]] |

## Revision as of 11:55, 22 October 2006

## Contents |

## 1 Introduction

Exact real arithmetic is an interesting area: it is a deep connection between

- numeric methods
- and deep theoretic fondations of algorithms (and mathematics).

Its topic: computable real numbers raise a lot of interesting questions rooted in mathematical analysis, arithmetic, but also Computability theory (see numbers-as-programs approaches).

Computable reals can be achieved by many approaches -- it is not one single theory.

### 1.1 What it is *not*

Exact real arithmetic is not the same as *fixed* arbitrary precision reals (see `Precision(n)`

of Yacas).

Exact reals must allow us to run a huge series of computations, prescribing only the precision of the end result. Intermediate computations, and determining their necessary precision must be achieved automatically, dynamically.

Maybe another problem, but it was that lead me to think on exact real arithmetic: using some Mandelbrot-plotting programs, the number of iterations must be prescribed by the user at the beginning. And when we zoom too deep into these Mandelbrot worlds, it will become ragged or smooth. Maybe solving this particular problem does not need necessarily the concept of exact real arithmetic, but it was the first time I began to think on such problems.

See other numeric algorithms at Libraries and tools/Mathematics.

### 1.2 Why, are there reals at all, which are defined exactly, but are not computable?

See e.g. Chaitin's construction.

## 2 Theory

- Jean Vuillemin's Exact real computer arithmetic with continued fractions is very good article on the topic itself. It can serve also as a good introductory article, too, because it presents the connections to both mathematical analysis and Computability theory. It discusses several methods, and it describes some of them in more details.

- Martín Escardó's project A Calculator for Exact Real Number Computation -- its chosen functional language is Haskell, mainly because of its purity, lazyness, presence of lazy lists, pattern matching. Martín Escardó has many exact real arithetic materials also among his many papers.

- Jerzy Karczmarczuk's paper with the funny title The Most Unreliable Technique in the World to compute pi describes how to compute Pi as a lazy list of digits.

## 3 Portal-like homepages

### 3.1 Exact Computation

There are functional programming materials too, even with downloadable Haskell source.

### 3.2 ExactRealArithmetic

This HaWiki article provides links to many implementations.

# 4 Exact Real Arithmetic

Exact real arithmetic refers to an implementation of the computable real numbers. There are several implementations of exact real arithmetic in Haskell.

## 4.1 BigFloat

BigFloat is an implementation by Martin Guy. It works with streams of decimal digits (strictly in the range from 0 to 9) and a separate sign. The produced digits are always correct. Output is postponed until the code is certain what the next digit is. This sometimes means that no more data is output.

## 4.2 COMP

COMP is an implementation by Yann Kieffer. The work is in beta, and the library isn't available yet.

## 4.3 Era

Era is an implementation (in Haskell 1.2) by David Lester. It is quite fast, possibly the fastest Haskell implementation. At 220 lines it is also the shortest. Probably the shortest implementation of exact real arithmetic in any language.

Here is a patch to get Era 1.0 to compile in Haskell 98.

{{{ --- Era.hs 2005-10-26 12:16:05.835361616 +0200 +++ Era.hs 2005-10-26 12:15:28.396053256 +0200 @@ -8,6 +8,10 @@

module Era where

+import Ratio +import Char +import Numeric (readDec, readSigned) +

data CR = CR_ (Int -> Integer)

instance Eq CR where

@@ -179,8 +183,10 @@

digits :: Int digits = 40

-instance Text CR where +instance Read CR where

readsPrec p = readSigned readFloat

+ +instance Show CR where

showsPrec p x = let xs = get_str digits x in if head xs == '-' then showParen (p > 6) (showString xs) else showString xs

}}}

## 4.4 Few Digits

Few Digits is an implementation by Russell O'Connor. This is a prototype of the implementation he intendeds to write in Coq. Once the Coq implementation is complete, the Haskell code could be extracted producing an implementation that would be proved correct.

## 4.5 IC-Reals

IC-Reals is an implementation by Abbas Edalat, Marko Krznarć and Peter J. Potts. This implementation uses linear fractional transformations.

## 4.6 NumericPrelude/Positional

Represents a real number as pair {{{(exponent,[digit])}}}, where the digits are {{{Int}}}s in the open range {{{(-basis,basis)}}}. There is no need for an extra sign item in the number data structure. The {{{basis}}} can range from {{{10}}} to {{{1000}}}. (Binary representations can be derived from the hexadecimal representation.) Showing the numbers in traditional format (non-negative digits) fails for fractions ending with a run of zeros. However the internal representation with negative digits can always be shown and is probably more useful for further processing. An interface for the numeric type hierarchy of the NumericPrelude project is provided.

It features

* basis conversion * basic arithmetic: addition, subtraction, multiplication, division * algebraic arithmetic: square root, other roots (no general polynomial roots) * transcendental arithmetic: pi, exponential, logarithm, trigonometric and inverse trigonometric functions