UP | HOME

Precise arithmetic

A programming language with precise arithmetic has all of the following:

The only language I know which satisfies all of these criteria is Perl 6. Scheme is almost there (some implementations probably do this).

Rationals, not floats

Any binary floating-point number system, like IEEE 754 floating-point, has serious problems with exactness. For instance, 0.1 + 0.2 ≠ 0.3 with IEEE 754.

Rationals are, at least potentially, not that much slower than floats. Most operations on rationals are just a few integer instructions, assuming you normalize (convert to lowest terms) lazily.

An alternative is decimal floats like those provided by Python’s decimal module. But these are slow because they involve a lot of bit-fiddling, and they can’t be used for bases other than base 10, whereas rationals are base-agnostic.