On Tue, Jun 12, 2018 at 4:40 PM, Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
> Tim Peters wrote:
>
>> 1. Python's float "%" is unsuitable for argument reduction; e.g.,
>>
>>  >>> -1e-14 % 360.0
>> 360.0
>>
>> `math.fmod` is suitable, because it's exact:
>>
>>  >>> math.fmod(-1e-14, 360.0)
>> -1e-14
>
>
> So why doesn't float % use math.fmod?

https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
https://docs.python.org/3/reference/expressions.html#id17
https://docs.python.org/3/reference/expressions.html#id18

(the latter two being footnotes from the section in the first link)

With real numbers, divmod (and thus the // and % operators) would
always return values such that:

div, mod = divmod(x, y):
1) div*y + mod == x
2) sign(mod) == sign(y)
3) 0 <= abs(mod) < abs(y)

But with floats, you can't guarantee all three of these. The divmod
function focuses on the first, guaranteeing the fundamental arithmetic
equality, but to do so, it sometimes has to bend the third one and
return mod==y.

There are times when it's better to sacrifice one than the other, and
there are other times when it's the other way around. We get the two
options.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to