TDME2  1.9.200
Float.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tdme/tdme.h>
4 #include <tdme/math/Math.h>
6 
7 #include <cmath>
8 #include <limits>
9 #include <string>
10 #include <string_view>
11 
12 using std::isinf;
13 using std::isfinite;
14 using std::isfinite;
15 using std::isnan;
16 using std::numeric_limits;
17 using std::string;
18 using std::string_view;
19 
20 using tdme::math::Math;
21 
22 /**
23  * Float class
24  * @author Andreas Drewke
25  */
27 {
28 public:
29  static constexpr float MAX_VALUE { numeric_limits<float>::max() };
30  static constexpr float MIN_VALUE { -numeric_limits<float>::max() };
31  static constexpr float NAN_VALUE { numeric_limits<float>::quiet_NaN() };
32 
33  /**
34  * Check if given string is a float string
35  * @param str string
36  * @return given string is float
37  */
38  static bool is(const string& str);
39 
40  /**
41  * Check if given string is a float string
42  * @param str string
43  * @return given string is float
44  */
45  static bool viewIs(const string_view& str);
46 
47  /**
48  * Parse float
49  * @param str string
50  * @return float
51  */
52  static float parse(const string& str);
53 
54  /**
55  * Parse float
56  * @param str string
57  * @return float
58  */
59  static float viewParse(const string_view& str);
60 
61  /**
62  * Check if float is not a number
63  * @param value float value
64  * @return if value is not a number
65  */
66  inline static bool isNaN(float value) {
67  return isnan(value);
68  }
69 
70  /**
71  * Check if float is infinite
72  * @param value float value
73  * @return if value is infinite
74  */
75  inline static bool isInfinite(float value) {
76  return isinf(value);
77  }
78 
79  /**
80  * Check if float is infinite
81  * @param value float value
82  * @return if value is finite
83  */
84  inline static bool isFinite(float value) {
85  return isfinite(value);
86  }
87 
88  /**
89  * Interpolates between a and b by 0f<=t<=1f linearly
90  * @param a float a
91  * @param b float b
92  * @param t t
93  * @return interpolated float value
94  */
95  inline static float interpolateLinear(float a, float b, float t) {
96  return (b * t) + ((1.0f - t) * a);
97  }
98 
99  /**
100  * @return f1 and f2 are equals
101  */
102  inline static bool equals(float f1, float f2) {
103  return Math::abs(f2 - f1) < Math::EPSILON;
104  }
105 
106 };
Standard math functions.
Definition: Math.h:19
Float class.
Definition: Float.h:27
static bool isFinite(float value)
Check if float is infinite.
Definition: Float.h:84
static bool viewIs(const string_view &str)
Check if given string is a float string.
Definition: Float.cpp:39
static float interpolateLinear(float a, float b, float t)
Interpolates between a and b by 0f<=t<=1f linearly.
Definition: Float.h:95
static bool isInfinite(float value)
Check if float is infinite.
Definition: Float.h:75
static constexpr float MAX_VALUE
Definition: Float.h:29
static float viewParse(const string_view &str)
Parse float.
Definition: Float.cpp:70
static float parse(const string &str)
Parse float.
Definition: Float.cpp:53
static constexpr float NAN_VALUE
Definition: Float.h:31
static bool equals(float f1, float f2)
Definition: Float.h:102
static constexpr float MIN_VALUE
Definition: Float.h:30
static bool isNaN(float value)
Check if float is not a number.
Definition: Float.h:66
static bool is(const string &str)
Check if given string is a float string.
Definition: Float.cpp:26