#### Archived

This topic is now archived and is closed to further replies.

# faster sin() really faster?

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

## Recommended Posts

I made a little math function that calculates sin of an angle (in radians). I tested it against the math.h sin function and when you run it once, its twice as fast, but the more times you run them, the math.h sin starts to take less time than mine after about 5 calls. Why is this? Maybe its my code, does anyone see the problem or how I could speed this up? I was think about a table for fa. Is mod (%) slow? __inline double dsin(double x) { double xsq = x*x; double xp = x*xsq; double fa = 1; for(int i=3; i<24; i+=2) { fa = fa*i*(i-1); if((i+1)%4 == 0) x -= xp/fa; else x += xp/fa; xp = xp*xsq; } return x; } I just used a infinite power series to a limited degree, not perfectly accurate, but all I need.

##### Share on other sites
Yes, mod is slow. You can unroll the loop yourself, although an aggressive optimizer may be able to do it.

The other thing is that you are just adding too many terms. If you need that much precission, just use sin(), which is implemented in hardware and much faster than any software implementation you can write. You can only win in speed by sacrificing precission.

##### Share on other sites
you can replace "(i+1) % 4" by "(i+1) & 3"

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002131
×