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 // Float operations
32 
33 inline bool AreSame(float l, float r)
34 {
35  return std::fabs(l-r) <= FLT_EPSILON;
36 }
37 
38 inline bool AreSame(double l, double r)
39 {
40  return std::fabs(l-r) <= DBL_EPSILON;
41 }
42 
43 inline float Round(float x)
44 {
45  return x < 0.0f ? std::ceil(x - 0.5f) : std::floor(x + 0.5f);
46 }
47 
48 inline double Round(double x)
49 {
50  return x < 0.0 ? std::ceil(x - 0.5) : std::floor(x + 0.5);
51 }
52 
53 // Number operations
54 
55 template <class _Tx>
56 inline _Tx Sqr(const _Tx& x)
57 {
58  return x * x;
59 }
60 
61 template <class _Tx>
62 inline _Tx Sign(const _Tx& x)
63 {
64  return (_Tx(0) < x) - (x < _Tx(0));
65 }
66 
67 template <class _Tx>
68 inline _Tx Clamp(const _Tx& x, const _Tx& min, const _Tx& max)
69 {
70  return std::min(std::max(x, min), max);
71 }
72 
73 // Degree operations
74 
75 inline double Deg2Rad(double deg)
76 {
77  return deg * M_PI / 180.0;
78 }
79 
80 inline double Rad2Deg(double rad)
81 {
82  return rad / M_PI * 180.0;
83 }
84 
85 inline double Xcom2Rad(int deg)
86 {
87  return deg * 0.125 * M_PI / 180.0;
88 }
89 
90 inline double Nautical(double x)
91 {
92  return x * (1 / 60.0) * (M_PI / 180.0);
93 }