Background : The other day I felt like updating my knowledge with GUI programming so I made a small application that rendered a multivalued periodic function $\mathbb R^3 \to \mathbb [0 ,255]^3$. The three in-dimensions being $(x,y,t)$ and the three out-dimensions $(R,G,B)$.
The problem I encountered was that my math librarys "sin" routine which I had intended to use was too slow. I instead tried a sawtooth-like approximation using round, and fabs which was much faster but not nearly as pretty.
How can I make a computationally cheap but still relatively smooth sine-like curve for purposes like this?
My own ideas of how to do it have mostly been in the realms of low-order polynomial approximations and interpolation.
Edit After implementation in C and some fiddling I managed to get Claude's proposals
- Mahabhaskariya : 3% CPU load and
- RobJohn : 2% CPU load. This is same as my edgy saw-tooth above.
This can be compared to cmath.h functions
- double precision "sin" which took 10% CPU.
- single precision "sinf" which took 5% and
Now included in CPU load is also everything else that is required by QT to plot to screen and so on so it is quite possible that the actual sin routines are many orders of magnitude faster. Replacing sin by a function that returns 0 gives 1% CPU load. So it is possible that my implementation of Claudes fonctions are 2-3 and 5-10 times faster. If we need accuracy in these estimates we should do more careful time measurements, but as this mostly is an artistic application it is good enough for now.