4 ms·
This a perfectly nice Programming 101 example of a recursive function -- if an angle is too large, calculate its sine using the sine of the half-angle, otherwis
by jhgb 5y ago
This a perfectly nice Programming 101 example of a recursive function -- if an angle is too large, calculate its sine using the sine of the half-angle, otherwise return the angle (or, if you're fancy, some simple polynomial approximation of the sine). I'm sure everyone did this in school (we did, in fact).
- gugagore 5y agoIt's not obvious when you should switch over to an approximation (base case), so I'd say it's not a good example to introduce recursion. I have never seen it, and I did not do it in school.
- aaaronic 5y agoWhen the difference between iterations is less than whatever threshold of accuracy you're looking for, I'd assume. "Relaxation" algorithms for calculating fields work that way.
- jhgb 5y agoIt's not obvious but that makes it a nice example in parameter optimization as well.
- scythe 5y agoIt's "obvious" if you remember how the Taylor series for sin(x) works. The error between x and sin(x) is bounded above by x^3/6. So there you go.
- SnowHill9902 5y agoFor a physicist sin(x) ~= x if x^3/6 << x
- eyelidlessness 5y agoI didn’t do this (or any programming, or any trigonometry) in school, but I’m appreciative of tidbits like this from people who did. I definitely feel comfortable with recursion, and have a stalled art project which could benefit from better understanding how to apply trig as I accumulate understanding in a much less structured way.