UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Numbers.hxx
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2020 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: Numbers.hxx
22//
23
24namespace Utils {
25
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 using std::numeric_limits;
28 using std::string;
29 using std::sqrt;
30 using std::fpclassify;
31 using std::floor;
32 #endif
33
34 /*
35 // ____ _ _
36 // / ___|___ _ __ ___| |_ __ _ _ __ | |_ ___
37 // | | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
38 // | |__| (_) | | | \__ \ || (_| | | | | |_\__ \
39 // \____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
40 */
41
46 template <typename T> T NaN();
47
52 template <typename T> T Inf();
53
58 template <typename T> T machine_eps();
59
64 template <typename T> T sqrt_machine_eps();
65
70 template <typename T> T maximum_value();
71
76 template <typename T> T minimum_value();
77
78 #ifndef DOXYGEN_SHOULD_SKIP_THIS
79 template <> inline float NaN() { return numeric_limits<float>::quiet_NaN(); }
80 template <> inline double NaN() { return numeric_limits<double>::quiet_NaN(); }
81
82 template <> inline float Inf() { return numeric_limits<float>::infinity(); }
83 template <> inline double Inf() { return numeric_limits<double>::infinity(); }
84
85 template <> inline float machine_eps() { return numeric_limits<float>::epsilon(); }
86 template <> inline double machine_eps() { return numeric_limits<double>::epsilon(); }
87
88 template <> inline float sqrt_machine_eps() { return sqrt(numeric_limits<float>::epsilon()); }
89 template <> inline double sqrt_machine_eps() { return sqrt(numeric_limits<double>::epsilon()); }
90
91 template <> inline float maximum_value() { return sqrt(numeric_limits<float>::max()); }
92 template <> inline double maximum_value() { return sqrt(numeric_limits<double>::max()); }
93
94 template <> inline float minimum_value() { return sqrt(numeric_limits<float>::min()); }
95 template <> inline double minimum_value() { return sqrt(numeric_limits<double>::min()); }
96 #endif
97
102 static inline bool is_zero(double x) { return FP_ZERO == fpclassify(x); }
103
108 static inline bool is_zero(float x) { return FP_ZERO == fpclassify(x); }
109
114 static inline bool is_NaN(double x) { return std::isnan(x); }
115
120 static inline bool is_NaN(float x) { return std::isnan(x); }
121
126 static inline bool is_infinite(double x) { return std::isinf(x); }
127
132 static inline bool is_infinite(float x) { return std::isinf(x); }
133
138 static inline bool is_normal(double x) { return std::isnormal(x); }
139
144 static inline bool is_normal(float x) { return std::isnormal(x); }
145
150 static inline bool is_finite(double x) { return std::isfinite(x); }
151
156 static inline bool is_finite(float x) { return std::isfinite(x); }
157
162 static inline bool is_integer(double x) { return is_zero(x - floor(x)); }
163
168 static inline bool is_integer(float x) { return is_zero(x - floor(x)); }
169
174 static inline bool is_unsigned(double x) { return is_integer(x) && x >= 0; }
175
180 static inline bool is_unsigned(float x) { return is_integer(x) && x >= 0; }
181
182 //============================================================================
183
189 bool found_NaN(double const* pv, int DIM);
190
196 bool found_NaN(float const* pv, int DIM);
197
205 void check_NaN( double const* pv, string_view v_name, int DIM, int line, string_view file );
206
214 void check_NaN( float const* pv, string_view v_name, int DIM, int line, string_view file );
215
216 //============================================================================
217
219 static double const m_e = 2.718281828459045235360287471352662497757;
220
222 static double const m_pi = 3.141592653589793238462643383279502884197;
223
225 static double const m_2pi = 6.283185307179586476925286766559005768394;
226
228 static double const m_pi_2 = 1.570796326794896619231321691639751442098;
229
231 static double const m_pi_4 = 0.7853981633974483096156608458198757210492;
232
234 static double const m_1_pi = 0.3183098861837906715377675267450287240689;
235
237 static double const m_2_pi = 0.6366197723675813430755350534900574481378;
238
240 static double const m_sqrtpi = 1.772453850905516027298167483341145182798;
241
243 static double const m_2_sqrtpi = 1.128379167095512573896158903121545171688;
244
246 static double const m_sqrt2 = 1.414213562373095048801688724209698078570;
247
249 static double const m_1_sqrt2 = 0.7071067811865475244008443621048490392850;
250
251 #ifdef UTILS_OLD_CAMELCASE
257 template <typename T> inline T machineEps() { return machine_eps<T>(); }
258
264 template <typename T> inline T sqrtMachineEps() { return sqrt_machine_eps<T>(); }
265
271 template <typename T> inline T maximumValue() { return maximum_value<T>(); }
272
278 template <typename T> inline T minimumValue() { return minimum_value<T>(); }
279
285 static inline bool isZero(double x) { return is_zero(x); }
286
292 static inline bool isZero(float x) { return is_zero(x); }
293
299 static inline bool isInfinite(double x) { return is_infinite(x); }
300
306 static inline bool isInfinite(float x) { return is_infinite(x); }
307
313 static inline bool isNaN(double x) { return is_NaN(x); }
314
320 static inline bool isNaN(float x) { return is_NaN(x); }
321
327 static inline bool isFinite(double x) { return is_finite(x); }
328
334 static inline bool isFinite(float x) { return is_finite(x); }
335
341 static inline bool isRegular(double x) { return is_finite(x); }
342
348 static inline bool isRegular(float x) { return is_finite(x); }
349
355 static inline bool isInteger(double x) { return is_integer(x); }
356
362 static inline bool isInteger(float x) { return is_integer(x); }
363
369 static inline bool isUnsigned(double x) { return is_unsigned(x); }
370
376 static inline bool isUnsigned(float x) { return is_unsigned(x); }
377
384 static inline bool foundNaN(double const* pv, int DIM) { return found_NaN(pv, DIM); }
385
392 static inline bool foundNaN(float const* pv, int DIM) { return found_NaN(pv, DIM); }
393
402 static inline void checkNaN( double const* pv, string_view v_name, int DIM, int line, string_view file ) {
403 check_NaN(pv, v_name, DIM, line, file);
404 }
405
414 static inline void checkNaN( float const* pv, string_view v_name, int DIM, int line, string_view file ) {
415 check_NaN(pv, v_name, DIM, line, file);
416 }
417
418 #endif
419}
420
421//
422// eof: Number.hxx
423//
Definition SystemUtils.cc:39
bool found_NaN(double const *pv, int DIM)
T machine_eps()
T maximum_value()
T NaN()
T minimum_value()
void check_NaN(double const *pv, string_view v_name, int DIM, int line, string_view file)
T sqrt_machine_eps()
T Inf()