Using fractions instead of floats

Ben Finney bignose+hates-spam at
Mon Oct 1 03:48:02 CEST 2007

andresj < at> writes:

> The problem arises when you try to represent some number, like 0.4 in
> a float.

Which is really a specific case of the general problem that, for any
given number base, some non-integer numbers cannot be exactly
represented as fractions.

> Secondly, what happens if I need to sum 1/3 and 0.4? I could use
> Decimal to represent 0.4 precisely, but what about 1/3?

What about the sum of π (pi) and √2 (sqrt(2))?

> So the solution I think is using a fraction type/class

As explained above, a fractional-number class only shifts the "exact
representation" problem, it doesn't solve it.

 \         "Facts do not cease to exist because they are ignored."  -- |
  `\                                                     Aldous Huxley |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list