123.3 + 0.1 is 123.3999999999 ?

Tony Meyer ta-meyer at ihug.co.nz
Thu May 15 18:36:10 EDT 2003


> Computers work with bits internally. A bit can be either 
> switched on or off. If one wants to express something like 
> the number 1 divided by 3 there's no way to perfectly 
> represent the outcome using something built out of 2-valued  
> bits.

This isn't strictly true, of course.  A third (or any other rational
number) can be represented by a pair of numbers (numerator, denominator)
like 1 and 3.  You can build up a rational data type that will do
arithmetic and so on if you like; I'm sure that there are reasonably
good implementations out there already.  I'm also pretty sure that
adding a rational data type to Python has been discussed here before.

Irrational numbers (PI, e, ...) are another story, of course; other
methods are needed to deal with those.

=Tony Meyer






More information about the Python-list mailing list