Original Post
This is a short question. Which is more CPU expensive in C++? Trig functions like sin, or the square root function?
Quote:
Original post by TheAdmiral
The ratios remain roughly unchanged but, curiously, some of the float versions took longer. Any guesses?
double mysin(double x) { return x - x*x*x/6 + x*x*x*x*x/120;}Quote:
Original post by smc
Maybe I am missing the point of this thread, but you could trade size for speed and use a lookup table for you trig functions.
Quote:
Original post by RAZORUNREAL
Isn't the sse sqrt already based on a lookup table but done in hardware?
Quote:
Original post by erissian
If you're really concerned about speed, and you can always roll your own for arbitrary precision.
return x - x*x*x/6 + x*x*x*x*x/120;toreturn x*(1 - x*x*(1/6 + x*x/120));Quote:
Original post by TheAdmiral Quote:
Original post by erissian
If you're really concerned about speed, and you can always roll your own for arbitrary precision.
Sure you could, but I've found that optimised evaluation of a Taylor expansion only outperforms their trig function equivalents below order-four, or so, which makes them useful only for the quadratic/cubic versions ('cause linear is just silly). If you weigh in the fact that you have an expensive modulus function to call, to bring the input into localised range, you're probably losing out.
Should you be possessed to proceed in this way, however, you can use the distributivity laws to optimisetoreturn x - x*x*x/6 + x*x*x*x*x/120;return x*(1 - x*x*(1/6 + x*x/120));
Regards
Admiral
Quote:
Original post by mrbig
This comes to mind:
float a = x * x;
return x * (1.0 - a * (0.166 + a * 0.00833));
Quote:
Original post by AdamGL
Thanks for the quick reply. I'm just trying to figure out the co-ordinates of a line with a given starting point, length and angle. to find the y I use sin, and to find x I can use cos or square root. I didn't know which so I based my decision on speed. So thank you very much for helping.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more