22 #ifndef FIFE_UTIL_FIFE_MATH_H
23 #define FIFE_UTIL_FIFE_MATH_H
40 #define ABS(x) ((x)<0?-(x):(x))
45 #if defined( WIN32 ) && defined( _MSC_VER )
46 inline double round(
const double x) {
47 return x < 0.0 ? ceil(x - 0.5) : floor(x + 0.5);
53 static const float FLT_STD_EPSILON = std::numeric_limits<float>::epsilon();
54 static const float FLT_STD_MAX = std::numeric_limits<float>::max();
55 static const float FLT_ZERO_TOLERANCE = 1e-06f;
56 static const float FLT_PI = 4.0f*std::atan(1.0f);
57 static const float FLT_TWO_PI = 2.0f*FLT_PI;
58 static const float FLT_HALF_PI = 0.5f*FLT_PI;
59 static const float FLT_INVERSE_PI = 1.0f/FLT_PI;
60 static const float FLT_INVERSE_TWO_PI = 1.0f/FLT_TWO_PI;
61 static const float FLT_DEG_TO_RAD = FLT_PI/180.0f;
62 static const float FLT_RAD_TO_DEG = 180.0f/FLT_PI;
63 static const float FLT_LOG_2 = std::log(2.0f);
64 static const float FLT_LOG_10 = std::log(10.0f);
65 static const float FLT_INV_LOG_2 = 1.0f/std::log(2.0f);
66 static const float FLT_INV_LOG_10 = 1.0f/std::log(10.0f);
68 static const double DBL_STD_EPSILON = std::numeric_limits<double>::epsilon();
69 static const double DBL_STD_MAX = std::numeric_limits<double>::max();
70 static const double DBL_ZERO_TOLERANCE = 1e-08;
71 static const double DBL_PI = 4.0*std::atan(1.0);
72 static const double DBL_TWO_PI = 2.0*DBL_PI;
73 static const double DBL_HALF_PI = 0.5*DBL_PI;
74 static const double DBL_INVERSE_PI = 1.0/DBL_PI;
75 static const double DBL_INVERSE_TWO_PI = 1.0/DBL_TWO_PI;
76 static const double DBL_DEG_TO_RAD = DBL_PI/180.0;
77 static const double DBL_RAD_TO_DEG = 180.0f/DBL_PI;
78 static const double DBL_LOG_2 = std::log(2.0);
79 static const double DBL_LOG_10 = std::log(10.0);
80 static const double DBL_INV_LOG_2 = 1.0/std::log(2.0);
81 static const double DBL_INV_LOG_10 = 1.0/std::log(10.0);
84 struct float_traits { };
87 struct float_traits<float> {
88 typedef float float_type;
89 static inline float_type epsilon() {
return FLT_STD_EPSILON; }
90 static inline float_type zeroTolerance() {
return FLT_ZERO_TOLERANCE; }
91 static inline float_type max() {
return FLT_STD_MAX; }
92 static inline float_type pi() {
return FLT_PI; }
93 static inline float_type twoPi() {
return FLT_TWO_PI; }
94 static inline float_type halfPi() {
return FLT_HALF_PI; }
95 static inline float_type inversePi() {
return FLT_INVERSE_PI; }
96 static inline float_type inverseTwoPi() {
return FLT_INVERSE_TWO_PI; }
97 static inline float_type degToRad() {
return FLT_DEG_TO_RAD; }
98 static inline float_type radToDeg() {
return FLT_RAD_TO_DEG; }
99 static inline float_type log2() {
return FLT_LOG_2; }
100 static inline float_type log10() {
return FLT_LOG_10; }
101 static inline float_type invLog2() {
return FLT_INV_LOG_2; }
102 static inline float_type invLog10() {
return FLT_INV_LOG_10; }
106 struct float_traits<double> {
107 typedef double float_type;
108 static inline float_type epsilon() {
return DBL_STD_EPSILON; }
109 static inline float_type zeroTolerance() {
return DBL_ZERO_TOLERANCE; }
110 static inline float_type max() {
return DBL_STD_MAX; }
111 static inline float_type pi() {
return DBL_PI; }
112 static inline float_type twoPi() {
return DBL_TWO_PI; }
113 static inline float_type halfPi() {
return DBL_HALF_PI; }
114 static inline float_type inversePi() {
return DBL_INVERSE_PI; }
115 static inline float_type inverseTwoPi() {
return DBL_INVERSE_TWO_PI; }
116 static inline float_type degToRad() {
return DBL_DEG_TO_RAD; }
117 static inline float_type radToDeg() {
return DBL_RAD_TO_DEG; }
118 static inline float_type log2() {
return DBL_LOG_2; }
119 static inline float_type log10() {
return DBL_LOG_10; }
120 static inline float_type invLog2() {
return DBL_INV_LOG_2; }
121 static inline float_type invLog10() {
return DBL_INV_LOG_10; }
124 template <
typename T>
128 typedef float_traits<num_type> traits_type;
130 static inline num_type epsilon() {
return traits_type::epsilon(); }
131 static inline num_type zeroTolerance() {
return traits_type::zeroTolerance(); }
132 static inline num_type max() {
return traits_type::max(); }
133 static inline num_type pi() {
return traits_type::pi(); }
134 static inline num_type twoPi() {
return traits_type::twoPi(); }
135 static inline num_type halfPi() {
return traits_type::halfPi(); }
136 static inline num_type inversePi() {
return traits_type::inversePi(); }
137 static inline num_type inverseTwoPi() {
return traits_type::inverseTwoPi(); }
138 static inline num_type degToRad() {
return traits_type::degToRad(); }
139 static inline num_type radToDeg() {
return traits_type::radToDeg(); }
140 static inline num_type log2() {
return traits_type::log2(); }
141 static inline num_type log10() {
return traits_type::log10(); }
142 static inline num_type invLog2() {
return traits_type::invLog2(); }
143 static inline num_type invLog10() {
return traits_type::invLog10(); }
145 static T ACos(T _val);
146 static T ASin(T _val);
147 static T ATan(T _val);
148 static T ATan2(T _x, T _y);
149 static T Ceil(T _val);
150 static T Cos(T _val);
151 static T Exp(T _val);
152 static T FAbs(T _val);
153 static T Floor(T _val);
154 static T FMod (T _x, T _y);
155 static T InvSqrt(T _val);
156 static T Log(T _val);
157 static T Log2(T _val);
158 static T Log10(T _val);
159 static T Pow(T _base, T _exponent);
160 static T Sin(T _val);
161 static T Sqr(T _val);
162 static T Sqrt(T _val);
163 static T Tan(T _val);
166 typedef Math<float> Mathf;
167 typedef Math<double> Mathd;
170 inline T Math<T>::ACos(T _val) {
171 if (-static_cast<T>(1) < _val) {
172 if (_val < static_cast<T>(1)) {
173 return static_cast<T
>(std::acos(_val));
176 return static_cast<T
>(0);
185 inline T Math<T>::ASin(T _val) {
186 if (-static_cast<T>(1) < _val) {
187 if (_val < static_cast<T>(1)) {
188 return static_cast<T
>(std::asin(_val));
200 inline T Math<T>::ATan(T _val) {
201 return static_cast<T
>(std::atan(_val));
205 inline T Math<T>::ATan2(T _x, T _y) {
206 return static_cast<T
>(std::atan2(_x, _y));
210 inline T Math<T>::Ceil(T _val) {
211 return static_cast<T
>(std::ceil(_val));
215 inline T Math<T>::Cos(T _val) {
216 return static_cast<T
>(std::cos(_val));
220 inline T Math<T>::Exp(T _val){
221 return static_cast<T
>(std::exp(_val));
225 inline T Math<T>::FAbs(T _val) {
226 return static_cast<T
>(std::fabs(_val));
230 inline T Math<T>::Floor(T _val) {
231 return static_cast<T
>(std::floor(_val));
235 inline T Math<T>::FMod(T _x, T _y) {
236 return static_cast<T
>(std::fmod(_x, _y));
240 inline T Math<T>::InvSqrt(T _val) {
241 return static_cast<T
>(1/std::sqrt(_val));
245 inline T Math<T>::Log(T _val) {
246 return static_cast<T
>(std::log(_val));
250 inline T Math<T>::Log2(T _val) {
251 return invLog2() *
static_cast<T
>(std::log(_val));
254 inline T Math<T>::Log10(T _val) {
256 return invLog10() *
static_cast<T
>(std::log(_val));
260 inline T Math<T>::Pow(T _base, T _exponent) {
261 return static_cast<T
>(std::pow(_base, _exponent));
265 inline T Math<T>::Sin(T _val) {
266 return static_cast<T
>(std::sin(_val));
270 inline T Math<T>::Sqr(T _val) {
275 inline T Math<T>::Sqrt(T _val) {
276 return static_cast<T
>(std::sqrt(_val));
280 inline T Math<T>::Tan(T _val) {
281 return static_cast<T
>(std::tan(_val));
298 #endif // FIFE_UTIL_FIFE_MATH_H