# Precise arithmetic

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

- Practically unlimited range for integers by default (automatic degradation to bignums).
- Fractional numbers are rationals by default, not floats.
- Optionally, some kind of mechanism for dealing with irrational numbers precisely, like Simon Tatham’s spigot calculator.
- Optionally, complex numbers which are pairs of any of the above kinds of numbers.
- At the very least, a system of operator overloading so that the above optional features can be implemented.

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.