![]() |
XII Release 0.1.0
|
Implements fixed point arithmetic for fractional values. More...
#include <FixedPoint.h>
Public Member Functions | |
XII_ALWAYS_INLINE | xiiFixedPoint ()=default |
Default constructor does not do any initialization. | |
xiiFixedPoint (xiiInt32 iIntVal) | |
Construct from an integer. | |
xiiFixedPoint (float fVal) | |
Construct from a float. | |
xiiFixedPoint (double fVal) | |
Construct from a double. | |
const xiiFixedPoint< DecimalBits > & | operator= (xiiInt32 iVal) |
Assignment from an integer. | |
const xiiFixedPoint< DecimalBits > & | operator= (float fVal) |
Assignment from a float. | |
const xiiFixedPoint< DecimalBits > & | operator= (double fVal) |
Assignment from a double. | |
xiiInt32 | ToInt () const |
Implicit conversion to int (the fractional part is dropped). | |
float | ToFloat () const |
Implicit conversion to float. | |
double | ToDouble () const |
Implicit conversion to double. | |
bool | operator== (const xiiFixedPoint< DecimalBits > &rhs) const |
'Equality' comparison. | |
std::strong_ordering | operator<=> (const xiiFixedPoint< DecimalBits > &rhs) const |
Comparison operator. | |
const xiiFixedPoint< DecimalBits > | operator- () const |
void | operator+= (const xiiFixedPoint< DecimalBits > &rhs) |
+= operator | |
void | operator-= (const xiiFixedPoint< DecimalBits > &rhs) |
-= operator | |
void | operator*= (const xiiFixedPoint< DecimalBits > &rhs) |
*= operator | |
void | operator/= (const xiiFixedPoint< DecimalBits > &rhs) |
/= operator | |
void | operator*= (xiiInt32 rhs) |
*= operator with integers (more efficient) | |
void | operator/= (xiiInt32 rhs) |
/= operator with integers (more efficient) | |
xiiInt32 | GetRawValue () const |
Returns the underlying integer value. Mostly useful for serialization (or tests). | |
void | SetRawValue (xiiInt32 iVal) |
Sets the underlying integer value. Mostly useful for serialization (or tests). | |
Implements fixed point arithmetic for fractional values.
Advantages over float and double are mostly that the computations are entirely integer-based and therefore have a predictable (i.e. deterministic) result, independent from floating point settings, SSE support and differences among CPUs. Additionally fixed point arithmetic should be quite fast, compare to traditional floating point arithmetic (not comparing it to SSE though). With the template argument 'DecimalBits' you can specify how many bits are used for the fractional part. I.e. a simple integer has zero DecimalBits. For a precision of about 1/1000 you need at least 10 DecimalBits (1 << 10) == 1024. Conversion between integer and fixed point is very fast (a shift), in contrast to float/int conversion.
If you are using xiiFixedPoint to get guaranteed deterministic behavior, you should minimize the usage of xiiFixedPoint <-> float conversions. You can set xiiFixedPoint variables from float constants, but you should never put data into xiiFixedPoint variables that was computed using floating point arithmetic (even if the computations are simple and look harmless). Instead do all those computations with xiiFixedPoint variables.