[Python-ideas] Fwd: Trigonometry in degrees
Chris Angelico
rosuav at gmail.com
Tue Jun 12 02:50:56 EDT 2018
On Tue, Jun 12, 2018 at 4:40 PM, Greg Ewing <greg.ewing at 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
More information about the Python-ideas
mailing list