sin, cos, since when?

Started by
21 comments, last by RolandofGilead 21 years, 5 months ago
Alright, I''ve seen in several posts, that using a look up table for sin and cos is not the fastest way. I assume that the fastest way is to use a function. I''ve been programming games for awhile now and I am pretty sure that when I started, using a look up table was faster. Could someone please tell me when things changed? Also in all the posts, no one bothered to mention why things changed. The only things I can guess is that sin and cos were made into an instruction that can be done on the processor or that the computer memory is now so much slower than the processor. Am I right or is it something else?
Advertisement
You''re probably right. FPUs includes instructions for trig functions, and they are fast nowadays, although I''m a sceptist of giving up all traditional gems.

---------------------------------------------
"VB is a disease"
in the badolden days, i''d use look up tables, but now its best to use sin or cos.
A note though, I doubt that using ANSI functions really are faster than LUTs. Even if they are optimized to utilize CPU/FPU instructions, the overhead is probably more than you loose using LUTs. I''m not sure about the best way, so I would recommend using macros for trigs which use the normal ANSI functions and then in the optimazing stage change them to inline assembly, LUT-functions, or whatever you see fit then.

---------------------------------------------
"VB is a disease"
On PCs, there is an instruction FSINCOS that computes sin() and cos() at the same time. Most of the times you need both values, so make sure that you use it (or that your compiler does).
sin(angle) = 1 - cos(angle)
cos(angle) = 1 - sin(Angle)

Therefore if you know the sine then you know the cosine and vice versa. Rather than hoping your compiler uses fsincos, you can do it yourself:

SinAngle = sin(Angle);
CosAngle = 1 - SinAngle;

~CGameProgrammer( );
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
quote:Original post by CGameProgrammer
sin(angle) = 1 - cos(angle)
cos(angle) = 1 - sin(Angle)

Therefore if you know the sine then you know the cosine and vice versa. Rather than hoping your compiler uses fsincos, you can do it yourself:

SinAngle = sin(Angle);
CosAngle = 1 - SinAngle;

~CGameProgrammer( );


Hmm...

sin(angle) squared + cos(angle) squared = 1

which contradicts the above quote.

It''s easy to see why using Pythagoras.



"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
quote:Original post by CGameProgrammer
sin(angle) = 1 - cos(angle)
cos(angle) = 1 - sin(Angle)


No, but
sin²(angle) = 1 - cos²(angle)
cos²(angle) = 1 - sin²(angle).

So, only if what you need is the squared values, you should try to "do it yourself"...
On modern computers the cpu is really fast and memory is really slow. So the guys that make the cpus put in big caches so the cpu can avoid waiting on the slow memeory all the time. However, these caches work best when you access memory in linear order. So if you hit a part of memory where your look up table for sin/cos sits at some random places in your code with some random indices, the processor will just sit there waiting for the slow memory. that''s why people don''t use LUTs anymore. That doesn''t mean LUT are always bad, but today, most of the time it''s faster to compute on the cpu.
Sorry, I was wrong. Actually what I was thinking of was a combination of that square identity and the fact that, for lookup tables, it helps to realize that sin(angle) = cos(angle+(pi/2)) and vice versa. Or something like that. I think I got it right this time.

~CGameProgrammer( );
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement