57

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?

uj2
  • 1,375
  • 2
  • 15
  • 15
  • Possible dupe: http://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math – Pavel Radzivilovsky May 31 '10 at 20:15
  • 7
    @Pavel: Thanks. My question is not how to implement fixed point math, though. I would write it myself if I had to, but I'm trying to avoid writing all the boilerplate code. That's why I'm asking for a ready made solution. Additionaly, I didn't find any mentioing to fixed-point math functions on other similiar posts in SO. – uj2 May 31 '10 at 20:27
  • 2
    I wouldn't call maths libraries "boilerplate". – Charles Stewart Jun 01 '10 at 08:59
  • 79677 comment [79942][1] links to http://www.codef00.com/code/Fixed.h which seems vaguely sane. No functions though. Not looked at division/rounding, and I imagine it requires a signed right-shift. [1]: http://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math/79942#79942 – tc. Jun 02 '10 at 12:15
  • possible duplicate of [What do you use for fixed point representation in C++?](http://stackoverflow.com/questions/146207/what-do-you-use-for-fixed-point-representation-in-c) – herohuyongtao Jan 14 '14 at 10:50
  • I'm voting to close this question as off-topic - but maybe the rules were different back then – Strawberry Nov 22 '15 at 19:23

7 Answers7

8

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).

endolith
  • 23,532
  • 31
  • 125
  • 187
flatmush
  • 211
  • 3
  • 5
  • 3
    why it only supports Q16.16? so the maximum value is 65536. Not enough for many cases. – Oleg Vazhnev Jan 10 '14 at 10:52
  • 16.16 is the most common, you're unlikely to ever use over 32-bits if you're using fixed point for performance reasons (I don't know of any 64-bit microcontrollers). There are some other minor formats like 24.8, or 8.24 and I guess a large number of odd formats, but if you're at the stage of needing those then you probably just need to write the code by hand (like I normally do). – flatmush Mar 19 '14 at 16:16
  • Dead URL ...... – Cuadue Mar 16 '17 at 20:14
  • Code is now on github as far as I can see: https://github.com/PetteriAimonen/libfixmath – Donal Fellows Feb 02 '18 at 09:15
5

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.

herohuyongtao
  • 47,739
  • 25
  • 124
  • 164
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.

gbmhunter
  • 1,607
  • 3
  • 22
  • 24
2

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
Brad Larson
  • 169,393
  • 45
  • 393
  • 567
burner
  • 343
  • 2
  • 10
0

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

Stef
  • 3,566
  • 5
  • 38
  • 56
-3

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:

GMP library

or here:

MPFR library

PeterK
  • 6,234
  • 5
  • 48
  • 82
  • 1
    Nice libraries, good C++ bindings. But both support just big ints, rationals and big floats: I don't think either support fixed point. – Charles Stewart Jun 01 '10 at 08:58
  • My bad, i mistakenly thought you meant "floating" point. I should read the question more carefully in the future. Sorry. – PeterK Jun 01 '10 at 09:08
-4

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

mtrw
  • 32,117
  • 7
  • 59
  • 70
  • 2
    I see mention of "fixed-width integers" on the site, but not fixed-point values. Can you point me to the fixed-point part of the library? – Brooks Moses Nov 11 '11 at 22:41
  • @BrooksMoses - they're the same thing. Fixed width means you can specify the location of the decimal point, which then stays in that fixed position through all the math. – mtrw Nov 18 '11 at 02:29
  • 2
    Are you sure about that? I'm looking at the core of their "fixed-width integer" implementation in http://spuc.cvs.sourceforge.net/viewvc/spuc/spuc/generic/int_s.h?view=markup, and that looks like an implementation of integers with specific widths left of the decimal point (so the remaining bits in the "long" underlying type are simply masked out), not of fixed-point numbers. The telling point is that there's no scaling on multiplication, as I would expect if the decimal point weren't at the right-hand end of the number. – Brooks Moses Nov 18 '11 at 06:20