Skip to content
Fixed-point arithmetic in C++
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Fixed-point arithmetic in C++

Some pet-projects ago, i got hooked up by fixed-point arithmetic and its usefullness on the mobile platform where GPUs and multiple cores weren't that ubiquitous.

While experimenting with the traditional implementations such as this one or this other one, i wanted to try an object-oriented approach and this is the result.

At that time, a more traditional-style implementation (#defines-based) was really faster than the object-oriented one, obviously the overhead was larger than the gain, but it was an interesting experiment nonetheless.


The class itself predefines both the 24.8 and the 16.16 formats:

typedef FixedPoint< 8, LowPrecision,  LowPrecision> fixed8_t;
typedef FixedPoint<16, HighPrecision, HighPrecision> fixed16_t;

An usage example coult be a generic, datatype-unaware matrix implementation:

template<class Number>
class matrix {
		Number m[4][4];


	// matrix indexing
	inline Number operator()(const int,const int);

	// matrix operations
	matrix<Number> operator*(const matrix<Number>);
	matrix<Number> operator/(const matrix<Number>);

Then you should be able to declare your test matrix types like this:

matrix<float_t> float_matrix;
matrix<fixed16_t> fixed_matrix;
You can’t perform that action at this time.