OpenXcom  1.0
Open-source clone of the original X-Com
fmath.h
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <algorithm>
21 #include <cfloat>
22 #define _USE_MATH_DEFINES
23 #include <cmath>
24 
25 #ifndef M_PI
26 #define M_PI 3.14159265358979323846
27 #define M_PI_2 1.57079632679489661923
28 #define M_PI_4 0.785398163397448309616
29 #endif
30 
31 inline bool AreSame(const float l, const float r) {
32  return std::fabs(l-r) <= FLT_EPSILON;
33 }
34 
35 inline bool AreSame(const double l, const double r) {
36  return std::fabs(l-r) <= DBL_EPSILON;
37 }
38 
39 template <class _Tx>
40 inline _Tx Round(const _Tx& x)
41 {
42  return x < static_cast<_Tx>(0) ? std::ceil(x - static_cast<_Tx>(0.5)) : std::floor(x + static_cast<_Tx>(0.5));
43 }
44 
45 template <class _Tx>
46 inline _Tx Sqr(const _Tx& x)
47 {
48  return x * x;
49 }
50 
51 template <class _Tx>
52 inline _Tx Sign(const _Tx& x)
53 {
54  return (_Tx(0) < x) - (x < _Tx(0));
55 }
56 
57 template <class _Tx>
58 inline _Tx Clamp(const _Tx& x, const _Tx& min, const _Tx& max)
59 {
60  return std::min(std::max(x, min), max);
61 }