simple math don't work like I expected

Mike C. Fletcher mcfletch at rogers.com
Wed Jan 29 16:59:32 CET 2003


What you're seeing when you use str on the calculated float is a rounded 
value, try using repr instead...

 >>> a=1.99
 >>> while a < 2.01:
...     if (a-1)<1:
...         print(repr(a-1)+'<1')
...     a=a+0.005
...
0.98999999999999999<1
0.99499999999999988<1
0.99999999999999978<1
 >>> repr( 0.005)
'0.0050000000000000001'

Enjoy,
Mike


Elvis Zaichenok wrote:

>Please, could somebody explain why things work like this? Why 1.0<1?
>
>
>Python 2.2 (#1, Apr 12 2002, 15:29:57)
>[GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-109)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>  
>
>>>>a=1.99
>>>>while a<2.01:
>>>>        
>>>>
>...     if (a-1)<1:
>...         print(str(a-1)+'<1')
>...     a=a+0.005
>...
>0.99<1
>0.995<1
>1.0<1
>
>
>Thanks.
>  
>

-- 
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list