5 ms·
There should be plenty of C trigonometric algorithms made for fixed point. They usually use lookup tables and interpolation. Used a lot in embedded microcontrol
by ChuckNorris89 4y ago
There should be plenty of C trigonometric algorithms made for fixed point. They usually use lookup tables and interpolation. Used a lot in embedded microcontrollers that don't have hardware floating point or hardware trig functions, and in automotive where floats were (still are?) a no-go due to the dangerous float math pitfalls and the safety critical nature of ECUs.
I remember writing a fast, small footprint, but very accurate fixed point trig function for the microcontroller used in my batcher project, many lives ago. I'll try to find it and put it on github.
The neat thing is that you can use the native overflow wrap around property enabled by C integers and processor registers to rotate back and fourth around the trigonometric circle without needing to account for the edge cases of the 0/360 degree point. You do need to suppress MISRA warning here though as it will go nuts seeing that.
- rtfeldman 4y agoI'd love to see some implementations of fixed point trig functions, but I haven't found any! Do you have any links by any chance?
- Someone 4y agoI’d Google “forth trig functions” and get, for example https://web.stanford.edu/~dljaffe/SVFIG99/trig.html https://web.stanford.edu/~dljaffe/SVFIG99/trig.html https://www.hpmuseum.org/forum/thread-18123.html https://www.hpmuseum.org/forum/thread-18123.html (which links to https://archive.org/details/Forth_Dimension_Volume_04_Number_1/page/n8/mode/1up https://archive.org/details/Forth_Dimension_Volume_04_Number...)
- rtfeldman 4y agoNice, I didn't realize Forth used them - thank you!
- ChuckNorris89 4y agoI don't have the code at hand to show you, nor do I have any links similar trig stuff as I wrote everything from scratch, first in Matlab to prover the concept, then in C, as there wasn't anything similar online at the time, but I used the principles of fixed point fractional math of representing floating point numbers as scaled integers in the Q notation, which is well documented for use in microcontrollers and DSPs of the era (early to late '00s) Here's the wiki gist with some C code snippet examples [1]. Here's a more detailed math paper that goes in the weeds[2]. Here's a nice presentation from Motorola-Freescale-NXP I just found which I wish I had back then as this is awesome and explains the basics for everyone to understand, and seeing those Motorola Windows 95 GUI tools brings a nostalgic smile on my face. [1] https://en.wikipedia.org/wiki/Q_(number_format) https://en.wikipedia.org/wiki/Q_(number_format) [2] http://darcy.rsgc.on.ca/ACES/ICE4M/FixedPoint/FixedPointRepresentationFractionalMath.pdf http://darcy.rsgc.on.ca/ACES/ICE4M/FixedPoint/FixedPointRepr... [3] https://www.nxp.com/files-static/training_presentation/TP_CONTROLLER_CONTINUUM_FP.pdf https://www.nxp.com/files-static/training_presentation/TP_CO...