# calculate sin and cos

This topic is 3046 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do I calculate sin and cos in order to store the results in a lookup table. Is there any formula?

##### Share on other sites
In general you'd just use the sin() and cos() functions of whatever programming language you're using. If for some reason those aren't available you can use the Taylor series for sin and cos.

##### Share on other sites
Why are you building a lookup table?

##### Share on other sites
So i don't call sin or cos on the fly. It might be faster. I should mention I am on a mobile phone arm9 processor.

##### Share on other sites
Continuing on the Tayler series expansion and your remark about speed. The following might be interesting:

http://en.wikipedia.org/wiki/Small-angle_approximation

If you don't mind losing some accuracy over that speed.

##### Share on other sites
Quote:
 Original post by bertchttp://en.wikipedia.org/wiki/Small-angle_approximation
The small angle approximation is accurate only for *extremely small* angles - i.e. in the limit as theta approaches 0.

##### Share on other sites
Quote:
 Original post by Hotshot5000So i don't call sin or cos on the fly. It might be faster. I should mention I am on a mobile phone arm9 processor.

you'd still use the normal sin and cos functions to generate the lookup table.

for example:
double sinTable[62832]; // (roughly 2PI * 1000)for (int i=0;i<62832];i++) {    sinTable[i] = sin(i/1000.0);}//Then you can replacey = sin(x);//Withy = sinTable[(int)(x*1000))];//You want to ensure that x is in the range 0 to 2PI aswell though.

It might actually be slower though since you may need to do a multiplication and cast when accessing the table unless you can store the input in the right format and range and memory access is getting relatively slow these days. (Allthough i don't know how this applies to the ARM9)

##### Share on other sites
A large lookup table could cost you in terms of cache misses. Taylor series are almost never a good solution for approximations, because they are "local" approximations. You should use "global" approximations, ones that are typically derived from least-squares fitting of integral norms. For example,

//----------------------------------------------------------------------------float FastSin0 (float angle){    // The input must be in [0,pi/2], max error 1.7e-04    float angleSqr = angle*angle;    float result = 7.61e-03f;    result *= angleSqr;    result -= 1.6605e-01f;    result *= angleSqr;    result += 1.0f;    result *= angle;    return result;}//----------------------------------------------------------------------------float FastSin1 (float angle){    // The input must be in [0,pi/2], max error 1.9e-08    float angleSqr = angle*angle;    float result = -2.39e-08f;    result *= angleSqr;    result += 2.7526e-06f;    result *= angleSqr;    result -= 1.98409e-04f;    result *= angleSqr;    result += 8.3333315e-03f;    result *= angleSqr;    result -= 1.666666664e-01f;    result *= angleSqr;    result += 1.0f;    result *= angle;    return result;}//----------------------------------------------------------------------------float FastCos0 (float angle){    // The input must be in [0,pi/2], max error 1.2e-03    float angleSqr = angle*angle;    float result = 3.705e-02f;    result *= angleSqr;    result -= 4.967e-01f;    result *= angleSqr;    result += 1.0f;    return result;}//----------------------------------------------------------------------------float FastCos1 (float angle){    // The input must be in [0,pi/2], max error 6.5e-09    float angleSqr = angle*angle;    float result = -2.605e-07f;    result *= angleSqr;    result += 2.47609e-05f;    result *= angleSqr;    result -= 1.3888397e-03f;    result *= angleSqr;    result += 4.16666418e-02f;    result *= angleSqr;    result -= 4.999999963e-01f;    result *= angleSqr;    result += 1.0f;    return result;}//----------------------------------------------------------------------------

##### Share on other sites
It's interesting to note that every angle that it is an integral multiple of 3 degrees has a closed form solution in terms of primitive arithmetic operations sqrt, add, subtract, multiply, and divide. I don't have a more easily accessible reference for this other than E.W. Hobson's A Treatise on Plane and Advanced Trigonometry. In any case, it's not usually applicable in games since often 3 degrees is too coarse, but if 3 degrees granularity is fine enough, you can use this method combined with the famous "fast sqrt" implementation that uses a bitwise operator, then linearly interpolate between angles for computing values that are in between consecutive multiples of 3 degrees.

I only mention this because I think it's totally awesome that sin/cos/tan of integral multiples of 3 degrees are algebraic numbers and thus have explicit closed form solutions, and I've always wanted to find an actual use for the fact :)

##### Share on other sites
this is what you need

http://en.wikipedia.org/wiki/CORDIC

##### Share on other sites
Quote:
 Original post by biki_this is what you needhttp://en.wikipedia.org/wiki/CORDIC

Quote:
 http://en.wikipedia.org/wiki/CORDICOn the other hand, when a hardware multiplier is available (e.g., in a DSP microprocessor), table-lookup methods and power series are generally faster than CORDIC.

I believe the ARM9 does have a hardware multiplier.

##### Share on other sites
I once dabbled in writing games for my PDA once - it was a Dell Axim x51v with a 624mhz XScale ARM processor. It didn't come with any floating-point hardware, so FP operations were emulated in software. Expectedly enough, this was excruciatingly slow - I recall that even the simplest floating-point operations took hundreds or thousands of cycles to complete.

Of course, it depends on the CPU. Some ARM chips come with coprocessors that offer hardware-accelerated floating-point. I know the iPhone has one. But I don't know anything specific about ARM9. In the event that it doesn't have hardware FP, you're going to want to avoid floating-point as much as possible if you're really concerned about performance.

##### Share on other sites

This topic is 3046 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.