Python wierdness
Roger H
rhyde99 at email.msn.com
Wed Feb 7 22:35:11 CET 2001
In my Python musings I came across a bit of seeming wierdness.
I threw some code at the Python 1.5.2 command line to demonstrate:
>>> # This function prints "bar" if the input
... # variable modulo 0.1 is equal to 0.1, and
... # "no bar" if it is not equal to 0.1:
...
>>> def foo(n):
... if n % 0.1 == 0.1:
... print "bar"
... else:
... print "no bar"
...
>>> # Here I create a variable x with the value 0.3:
...
>>> x = 0.3
>>>
>>> # Let's check whether x modulo 0.1 is equal to 0.1:
...
>>> x % 0.1
0.1
>>>
>>> # Obviously, x modulo 0.1 is indeed equal to 0.1.
... # If we pass it to foo(), we should see "bar", right?
...
>>> foo(x)
no bar
>>>
>>> # Hmmm... "no bar." So foo() doesn't agree that
... # x modulo 0.1 is equal to 0.1, even though the
... # command line shows that it is. Why not?
What's going on here, and how could one get around it?
~Roger the Shrubber~
More information about the Python-list
mailing list