C++ fixed point library? [closed]

Solution 1:

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

  • libfixmath Project Page
  • libfixmath Wikipedia Article

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:

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

  1. 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:

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:

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