![]() |
Plasma Engine
2.0
|
Implements fixed point arithmetic for fractional values. More...
#include <FixedPoint.h>
Public Member Functions | |
PL_ALWAYS_INLINE | plFixedPoint ()=default |
Default constructor does not do any initialization. | |
plFixedPoint (plInt32 iIntVal) | |
Construct from an integer. | |
plFixedPoint (float fVal) | |
Construct from a float. | |
plFixedPoint (double fVal) | |
Construct from a double. | |
const plFixedPoint< DecimalBits > & | operator= (plInt32 iVal) |
Assignment from an integer. | |
const plFixedPoint< DecimalBits > & | operator= (float fVal) |
Assignment from a float. | |
const plFixedPoint< DecimalBits > & | operator= (double fVal) |
Assignment from a double. | |
plInt32 | 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 plFixedPoint< DecimalBits > &rhs) const |
'Equality' comparison. | |
bool | operator!= (const plFixedPoint< DecimalBits > &rhs) const |
'Inequality' comparison. | |
bool | operator< (const plFixedPoint< DecimalBits > &rhs) const |
'Less than' comparison. | |
bool | operator> (const plFixedPoint< DecimalBits > &rhs) const |
'Greater than' comparison. | |
bool | operator<= (const plFixedPoint< DecimalBits > &rhs) const |
'Less than or equal' comparison. | |
bool | operator>= (const plFixedPoint< DecimalBits > &rhs) const |
'Greater than or equal' comparison. | |
const plFixedPoint< DecimalBits > | operator- () const |
void | operator+= (const plFixedPoint< DecimalBits > &rhs) |
+= operator | |
void | operator-= (const plFixedPoint< DecimalBits > &rhs) |
-= operator | |
void | operator*= (const plFixedPoint< DecimalBits > &rhs) |
*= operator | |
void | operator/= (const plFixedPoint< DecimalBits > &rhs) |
/= operator | |
void | operator*= (plInt32 rhs) |
*= operator with integers (more efficient) | |
void | operator/= (plInt32 rhs) |
/= operator with integers (more efficient) | |
plInt32 | GetRawValue () const |
Returns the underlying integer value. Mostly useful for serialization (or tests). | |
void | SetRawValue (plInt32 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 plFixedPoint to get guaranteed deterministic behavior, you should minimize the usage of plFixedPoint <-> float conversions. You can set plFixedPoint variables from float constants, but you should never put data into plFixedPoint variables that was computed using floating point arithmetic (even if the computations are simple and look harmless). Instead do all those computations with plFixedPoint variables.