This package was adapted from article:
This package provides a way to represent a rational number exactly with the use of factorial-base format.
To use Lisp:rational.el, insert in your
For good performance, be sure to byte-compile Lisp:rational.el, e.g.
M-x byte-compile-file <give the path to rational.el when prompted>
This will generate rational.elc, which will be loaded instead of Lisp:rational.el.
Lisp:rational.el was tested with GNU Emacs 20.4.1.
Please, read the article for mathematical proof and references.
As the article cited above explains (page 292):
x := 1 / n;
for i := 1 to 30 do
x := (n + 1) * x - 1;
f(x) = (n + 1)x - 1is invariant at the point
x = 1/n; that is,
f(x) = xfor
x = 1/n. On paper, then, you would expect the variable
xto remain unchanged after 30 interations of the loop. This is the case when I used factorial-base numbers. But the standard floating-point system failed badly and returned 286,331,161.6 instead of 0.33333 when
nwas set to 3.
n = 10because
1/10 = 0.1exactly in base 10. This is quite far from the truth:
xshould be equaled 0.1 but turned into 2.36378547759e21 after 30 loops. All the calculations are, of course, done in binary. The floating-point software finds the correct answer only when
Try to execute the test:
M-x rational-test RET
*** Rational Test ***
function: f(x) = (n + 1)x - 1 with x = 1/n and n = 3 (after 30 interactions of the loop)
Using rational package (factorial-base): 0 . 0 2 0 0 0 0 0 0 0 0 0 Using rational package (decimal-base) : 0.3333333333333333 Using floating-point : -21.0
The floating-point result may differ depending on which machine/environment you run, but the result will not be 0.333333.
‘rational-customize’ to customize rational options by typing:
M-x rational-customize RET
You can also bind
‘rational-customize’ to some key, like:
(global-set-key "\C-c\C-c" 'rational-customize)
There are the following predefined rational constants:
To create a rational number you can use
To check if an object is a rational factorial-base number, use
To set a rational number to zero or one, use
‘rational-one’ functions, respectively.
To set a rational number to any integer, use
To copy a rational, use
To compare two rationals, use
There are the following rational operations:
To translate a rational to another representation, use
‘rational-max-size’ specifies the maximum array storage for factorial-base number.