Jump to content

[Energia Library] Fixed point math

Recommended Posts

This isn't my work, it is 99.9% from the libfixmath library found at http://code.google.com/p/libfixmath/  I have just made very minor alterations by putting some necessary #define's into fix16.h to allow it to compile on Energia (the standard library requires -D compiler options), and added a pow() function.


While working on an altimeter project, I needed to convert barometer pressure to altitude which required quite a bit of floating point math.  On small microcontrollers, floating point is typically bad to use - it is expensive in both code size, and execution time. In my case, I was having difficulty getting my code to fit into the 16k on the 2553. For simple mathematic functions, you can often avoid floating point and get by with integers. But with the need to raise numbers to the power of 1/5.257, pure integer math was out of the question for my needs.  I was having difficulty getting my code to fit into the 16k on the 2553. Even a very simple sketch that just did a calculation of the barometric formula (without any code to read from the barometer IC or output the display in readable form) ran to over 6k.


Enter this library which uses a fixed 16.16 format number held as a 32bit signed long, as a compromise between floating point & integer math.  Internally, this allows it to use pure integer math resulting in higher speed and smaller code size.  Using this library, my basic sketch to calculate the barometric formula dropped from over 6k in size to about 2.5k.  


Compared to floating point there are still some limitations - the maximum number is +/-32,767.9999... so there may be some instances where you need to divide your numbers to ensure you don't overflow. Also the precision isn't as good as a float, but for most needs it should work fine as a good lean alternative to pure floating point.  In my case, the altitude error compared to pure floating point was in the order of 0.001m - the barometer is not nearly that accurate so errors introduced by the maths were insignificant.


Documentation is available at the original source website, along with examples, although the examples are not 430 compatible.  The attached library will compile as-is for the 430 platform. I haven't tested it on Stellaris, but there is no reason it shouldn't also work. On Stellaris you may want to experiment with the #defines in the fix16.h file. Because the Stellaris is 32bit you can definitely take out FIXMATH_OPTIMISE_8BIT which will increase performance.


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...