TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_math.hpp
1 
9 #ifndef TurtleBrains_Math_hpp
10 #define TurtleBrains_Math_hpp
11 
12 #include <turtle_brains/core/tb_configuration.hpp>
13 #include <turtle_brains/math/tb_constants.hpp>
14 
15 #include <cmath>
16 
17 namespace TurtleBrains
18 {
19  namespace Math
20  {
29  template<typename Type> constexpr const Type& Maximum(const Type& leftValue, const Type& rightValue) noexcept
30  {
31  return (leftValue < rightValue) ? rightValue : leftValue;
32  }
33 
42  template<typename Type> constexpr const Type& Minimum(const Type& leftValue, const Type& rightValue) noexcept
43  {
44  return (leftValue < rightValue) ? leftValue : rightValue;
45  }
46 
56  template<typename Type> bool IsEqual(const Type& leftValue, const Type& rightValue, const Type tolerance = tbMath::kTolerance)
57  {
58  // 2024-03-22: While digging into Godot 4.2.1 for porting the SnailedIt prototype, there were some interesting
59  // details found in the implementation of core/math/math_funcs.h is_equal_approx. First they have a specific
60  // check for == directly, which seems to be required to handle infinity values. Then they change tolerance
61  // based on epsilon, (here I renamed epsilon to tolerance), which I believe is to handle greater and greater
62  // precision losses for larger floating point numbers.
63  return std::fabs(leftValue - rightValue) <= Maximum<Type>(Type(tolerance), Type(tolerance * std::fabs(leftValue)));
64  }
65 
74  template<typename Type> bool IsZero(const Type& value, const Type tolerance = tbMath::kTolerance)
75  {
76  return std::fabs(value) <= tolerance;
77  }
78 
91  template<typename Type> constexpr const Type& Clamp(const Type& value, const Type& minimumValue, const Type& maximumValue) noexcept
92  {
93  return (value < minimumValue) ? minimumValue : (maximumValue < value) ? maximumValue : value;
94  }
95 
99  template<typename Type> Type Sign(const Type& value) noexcept
100  {
101  return IsZero(value) ? Type(0) : (std::signbit(value) ? Type(-1) : Type(1));
102  }
103 
104  }; /* namespace Math */
105 }; /* namespace TurtleBrains */
106 
107 namespace tbMath = TurtleBrains::Math;
108 
109 #endif /* TurtleBrains_Math_hpp */
Contains objects and functions for dealing with Vector and Matrix math.
bool IsZero(const Type &value, const Type tolerance=tbMath::kTolerance)
Definition: tb_math.hpp:74
bool IsEqual(const Type &leftValue, const Type &rightValue, const Type tolerance=tbMath::kTolerance)
Definition: tb_math.hpp:56
Type Sign(const Type &value) noexcept
Definition: tb_math.hpp:99
constexpr const Type & Minimum(const Type &leftValue, const Type &rightValue) noexcept
Definition: tb_math.hpp:42
constexpr const Type & Clamp(const Type &value, const Type &minimumValue, const Type &maximumValue) noexcept
Definition: tb_math.hpp:91
constexpr const Type & Maximum(const Type &leftValue, const Type &rightValue) noexcept
Definition: tb_math.hpp:29
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:22