# Accuracy of trig functions near asymptotes computer programming

• May 19th 2008, 09:56 PM
Geoff M
Accuracy of trig functions near asymptotes computer programming
Hi all,

I have a question regarding the accuracy of trig functions with regard to computer programming.

Do the accuracy of trig functions change as the angle approaches the function's asymptotes?

For example; would the angle returned by arctan be less accurate for say a triangle with height 100000 and width .0000001 than say a triangle with height 10 and width 10. What I'm steering towards here is that as the function reaches it's asymptote does the accuracy reduce?

Likewise I'm wondering (but perhaps it's not as relevant) with Sin and Cos as they reach their maximum and minimum values of +1 and -1 do they lose accuracy as the function flattens out (as the derivate reaches zero).

Also with respect to computer programming we're using floating point calculations which have inherent inaccuracies. Does anyone have any thoughts on how this might all tie together?

In my early programming I used to construct static tables for performing integer math on sin (scaled up by 10000 etc or a power of 2) so I could do fast lookups, but I always avoided using tan lookups because of the amyptototes and thought I could not perform the integer math effectively.

Again any thoughts?
• May 20th 2008, 11:00 AM
TKHunny
The usual risks are dividing by very small values (adding fake significance) or subtracting very similar values (losing significance). The specifics of your question rely on the specifics of your physical implementation. One would have to know EXACTLY how things are calculated. Exactly what algorithm is used to calculate sine, cosine, and tangent? For example, if the tangent is calculated directly (some kind of McLauren series, for example), that would be different than if the tangent is calculated from division of corresponding values of sine and cosine.

As a VERY ROUGH example, I started with McLauren expansions up through $x^{11}$ for sine, cosine, and tangent. I'll call these new functions s(x), c(x), and t(x) - where we expect t(x) = s(x)/c(x).

For x = 0 we get the very predictable:
s(0) = 0
c(0) = 1
t(0) = 0
s(0)/c(0) = 0

Not much interesting about that. With a McLauren expansion, we expect x = 0 to deliver exceptional results.

For $x = \frac{\pi}{4} < 1$ we get

$s\left(\frac{\pi}{4}\right) = 0.707106781179619$

$c\left(\frac{\pi}{4}\right) = 0.707106781071925$

Reference: $\frac{\sqrt{2}}{2} = 0.707106781186548$

$t\left(\frac{\pi}{4}\right) = 0.999792766967732$

$\frac{s(\frac{\pi}{4})}{c(\frac{\pi}{4})} = 1.00000000015230$

First, one might notice that c is NOT equal to s. This is because of the particular implementation. c(x) is an even function and s(x) has one higher order term. Recall that I quit at $x^{11}$. This makes c(x) actually quit at $x^{10}$, since it has no $x^{11}$. Neither is equal to the "exact" value.

Second, it is very clear that $t \neq s/c$. Frankly, the t(x) result is somewhat disappointing, giving only 3 decimal places exact. So we should prefer the quotient, right?

For $x = \frac{\pi}{2} > 1$ we get

$s\left(\frac{\pi}{2}\right) = 0.999999943741051$

$c\left(\frac{\pi}{2}\right) = -0.000000464766008$

$t\left(\frac{\pi}{2}\right) = 7.95774685489739$

$\frac{s(\frac{\pi}{2})}{c(\frac{\pi}{2})} = -2151620.22527681$

Now what?! The variations in tangent call into question what side of the asymptote we are on. For an asymptote, we would like a really big number (perhaps even the largest machine number), but I'm sure we'd be more comfortable with the sign not changing. Again, most of this error is a result of the SPECIFIC approximation. The McLauren expansion of tan(x) has ONLY positive terms. For x > 0, it cannot EVER produce a negative value. This, quite obviously, is not the case for sin(x)/cos(x).

What's the real answer to your question? That is very hard to say without a very clear understanding of the nature of your environment. The results shown here are specific to my implementation. Your results may differ, even in an attempt to reproduce what I have shown.

Note: Most implementations would have far sneakier algorithms than a simple McLauren expansion through $x^{11}$. Only the designers can tell you what is in there.
• May 20th 2008, 03:02 PM
CaptainBlack
Quote:

Originally Posted by Geoff M
Hi all,

I have a question regarding the accuracy of trig functions with regard to computer programming.

Do the accuracy of trig functions change as the angle approaches the function's asymptotes?

For example; would the angle returned by arctan be less accurate for say a triangle with height 100000 and width .0000001 than say a triangle with height 10 and width 10. What I'm steering towards here is that as the function reaches it's asymptote does the accuracy reduce?

Likewise I'm wondering (but perhaps it's not as relevant) with Sin and Cos as they reach their maximum and minimum values of +1 and -1 do they lose accuracy as the function flattens out (as the derivate reaches zero).

Also with respect to computer programming we're using floating point calculations which have inherent inaccuracies. Does anyone have any thoughts on how this might all tie together?

In my early programming I used to construct static tables for performing integer math on sin (scaled up by 10000 etc or a power of 2) so I could do fast lookups, but I always avoided using tan lookups because of the amyptototes and thought I could not perform the integer math effectively.

Again any thoughts?

If its done correctly arctan should give near full machine precision up to the point it goes out of normalisation/range.

RonL