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