Fixed Point Math

Limiting an Application to a Sin

A few years ago, I needed to use some data with decimal points on a DSP processor. The math library that came with the compiler was just too large and cumbersome. So, I wrote a fixed point library. This library is useful anywhere basic math needs to be done FAST. Microcontrollers don't use the floating point emulation, and PCs don't use the math coprocessor. With some tweaking, I have used it for basic animation and audio processing.

The fixed point library is very KISS. There is a class called "fixed" that essentially creates a new variable type similar to float or double.

#include <Fixed.h>
#include <math.h>
   fixed a, b, c, d;
   float f;

   // Assign the vars
   a = 2.56890830294f;
   b = 10.374237497987f;
   // Multiply them
   c = a*b;
   f = a*b;

   // Display the results
  printf("%.6f * %.6f = %.6f = %.6f\n", (float)a,(float)b,
         (float)c, f);

Also implemented in the library are all the arithmetic operators ( *, /, +, - ), and the trigonometric functions sin(), cos(), tan(), log(), log10(), pow(), sqrt(), and so forth.

// Assign the vars
a = 2.56890830294f;
b = 10.374237497987f;

// compute them
c = a.cos();
d = cosx(b);

// Display the results
printf("  cos(%.6f) = %.6f\n cosx(%.6f) = %.6f\n", (float)a,
       (float)c, (float)b, (float)d);

A lot of microcontrollers have limits to the integer sizes and other restrictions. There are a couple of #defines in the fixed.h header that account for some of these.

// Allow floating point input
// Allow longs

FIXED_HAS_DOUBLE defines whether the processor supports the "double" size. Comment it out if a "double" == "float".

FIXED_HAS_LONG defines whether the processor differentiates between "long" and "int". Comment it out if a "long" == "int".

Internally, the class uses "long long". This is just used to ramp up precision. If the processor or compiler do not support "long long", it will just reduce precision.precision.

Where you use this library class takes some thought. There is overhead in converting between "fixed" and "float", so converting to "fixed" for one operation is not recommended for speed. Also, the trigonometric functions are NOT optimized, so if you have a math coprocessor, it may be faster to use it for the trigonometric functions.

This library is for doing basic fixed point math using integers. It is also a great learning tool to learn how math libraries work.

About the Author

Erik Gawtry

I have worked as a Software Engineer since 1985. I currently work with Windows, Unix/Linux, DSP, and Microcontrollers.



  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds