Plasma Engine  2.0
Loading...
Searching...
No Matches
FixedPoint.h
1#pragma once
2
3#include <Foundation/Math/Declarations.h>
4
21template <plUInt8 DecimalBits>
23{
24public:
26 PL_ALWAYS_INLINE plFixedPoint() = default; // [tested]
27
29 /* implicit */ plFixedPoint(plInt32 iIntVal) { *this = iIntVal; } // [tested]
30
32 /* implicit */ plFixedPoint(float fVal) { *this = fVal; } // [tested]
33
35 /* implicit */ plFixedPoint(double fVal) { *this = fVal; } // [tested]
36
38 const plFixedPoint<DecimalBits>& operator=(plInt32 iVal); // [tested]
39
41 const plFixedPoint<DecimalBits>& operator=(float fVal); // [tested]
42
44 const plFixedPoint<DecimalBits>& operator=(double fVal); // [tested]
45
47 plInt32 ToInt() const; // [tested]
48
50 float ToFloat() const; // [tested]
51
53 double ToDouble() const; // [tested]
54
56 bool operator==(const plFixedPoint<DecimalBits>& rhs) const { return m_iValue == rhs.m_iValue; } // [tested]
57
59 bool operator!=(const plFixedPoint<DecimalBits>& rhs) const { return m_iValue != rhs.m_iValue; } // [tested]
60
62 bool operator<(const plFixedPoint<DecimalBits>& rhs) const { return m_iValue < rhs.m_iValue; } // [tested]
63
65 bool operator>(const plFixedPoint<DecimalBits>& rhs) const { return m_iValue > rhs.m_iValue; } // [tested]
66
68 bool operator<=(const plFixedPoint<DecimalBits>& rhs) const { return m_iValue <= rhs.m_iValue; } // [tested]
69
71 bool operator>=(const plFixedPoint<DecimalBits>& rhs) const { return m_iValue >= rhs.m_iValue; } // [tested]
72
73
74 const plFixedPoint<DecimalBits> operator-() const { return plFixedPoint<DecimalBits>(-m_iValue, true); }
75
77 void operator+=(const plFixedPoint<DecimalBits>& rhs) { m_iValue += rhs.m_iValue; } // [tested]
78
80 void operator-=(const plFixedPoint<DecimalBits>& rhs) { m_iValue -= rhs.m_iValue; } // [tested]
81
83 void operator*=(const plFixedPoint<DecimalBits>& rhs); // [tested]
84
86 void operator/=(const plFixedPoint<DecimalBits>& rhs); // [tested]
87
89 void operator*=(plInt32 rhs) { m_iValue *= rhs; } // [tested]
90
92 void operator/=(plInt32 rhs) { m_iValue /= rhs; } // [tested]
93
95 plInt32 GetRawValue() const { return m_iValue; }
96
98 void SetRawValue(plInt32 iVal) { m_iValue = iVal; }
99
100private:
101 plInt32 m_iValue;
102};
103
104template <plUInt8 DecimalBits>
105float ToFloat(plFixedPoint<DecimalBits> f)
106{
107 return f.ToFloat();
108}
109
110// Additional operators:
111// plFixedPoint operator+ (plFixedPoint, plFixedPoint); // [tested]
112// plFixedPoint operator- (plFixedPoint, plFixedPoint); // [tested]
113// plFixedPoint operator* (plFixedPoint, plFixedPoint); // [tested]
114// plFixedPoint operator/ (plFixedPoint, plFixedPoint); // [tested]
115// plFixedPoint operator* (int, plFixedPoint); // [tested]
116// plFixedPoint operator* (plFixedPoint, int); // [tested]
117// plFixedPoint operator/ (plFixedPoint, int); // [tested]
118
119#include <Foundation/Math/Implementation/FixedPoint_inl.h>
Implements fixed point arithmetic for fractional values.
Definition FixedPoint.h:23
double ToDouble() const
Implicit conversion to double.
Definition FixedPoint_inl.h:39
bool operator==(const plFixedPoint< DecimalBits > &rhs) const
'Equality' comparison.
Definition FixedPoint.h:56
void operator/=(plInt32 rhs)
/= operator with integers (more efficient)
Definition FixedPoint.h:92
plFixedPoint(float fVal)
Construct from a float.
Definition FixedPoint.h:32
void operator+=(const plFixedPoint< DecimalBits > &rhs)
+= operator
Definition FixedPoint.h:77
bool operator<(const plFixedPoint< DecimalBits > &rhs) const
'Less than' comparison.
Definition FixedPoint.h:62
plFixedPoint(double fVal)
Construct from a double.
Definition FixedPoint.h:35
const plFixedPoint< DecimalBits > & operator=(plInt32 iVal)
Assignment from an integer.
Definition FixedPoint_inl.h:6
plInt32 GetRawValue() const
Returns the underlying integer value. Mostly useful for serialization (or tests).
Definition FixedPoint.h:95
plFixedPoint(plInt32 iIntVal)
Construct from an integer.
Definition FixedPoint.h:29
bool operator>=(const plFixedPoint< DecimalBits > &rhs) const
'Greater than or equal' comparison.
Definition FixedPoint.h:71
void operator*=(plInt32 rhs)
*= operator with integers (more efficient)
Definition FixedPoint.h:89
void operator*=(const plFixedPoint< DecimalBits > &rhs)
*= operator
Definition FixedPoint_inl.h:45
plInt32 ToInt() const
Implicit conversion to int (the fractional part is dropped).
Definition FixedPoint_inl.h:27
float ToFloat() const
Implicit conversion to float.
Definition FixedPoint_inl.h:33
bool operator<=(const plFixedPoint< DecimalBits > &rhs) const
'Less than or equal' comparison.
Definition FixedPoint.h:68
PL_ALWAYS_INLINE plFixedPoint()=default
Default constructor does not do any initialization.
void operator-=(const plFixedPoint< DecimalBits > &rhs)
-= operator
Definition FixedPoint.h:80
void SetRawValue(plInt32 iVal)
Sets the underlying integer value. Mostly useful for serialization (or tests).
Definition FixedPoint.h:98
void operator/=(const plFixedPoint< DecimalBits > &rhs)
/= operator
Definition FixedPoint_inl.h:69
bool operator!=(const plFixedPoint< DecimalBits > &rhs) const
'Inequality' comparison.
Definition FixedPoint.h:59
bool operator>(const plFixedPoint< DecimalBits > &rhs) const
'Greater than' comparison.
Definition FixedPoint.h:65