NRE_Math.hpp
Go to the documentation of this file.
1 
10  #pragma once
11 
12  #include <cmath>
13  #include <type_traits>
14 
19  namespace NRE {
24  namespace Math {
25 
30  template <class ...>
31  struct ArithmeticChecker : std::true_type {
32  };
37  template <class T1, class ... Tn>
38  struct ArithmeticChecker<T1, Tn...> : std::conjunction<std::conjunction<ArithmeticChecker<Tn...>>, std::is_arithmetic<T1>> {
39  };
44  template <class T1>
45  struct ArithmeticChecker<T1> : std::is_arithmetic<T1> {
46  };
47 
48  template <class ... Tn>
49  constexpr bool ArithmeticCheckerV = ArithmeticChecker<Tn...>::value;
54  template <class ... Tn>
55  using UseIfArithmetic = std::enable_if_t<ArithmeticCheckerV<Tn...>>;
56 
57  static constexpr long double EPSILON = 0.000001;
58  static constexpr long double PI = 3.141592653589793238462643383279502884L;
59 
66  template <class T, class K, typename std::enable_if_t< std::is_floating_point<std::common_type_t<T, K>>::value, int> = 0>
67  constexpr bool equal(T a, K b) {
68  return std::abs(static_cast <std::common_type_t<T, K>> (a) - static_cast <std::common_type_t<T, K>> (b)) < EPSILON;
69  }
70 
77  template <class T, class K, typename std::enable_if_t<!std::is_floating_point<std::common_type_t<T, K>>::value, int> = 0>
78  constexpr bool equal(T a, K b) {
79  return static_cast <std::common_type_t<T, K>> (a) == static_cast <std::common_type_t<T, K>> (b);
80  }
87  template <class T, class K>
88  constexpr bool almostEqual(T a, K b) {
89  return equal(a, b);
90  }
98  template <class T, typename = UseIfArithmetic<T>>
99  constexpr T lerp(T const& a, T const& b, float const& f) {
100  return a * (1.0f - f) + b * f;
101  }
102  }
103  }
std::enable_if_t< ArithmeticCheckerV< Tn... >> UseIfArithmetic
Definition: NRE_Math.hpp:55
constexpr bool equal(T a, K b)
Definition: NRE_Math.hpp:67
Math&#39;s API.
constexpr bool ArithmeticCheckerV
Definition: NRE_Math.hpp:49
Template structure allowing compile-time check on template parameters if they are arithmetic or not...
Definition: NRE_Math.hpp:31
The NearlyRealEngine&#39;s global namespace.
constexpr T lerp(T const &a, T const &b, float const &f)
Definition: NRE_Math.hpp:99
constexpr bool almostEqual(T a, K b)
Definition: NRE_Math.hpp:88