C++ – How to calculate Sin function quicker and more precisely

algorithmsc

I want to calculate y(n)=32677Sin(45/1024•n), where y is an integer and n ranges from 0 to 2048. How can I make this process quicker and more precisely?
Now I want to show you a reference answer:
Since Sin(a+b)=Sin(a)Cos(b)+Cos(a)Sin(b)
And Cos(a+b)=Cos(a)Cos(b)-Sin(a)Cos(b).
So I can store Sin(45/1024•1) and Cos(45/1024•1) only.Then use this formula:

Sin(45/1024•2)=Sin(45/1024•1+45/1024•1),
Cos(45/1024•2)=Cos(45/1024•1+45/1024•1),
Sin(45/1024•n)=Sin(45/1024•(n-1)+45/1024•1),
Cos(45/1024•n)=Cos(45/1024•(n-1)+45/1024•1) ,
This way maybe quicker without storing large array.

Best Answer

If n ranges from 0 to 2048, you can pre-calculate the values, store then in an array. y(n) would become values[n].

Related Topic