aboutsummaryrefslogtreecommitdiff

1 Introduction

Edc is a dc-style RPN calculator program, distinct in its use of exact real arithmetic. It uses IC-Reals, a set of C routines developed at Imperial College that implement a system for performing exact (not arbitrary-precision) computations with real numbers (Edalat and Heckmann 2002). The distinction is subtle, but has significant consequences: try computing the 100th term of the sequence

a(0) = 11/2,

a(1)= 61/11,

a(n) = 111 - (1130 - 3000/a(n-2))/a(n-1)

in Emacs calc at precisions 5, 10, 30, 60, 100, 110, 120, 130, and 140.

;; Evaluate, and then: M-x calc RET p 5 RET 'hell(100) RET
;; etc.

(defmath hell (n)
  (let ((nm2 (/ :"11.0" :"2.0"))
	(nm1 (/ :"61.0" :"11.0"))
	(old 0))
    (for ((i 3 n))
	 (setq old nm1)
	 (setq nm1 (- 111
		      (/ (- 1130
			    (/ 3000
			       nm2))
			 nm1)))
	 (setq nm2 old))
    nm1))


You get the results

Precision Estimate
5 100.
10 100.
30 100.
60 99.9⁵⁷7)
100 100.0000000…
120 100.0³¹3…))
130 5.9⁷83
140 5.9⁷85…

This sequence provably converges to 6. And that should be scary; who would compute this to 130 digits of precision if the value for 60 were so near to a round number? Who would trust the result for 130 digits; how could you tell it was more correct? Maybe what caused this problem is worse with more digits. Not only do you need to worry about the truncation error of the sequence, but also the accumulated rounding error!

Exact real arithmetic avoids this problem via lazy precision. Numbers are viewed as streams of (signed) digits encoding chains of descending subsets, and when a computation returns, the result truly must be within the indicated subset.

The edc incantation


[0:1 0:2 111 1130 3000 0;2 / - 0;1 / - 0;1 r]0:s
11/2 61/11 0;sx0;sx0;sx0;sx0;sx

gives approximately 5.75; computing further terms yields a sequence that seems to monotonically approach 6. If you repeat 0;sx 100 times (to compute the 100th term), the result is 6 to within 2 in 10⁸.

2 Installation

Clone this repository, navigate to its root on the command line, and invoke make. This should build the edc program, as well as the IC-Reals library if necessary.

To make the edc program available on your PATH, move it to a directory on the path (often ~/bin for a per-user install, or /bin for a system-wide install). A system-wide install is not recommended, as the code's security is hardly well-proven.

The program and its bundled libraries compile with ISO C11, with the addition of strdup and fmemopen from the POSIX <string.h> and <stdio.h> headers.

3 Documentation

The command-line options and command language are documented in the manpage (doc/edc.1).

4 Running

The program, just like its counterpart, does not present any of the usual nice GNU readline shortcuts. Running it inside Emacs, through M-x comint-run, will recover that functionality.

5 Known Bugs and Desired Features

None right now.

6 Contributing

Report bugs to, ask questions of, and send patches to me at duncannwilkie@gmail.com.

7 Licensing

The original portions of this software are licensed under the GPLv3. This source distribution includes a copy of the IC-Reals library, which is under a bespoke license that grants permission to use, copy, modify, and distribute the software for non-commercial purposes. This source distribution also includes a copy of the GNU Multiple Precision library (necessary because IC-Reals uses the <gmp-impl.h> header not usually exposed on a system), the core of which is dual-licensed under the LGPLv3 and the GPLv2, and the example and test programs of which are licensed under the GPLv3.