[Tutor] If elif not working in comparison

Kent Johnson kent37 at tds.net
Tue Mar 29 03:21:59 CEST 2005


Sean Perry wrote:
> gerardo arnaez wrote:
> 
>> On Mon, 28 Mar 2005 09:27:11 -0500, orbitz <orbitz at drorbitz.ath.cx> 
>> wrote:
>>
>>> Floats are inherintly inprecise.  So if thigns arn't working like you
>>> expect don't be surprised if 0.15, 0.12, and 0.1 are closer to the same
>>> number than you think.
>>
>>
>>
>> Are you telling me that I cant expect 2 digit preceision?
> 
> 
> Not without using round. Have *NO* faith in floating points. This is 
> especially true when you are creating the decimals via division and the 
> like.

What?!?!

OK, floats don't necessarily have the exact values you expect (they may have errors after many 
decimal places), and comparing floats for equality is risky business (you should compare for close, 
not equal). But the errors are well past the two-digit place. See the FAQ for more info on one kind 
of error:
http://www.python.org/doc/faq/general.html#why-are-floating-point-calculations-so-inaccurate

Note that the original poster was looking at ranges, not looking for exact matches!

Can you be more specific about what kinds of problems you have had?

Kent



More information about the Tutor mailing list