[Python-ideas] Fwd: Trigonometry in degrees
Stephan Houben
stephanh42 at gmail.com
Tue Jun 12 03:02:43 EDT 2018
Hi all,
I wrote a possible implementation of sindg:
https://gist.github.com/stephanh42/336d54a53b31104b97e46156c7deacdd
This code first reduces the angle to the [0,90] interval.
After doing so, it can be observed that the simple implementation
math.sin(math.radians(angle))
produces exact results for 0 and 90, and a result already rounded to
nearest for
60.
For 30 and 45, this simple implementation is one ulp too low.
So I special-case those to return the correct/correctly-rounded value
instead.
Note that this does not affect monotonicity around those values.
So I am still unsure if this belong in the stdlib, but if so, this is how
it could be done.
Stephan
2018-06-12 8:50 GMT+02:00 Chris Angelico <rosuav at gmail.com>:
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180612/088e7aa8/attachment-0001.html>
More information about the Python-ideas
mailing list