2011-11-16 : Some collected thoughts on creating a Rational numeric type.

In finance, we are always dealing with quantities of money, which usually have some number of digits after the decimal point, usually two or four. It also used to be common to see prices given in eighths. Unfortunately, rather than using a representation that can represent the numbers with complete accuracy, standard floating point numbers are usually used. This often leads to rounding issues.

Now, I'm not one of those people who is paranoid about floating point numbers and believes that every comparison also needs to include an accuracy epsilon. However, I do understand that standard floating point are numbers stored as fractions with a denomitor that is 2n. Therefore, even a simple number like 0.1 (1/10) must be stored as an approximation (838861/8388608, which has been rounded up by 0.2/8388608).

It would be nice if we could represent simple useful numbers lime 1/10, 1/8, and even 1/3 without rounding. These are all rational numbers.

Standard IEEE 754 encoding of floating point numbers basically uses bit fields within a standard 32-bit int / 64-bit long / etc. They encode the numerator (mantissa or more properly significand) and denominator (derived from the exponent) for a base 2 fraction in a single integer. It would be interesting to come up with an encoding that allowed arbitrary fractions. Obviously, there would be some trade-offs compared to floating point, but how good could you make it?

Observations about a Rational Format

(Hand written notes: 2008-04-22 2008-04-24)

Here are some features that would be nice to have:

Some bit patterns are redundant, e.g., 2/4 == 1/2, 3/6 == 1/3.

Encoding the Denominator

I've looked how to encode the denominator using a variable number of bits. (See 2008-04-22/pg2.) One problem with this approach is that supporting a large number of "number of bits" itself consumes valuable bits. In the worst case, you are reduced to base 1 encoding. To have enough bits to actually encode a large denominator, you have to pick a small number of denominator bit-widths. That leads to the question of which widths are most useful. Bit-widths that can encode "even" powers of ten are definitely useful, since many calculations are performed on decimals.

n 2n floor(log10(2n))
4 16 1
7 128 2
10 1024 3
14 16,384 4
17 131,072 5
20 1,048,576 6
24 16,777,216 7
27 134,217,728 8
30 1,073,741,784 9

Some interesting combinations for 32-bit ints are as follows. Here, the first group is the number of bits in the denominators. The second group shows in more detail the number of bits in the numerator, the denominator, and the control bits.

Having a 0-deniminator-bit mode basically allows you to reserve half your bit patterns for representing large integers. Initially, I thought this was important, but I currently think this is not necessary: you are unlikely to use a rational unless there is a high probability that the denominator will be greater than one.

For 64-bit longs, consider these patterns:

The 7/14/30 is interesting because the numerators are all multiples of 8 bits.

The 10/20 for ints and the 10/20/30/40 for longs are interesting because, for the largest denominator, the numerator is smaller than the denominator. I'm not sure if this is useful, since while it allows you to represent smaller magnitude fractions (large denominator), the number of bits of precision goes down (small numerator).

It would be useful to have a denominator encoding that's the same for ints and longs, since you can then just cast from int to long and get the same value with greater precision (just like with regular integers).

The two most favorable candidates are

Since it seems to makes sense to favor numerator bits over denominator bits, then 7/14/30 is the winner.
The Result

Source: https://latenighthacking.com:8443/svn/code_root/2011/Rational

C o m m e n t s :    
(nothing yet)