[Tutor] If elif not working in comparison

Kent Johnson kent37 at tds.net
Tue Mar 29 13:16:29 CEST 2005


Brian van den Broek wrote:
> Sean Perry said unto the world upon 2005-03-29 03:48:
> 
>> Kent Johnson wrote:
>>
>>>> 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.
>>>
>>> Can you be more specific about what kinds of problems you have had?
>>>
>>
>> sure, what is 2/3? 0.66? 0.67?
> 
> 
>  >>> 2/3
> 0
>  >>> 2.0/3
> 0.66666666666666663

or even:
Python 2.4.1c2 (#64, Mar 17 2005, 11:11:23) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
  >>> 2/3
0
  >>> from __future__ import division
  >>> 2/3
0.66666666666666663
  >>> 2//3
0

There are definitely some gotchas you should be aware of when using floats. They are well-understood 
and fairly simple to work around. You have to understand the limitations. But to avoid floats 
entirely seems a bit extreme to me.

Kent



More information about the Tutor mailing list