Python -- floating point arithmetic

Ethan Furman ethan at stoneleaf.us
Wed Jul 7 13:32:50 EDT 2010


Nobody wrote:
> On Wed, 07 Jul 2010 15:08:07 +0200, Thomas Jollans wrote:
> 
>> you should never rely on a floating-point number to have exactly a
>> certain value.
> 
> "Never" is an overstatement. There are situations where you can rely
> upon a floating-point number having exactly a certain value.

It's not much of an overstatement.  How many areas are there where you 
need the number 
0.100000000000000005551115123125782702118158340454101562500000000000?

If I'm looking for 0.1, I will *never* (except by accident ;) say

if var == 0.1:

it'll either be <= or >=.

By contrast, if I'm dealing with integers I can say if var == 4 because 
I *know* that there are values that var can hold that are *exactly* 4. 
Not 3.999999999817263 or 4.0000000019726.

~Ethan~



More information about the Python-list mailing list