# calculate sin and cos

This topic is 3257 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 = 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

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 22
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631733
• Total Posts
3001927
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!