# Float compression [Re: floating point math results question]

Jonathan Hogg jonathan at onegoodidea.com
Sun Jan 27 08:27:02 EST 2002

```On 26/1/2002 21:07, in article
mailman.1012079405.13099.python-list at python.org, "François Pinard"
<pinard at iro.umontreal.ca> wrote:

> I guess that it is an interesting exercise, trying to represent an arbitrary
> float number within a given tolerance, with a small number of bits.
>
> Let's take math.pi, say.  It can be represented by 355:113 if the tolerance
> is over 0.00000027.  Maybe we could find efficient binary representations
> for 355 followed by 113, and this probably turns out into finding efficient
> ways for coding the bit length of each.

Interesting perhaps, but who needs a space-efficient representation anymore?
Given that memory is cheap and non-aligned/bitwise operations are expensive.

What we need is a 'rational' type, in a similar vein to the existing
'complex' type:

n = rational( 8, 10 )

A more interesting problem is an efficient algorithm for refactoring an
arbitrary rational number into a sequence of the form:

... + 100.k + 10.k + k + k[-1]/10 + k[-2]/100 + ...

where 0 <= k[i] < 10, which you would want for printing out the
representation as a normal floating point number. [Of course noting that
many rationals are non-terminating as floats.]

Does NumPy or something similar already have rational types? I don't do
numerical stuff in Python so I have no idea if this is a solved problem with