Python wierdness

Donn Cave donn at u.washington.edu
Wed Feb 7 17:15:21 EST 2001


Quoth "Roger H" <rhyde99 at email.msn.com>:
| 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:
...
|     >>> # 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.

It wouldn't be as obvious in Python 2.0.  Try this -

  >>> y = x % 0.1
  >>> '%.18f' % y
  '0.099999999999999978'

It isn't Python's fault, computers just don't have very good
heads for this sort of math.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list