Python wierdness
Michael Hudson
mwh21 at cam.ac.uk
Wed Feb 7 16:44:34 EST 2001
"Roger H" <rhyde99 at email.msn.com> writes:
[snip]
> >>> 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?
[snip]
> What's going on here, and how could one get around it?
Floating point is going on here. Python 2.0 is more helpful in this
regard:
>>> 0.3 % 0.1
0.099999999999999978
>>> 0.1
0.10000000000000001
>>> 0.3 % 0.1 - 0.1
-2.7755575615628914e-17
Comparing floats for equality is always fraught. Enjoy :-) (I may be
a mathematician, but you aren't going to catch me doing numerical
analysis...)
Cheers,
M.
--
I'd certainly be shocked to discover a consensus. ;-)
-- Aahz Maruch, comp.lang.python
More information about the Python-list
mailing list