# C++ fixed point library?

C++MathFixed Point## C++ Problem Overview

I am looking for a free C++ fixed point library (Mainly for use with embedded devices, not for arbitrary precision math). Basically, the requirements are:

- No unnecessary runtime overhead: whatever can be done at compile time, should be done at compile time.
- Ability to transparently switch code between fixed and floating point, with no inherent overhead.
- Fixed point math functions. There's no much point using fixed point if you need to cast back and forth in order to take a square root.
- Small footprint.

Any suggestions?

## C++ Solutions

## Solution 1 - C++

There is an open-source fixed point math library project which can be found by following the links below:

It is a C static library with a C++ class interface for C++ users, it implements the following functionality: Trig. Functions: sin, cos, tan, asin, acos, atan, atan2 Saturated Arithmetic: sadd, ssub, smul, sdiv Other Functions: sqrt, exp

It only supports 16.16 fixed-point datatype.

It is an actively developed open-source project (looking for interested developers).

## Solution 2 - C++

Check out the following two good implementations about handling fixed point representation in C++ (no external libs are needed).

- Fixed-Point-Class by Peter Schregle. It also efficiently implements the basic operations like
*addition*,*multiplication*, and*division*.

Code example:

```
#include <fixed_point.h>
using namespace fpml;
main()
{
fixed_point<int, 16> a = 256;
fixed_point<int, 16> b = sqrt(a);
}
```

2. Implementing Fixed-Point Numbers in C++ by Khuram Ali.

## Solution 3 - C++

Here is an open source fixed-point library on GitHub:

https://github.com/mbedded-ninja/MFixedPoint

It supports 32-bit and 64-bit fixed-point numbers (with a arbitrary quotient) and both fast (everything is templated, but a little more manual) and slow fixed-point numbers (more automatic, but slower).

It is geared towards embedded platforms, however I have used it on both microcontrollers and Linux without any issues.

## Solution 4 - C++

I got a nice little c++ header. You can find it under sweet::Fixed. Simply define typedef sweet::Fixed MyFloat; and use it like any other float value. Or exchange it whatever float type you want later. The class has two 64 bit values. One for the integer part and on for the fraction.

I have a small fixed point c++11 class header impl in sweet.hpp called **fixed.hpp**. It uses 32bit for both parts.

```
typedef float MyFloat; // This will feel the same
typedef sweet::Fixed MyFloat; // like this
```

## Solution 5 - C++

I will try http://www.efgh.com/software/fixed.htm tiny lib...

## Solution 6 - C++

Maybe you could try the GMP or MPFR libraries. I'm quite sure they will satisfy your performance needs, but maybe they are too much for your needs and you want something more lightweight. Anyway, look here:

or here:

## Solution 7 - C++

I haven't ever used SPUC, but the description claims fixed-point data types and some math functions.