|
|
|
|
|
by ThePadawan
1761 days ago
|
|
One of the things I recently learned that sounded the most "that can't possibly work well enough" is an optimization for sin(x): If abs(x) < 0.1, "sin(x)" is approximated really well by "x". That's it. For small x, just return x. (Obviously, there is some error involved, but for the speedup gained, it's a very good compromise) |
|
For |x| up to Pi/2 (90°) the one term approximation falls apart. The guarantee from the Taylor series is within 65% (in reality it does better, but is still off by 36%). Two terms is guaranteed to be within 8%, three within 0.5%, and four within 0.02%.
Here's a quick and dirty little Python program to compute a table of error bounds for a given x [3].
[1] x - x^3/3! + x^5/5! - x^7/7! + ...
[2] In reality the error will usually be quite a bit below this upper limit. The Taylor series for a given x is a convergent alternating series. That is, it is of the form A0 - A1 + A2 - A3 + ... where all the A's have the same sign, |Ak| is a decreasing sequence past some particular k, and |Ak| has a limit of 0 as k goes to infinity. Such a series converges to some value, and if you approximate that by just taking the first N terms the error cannot exceed the first omitted term as long as N is large enough to take you to the point where the sequence from there on is decreasing. This is the upper bound I'm using above.
[3] https://pastebin.com/thN8B7Gf