bug in modulus?
Christophe
chris.cavalaria at free.fr
Thu Apr 27 05:42:47 EDT 2006
jantod at gmail.com a écrit :
> I think there might be something wrong with the implementation of
> modulus.
>
> Negative float values close to 0.0 break the identity "0 <= abs(a % b)
> < abs(b)".
>
> print 0.0 % 2.0 # => 0.0
> print -1e-010 % 2.0 # =>1.9999999999
>
> which is correct, but:
>
> print -1e-050 % 2.0 # => 2.0
> print -1e-050 % 2.0 < 2.0 # => False
>
> This means I can't use modulus to "wrap around" before it reaches a
> certain value. I'm using Python 2.4.2 on WindowsXP.
>
> Thanks
> Janto
>
Consider that -1e-050 % 2.0 = 2.0 - 1e-050
Now, test that :
>>> 2.0 - 1e-050 == 2.0
True
Floating point numbers just don't have the required precision to
represent 2.0 - 1e-050. For your specific problem, if you really want
the result to be < 2.0, the the best you can do is admit that floating
point operations have errors and return 0.0 when the modulus operation
equals 2.0.
More information about the Python-list
mailing list