My first comment is that special casing values like this can lead to some very undesirable properties when you use the function for numerical analysis. Suddenly your sind is no longer continuous (sind(x) is no longer the limit of sind(x+d) as d goes to 0).
As I stated in my initial comment on this, if you are going to create a sind function with the idea that you want 'nice' angles to return 'exact' results, then what you need to do is have the degree based trig routines do the angle reduction in degrees, and only when you have a small enough angle, either use the radians version on the small angle or directly include an expansion in degrees. Angle reduction would be based on the identity that sin(x+y) = sin(x) * cos(y) + cos(x) * sin(y) and cos(x+y) = cos(x)*cos(y) - sin(x) * sin(y). If you want to find sin(z) for an arbitrary value z, you can reduce it to and x+y where x is some multiple of say 15 degrees, and y is in the range -7.5 to 7.5 degrees. You can have stored exact values of sin/cos of the 15 degree increments (and only really need them between 0 and 90) and then compute the sin and cos of the y value. On 6/13/18 6:07 AM, Stephan Houben wrote: > 2018-06-13 12:00 GMT+02:00 Robert Vanden Eynde <robertv...@gmail.com > <mailto:robertv...@gmail.com>>: > > What was wrong with my initial implementation with a lookup table > ? :D > > def sind(x): > if x % 90 == 0: > return (0, 1, 0, -1)[int(x // 90) % 4] > else: > return sin(radians(x)) > > > I kinda missed it, but now you ask: > > 1. It's better to reduce the angle while still in degrees since one of > the advantages > of degrees is that the reduction can be done exactly. Converting > very large angles > first to radians and then taking the sine can introduce a large error, > > 2. I used fmod instead of % on advice in this thread. > > 3. I also wanted to special case, 30, 45, and 60. > > > > If you want to support multiples of 30, you can do % 30 and // 30. > > > Sure, but I also wanted to special-case 45. > > Stephan > > > > Le mer. 13 juin 2018 à 09:51, Stephan Houben <stephan...@gmail.com > <mailto:stephan...@gmail.com>> a écrit : > > Op di 12 jun. 2018 12:41 schreef Nathaniel Smith > <n...@pobox.com <mailto:n...@pobox.com>>: > > On Tue, Jun 12, 2018, 00:03 Stephan Houben > <stephan...@gmail.com <mailto:stephan...@gmail.com>> wrote: > > Hi all, > > I wrote a possible implementation of sindg: > > > https://gist.github.com/stephanh42/336d54a53b31104b97e46156c7deacdd > > <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. > > > You observed this on your system, but math.sin uses the > platform libm, which might do different things on other > people's systems. > > > > Ok, I updated the code to treat all the values 0, 30, 45, 60 > and 90 specially. > > Stephan > > > > 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. > > > Again, monotonicity is preserved on your system, but it > might not be on others. It's not clear that this matters, > but then it's not clear that any of this matters... > > -n > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org <mailto:Python-ideas@python.org> > https://mail.python.org/mailman/listinfo/python-ideas > <https://mail.python.org/mailman/listinfo/python-ideas> > Code of Conduct: http://python.org/psf/codeofconduct/ > <http://python.org/psf/codeofconduct/> > > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org <mailto:Python-ideas@python.org> > https://mail.python.org/mailman/listinfo/python-ideas > <https://mail.python.org/mailman/listinfo/python-ideas> > Code of Conduct: http://python.org/psf/codeofconduct/ > <http://python.org/psf/codeofconduct/> > > > > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ -- Richard Damon _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/