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)
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)
N Error Limit
1 0.167% (1/6%)
2 8.35 x 10^-5% (1/11980%)
3 1.99 x 10^-8% (1/50316042%)
4 2.76 x 10^-12% (1/362275502328%)
Even for |x| as large as 1 the sin(x) = x approximation is within 20%, which is not too bad when you are just doing a rough ballpark calculation, and a two term approximation gets you under 1%. Three terms is under 0.024% (1/43%).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.
The sin(x) = x approximation is actually exact (in terms of doubles) for |x| < 2^-26 = 1.4e-8. See also [1]. This happens to cover 48.6% of all doubles.
Similarly, cos(x) = 1 for |x| < 2^-27 = 7.45e-9 (see [2]).
Finally, sin(double(pi)) tells you the error in double(pi) - that is, how far the double representation of pi is away from the "real", mathematical pi [3].
[0]: https://randomascii.wordpress.com/2014/10/09/intel-underesti...
[1]: https://github.com/lattera/glibc/blob/master/sysdeps/ieee754...
[2]: https://github.com/lattera/glibc/blob/master/sysdeps/ieee754...
[3]: https://randomascii.wordpress.com/2012/02/25/comparing-float...
What is more interesting to me is that this can be one of rationales behind using radians.
And that tan(x)~x also holds for small angles, greatly easing estimations of distance to objects of known size (cf. mil-dot reticle).
Whether it's appropriate in a numerical calculation obviously depends on the possible inputs and the acceptable error bars :)
That the sin(x) approximation still works well for 10^-1 (with an error of ~0.01%) is the really cool thing!
What I really found amazing was that rather than reducing the number of multiplications to 2 (to calculate x^3), you can reduce it to 0, and it would still do surprisingly well.