Rational numbers

Posted: January 28th, 2012 | Author: Mars | Filed under: Uncategorized | Comments Off

Scientific computing seems like a reasonable class of uses for a tool like Radian, but an integer-only number system will only take us so far. I’ve just finished adding on a rational number system which handles arithmetic on fractional numbers. Dividing one integer by another no longer discards the remainder; if the denominator is not a factor of the numerator, the result is a fractional number instead of an integer.

“Rational” sits below “integer” on the numeric tower; that is, all integers are rationals, but not all rationals are integers. The type system works as follows:

Number has number?, rational?, integer?

Rational extends number with add, subtract, multiply, divide, modulus, exponentiate, numerator, denominator, and compare_to

Integer extends rational with shift_left, shift_right, bit_and, bit_or, bit_xor, and to_char

The next step for numerics will be the addition of an arbitrary-precision “bigint” implementation. Integers are currently limited to the range of a machine word, which is either 32 or 64 bits, and they offer no provision for overflow; the new system will detect overflow and shift up to a larger storage format. If this works out well it should be inexpensive for word-sized integers but allow seamless scaling up to thousands of bits of precision.


Comments are closed.