SGL
gmath.h
1 /*
2  * File: gmath.h
3  * -------------
4  * This file exports several functions for working with graphical
5  * geometry along with the mathematical constants <code>PI</code>
6  * and <code>E</code>.
7  *
8  * @version 2018/11/22
9  * - added headless mode support
10  * - alphabetized methods
11  * @version 2018/09/25
12  * - added doc comments for new documentation generation
13  * @version 2017/12/12
14  * - added floatingPointEqual(a, b, tolerance)
15  * @version 2016/10/14
16  * - added floatingPointEqual method for comparing floats and doubles
17  */
18 
19 
20 #ifndef _gmath_h
21 #define _gmath_h
22 
23 #include <cmath>
24 #include <limits>
25 
26 #ifndef SPL_HEADLESS_MODE
27 #include "gtypes.h"
28 #endif // SPL_HEADLESS_MODE
29 
30 
35 extern const double PI;
36 
42 extern const double E;
43 
48 double cosDegrees(double angle);
49 
50 
57 int countDigits(int n, int base = 10);
58 
68 template<typename T>
69 bool floatingPointEqual(T f1, T f2, T tolerance) {
70  return (std::fabs(f1 - f2) <= tolerance);
71 }
72 
81 template<typename T>
82 bool floatingPointEqual(T f1, T f2) {
83  return floatingPointEqual(f1, f2, /* tolerance */ (T) std::numeric_limits<T>::epsilon() * std::fmax(fabs(f1), fabs(f2)));
84 }
85 
94 template<typename T>
95 bool floatingPointEqual(T f1, int f2) {
96  return floatingPointEqual(f1, (T) f2);
97 }
98 
107 template<typename T>
108 bool floatingPointEqual(int f1, T f2) {
109  return floatingPointEqual((T) f1, f2);
110 }
111 
120 template<typename T>
121 bool floatingPointEqual(T f1, long int f2) {
122  return floatingPointEqual(f1, (T) f2);
123 }
124 
133 template<typename T>
134 bool floatingPointEqual(long int f1, T f2) {
135  return floatingPointEqual((T) f1, f2);
136 }
137 
142 double sinDegrees(double angle);
143 
148 double tanDegrees(double angle);
149 
153 double toDegrees(double radians);
154 
158 double toRadians(double degrees);
159 
166 double vectorAngle(double x, double y);
167 
174 double vectorAngle(const GPoint& pt);
175 
179 double vectorDistance(double x, double y);
180 
184 double vectorDistance(const GPoint& pt);
185 
186 #endif // _gmath_h
This struct contains real-valued x and y fields.
Definition: gtypes.h:198