ezEngine  Milestone 7
ezFixedPoint< DecimalBits > Class Template Reference

Implements fixed point arithmetic for fractional values. More...

#include <FixedPoint.h>

Public Member Functions

EZ_FORCE_INLINE ezFixedPoint ()
 Default constructor does not do any initialization.
 
 ezFixedPoint (ezInt32 IntVal)
 Construct from an integer.
 
 ezFixedPoint (float FloatVal)
 Construct from a float.
 
 ezFixedPoint (double FloatVal)
 Construct from a double.
 
const ezFixedPoint< DecimalBits > & operator= (ezInt32 IntVal)
 Assignment from an integer.
 
const ezFixedPoint< DecimalBits > & operator= (float FloatVal)
 Assignment from a float.
 
const ezFixedPoint< DecimalBits > & operator= (double FloatVal)
 Assignment from a double.
 
ezInt32 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 ezFixedPoint< DecimalBits > &rhs) const
 'Equality' comparison.
 
bool operator!= (const ezFixedPoint< DecimalBits > &rhs) const
 'Inequality' comparison.
 
bool operator< (const ezFixedPoint< DecimalBits > &rhs) const
 'Less than' comparison.
 
bool operator> (const ezFixedPoint< DecimalBits > &rhs) const
 'Greater than' comparison.
 
bool operator<= (const ezFixedPoint< DecimalBits > &rhs) const
 'Less than or equal' comparison.
 
bool operator>= (const ezFixedPoint< DecimalBits > &rhs) const
 'Greater than or equal' comparison.
 
const ezFixedPoint< DecimalBits > operator- () const
 
void operator+= (const ezFixedPoint< DecimalBits > &rhs)
 += operator
 
void operator-= (const ezFixedPoint< DecimalBits > &rhs)
 -= operator
 
void operator*= (const ezFixedPoint< DecimalBits > &rhs)
 *= operator
 
void operator/= (const ezFixedPoint< DecimalBits > &rhs)
 /= operator
 
void operator*= (ezInt32 rhs)
 *= operator with integers (more efficient)
 
void operator/= (ezInt32 rhs)
 /= operator with integers (more efficient)
 
ezInt32 GetRawValue () const
 Returns the underlying integer value. Mostly useful for serialization (or tests).
 
void SetRawValue (ezInt32 val)
 Sets the underlying integer value. Mostly useful for serialization (or tests).
 

Private Attributes

ezInt32 m_Value
 

Detailed Description

template<ezUInt8 DecimalBits>
class ezFixedPoint< DecimalBits >

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 ezFixedPoint to get guaranteed deterministic behavior, you should minimize the usage of ezFixedPoint <-> float conversions. You can set ezFixedPoint variables from float constants, but you should never put data into ezFixedPoint variables that was computed using floating point arithmetic (even if the computations are simple and look harmless). Instead do all those computations with ezFixedPoint variables.


The documentation for this class was generated from the following files: