Plasma Engine  2.0
Loading...
Searching...
No Matches
MathFloat_inl.h
1#pragma once
2
3#include <algorithm>
4
5namespace plMath
6{
7 PL_ALWAYS_INLINE bool IsFinite(float value)
8 {
9 // Check the 8 exponent bits.
10 // NAN -> (exponent = all 1, mantissa = non-zero)
11 // INF -> (exponent = all 1, mantissa = zero)
12
13 plIntFloatUnion i2f(value);
14 return ((i2f.i & 0x7f800000u) != 0x7f800000u);
15 }
16
17 PL_ALWAYS_INLINE bool IsNaN(float value)
18 {
19 // Check the 8 exponent bits.
20 // NAN -> (exponent = all 1, mantissa = non-zero)
21 // INF -> (exponent = all 1, mantissa = zero)
22
23 plIntFloatUnion i2f(value);
24 return (((i2f.i & 0x7f800000u) == 0x7f800000u) && ((i2f.i & 0x7FFFFFu) != 0));
25 }
26
27 PL_ALWAYS_INLINE float Floor(float f)
28 {
29 return floorf(f);
30 }
31
32 PL_ALWAYS_INLINE float Ceil(float f)
33 {
34 return ceilf(f);
35 }
36
37 PL_ALWAYS_INLINE float Round(float f)
38 {
39 return Floor(f + 0.5f);
40 }
41
42 PL_ALWAYS_INLINE float RoundToMultiple(float f, float fMultiple)
43 {
44 return Round(f / fMultiple) * fMultiple;
45 }
46
47
48 inline float RoundDown(float f, float fMultiple)
49 {
50 float fDivides = f / fMultiple;
51 float fFactor = Floor(fDivides);
52 return fFactor * fMultiple;
53 }
54
55 inline float RoundUp(float f, float fMultiple)
56 {
57 float fDivides = f / fMultiple;
58 float fFactor = Ceil(fDivides);
59 return fFactor * fMultiple;
60 }
61
62 PL_ALWAYS_INLINE float Sin(plAngle a)
63 {
64 return sinf(a.GetRadian());
65 }
66
67 PL_ALWAYS_INLINE float Cos(plAngle a)
68 {
69 return cosf(a.GetRadian());
70 }
71
72 PL_ALWAYS_INLINE float Tan(plAngle a)
73 {
74 return tanf(a.GetRadian());
75 }
76
77 PL_ALWAYS_INLINE plAngle ASin(float f)
78 {
79 return plAngle::MakeFromRadian(asinf(f));
80 }
81
82 PL_ALWAYS_INLINE plAngle ACos(float f)
83 {
84 return plAngle::MakeFromRadian(acosf(f));
85 }
86
87 PL_ALWAYS_INLINE plAngle ATan(float f)
88 {
89 return plAngle::MakeFromRadian(atanf(f));
90 }
91
92 PL_ALWAYS_INLINE plAngle ATan2(float y, float x)
93 {
94 return plAngle::MakeFromRadian(atan2f(y, x));
95 }
96
97 PL_ALWAYS_INLINE float Exp(float f)
98 {
99 return expf(f);
100 }
101
102 PL_ALWAYS_INLINE float Ln(float f)
103 {
104 return logf(f);
105 }
106
107 PL_ALWAYS_INLINE float Log2(float f)
108 {
109 return log2f(f);
110 }
111
112 PL_ALWAYS_INLINE float Log10(float f)
113 {
114 return log10f(f);
115 }
116
117 PL_ALWAYS_INLINE float Log(float fBase, float f)
118 {
119 return log10f(f) / log10f(fBase);
120 }
121
122 PL_ALWAYS_INLINE float Pow2(float f)
123 {
124 return exp2f(f);
125 }
126
127 PL_ALWAYS_INLINE float Pow(float fBase, float fExp)
128 {
129 return powf(fBase, fExp);
130 }
131
132 PL_ALWAYS_INLINE float Root(float f, float fNthRoot)
133 {
134 return powf(f, 1.0f / fNthRoot);
135 }
136
137 PL_ALWAYS_INLINE float Sqrt(float f)
138 {
139 return sqrtf(f);
140 }
141
142 PL_ALWAYS_INLINE float Mod(float f, float fDiv)
143 {
144 return fmodf(f, fDiv);
145 }
146} // namespace plMath
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
static constexpr plAngle MakeFromRadian(float fRadian)
Creates an instance of plAngle that was initialized from radian. (No need for any conversion)
Definition Angle_inl.h:38
constexpr float GetRadian() const
Returns the radian value. (No need for any conversion)
Definition Angle_inl.h:48
This namespace provides common math-functionality as functions.
Definition Constants.h:6
double RoundDown(double f, double fMultiple)
Returns a multiple of fMultiple that is smaller than f.
Definition MathDouble_inl.h:40
PL_ALWAYS_INLINE plAngle ASin(float f)
Returns the arcus sinus of f.
Definition MathFloat_inl.h:77
PL_ALWAYS_INLINE plAngle ATan(float f)
Returns the arcus tangent of f.
Definition MathFloat_inl.h:87
PL_ALWAYS_INLINE plAngle ACos(float f)
Returns the arcus cosinus of f.
Definition MathFloat_inl.h:82
PL_ALWAYS_INLINE double RoundToMultiple(double f, double fMultiple)
Rounds f to the closest value of multiple.
Definition MathDouble_inl.h:54
PL_ALWAYS_INLINE double Round(double f)
Rounds f to the next integer.
Definition MathDouble_inl.h:35
PL_ALWAYS_INLINE float Sin(plAngle a)
***** Trigonometric Functions *****
Definition MathFloat_inl.h:62
PL_ALWAYS_INLINE plAngle ATan2(float y, float x)
Returns the atan2 of x and y.
Definition MathFloat_inl.h:92
double RoundUp(double f, double fMultiple)
Returns a multiple of fMultiple that is larger than f.
Definition MathDouble_inl.h:47
PL_ALWAYS_INLINE double Mod(double f, double fDiv)
Returns "value mod div" for doubles. This also works with negative numbers, both for value and for di...
Definition MathDouble_inl.h:104
PL_ALWAYS_INLINE float Tan(plAngle a)
Takes an angle, returns its tangent.
Definition MathFloat_inl.h:72
PL_ALWAYS_INLINE double Sqrt(double f)
Returns the square root of f.
Definition MathDouble_inl.h:99
PL_ALWAYS_INLINE float Cos(plAngle a)
Takes an angle, returns its cosine.
Definition MathFloat_inl.h:67
Simple helper union to store ints and floats to modify their bit patterns.
Definition Declarations.h:18