NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
essentiallyEqual.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 #include <complex>
32 #include <string>
33 
37 
38 namespace nc
39 {
40  namespace utils
41  {
42  //============================================================================
50  template<typename dtype, enable_if_t<std::is_integral<dtype>::value, int> = 0>
51  bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
52  {
53  return inValue1 == inValue2;
54  }
55 
56  //============================================================================
65  template<typename dtype, enable_if_t<std::is_floating_point<dtype>::value, int> = 0>
66  bool essentiallyEqual(dtype inValue1, dtype inValue2, dtype inEpsilon) noexcept
67  {
68  const auto absValue1 = std::abs(inValue1);
69  const auto absValue2 = std::abs(inValue2);
70  return std::abs(inValue1 - inValue2) <=
71  ((absValue1 > absValue2 ? absValue2 : absValue1) * std::abs(inEpsilon));
72  }
73 
74  //============================================================================
82  template<typename dtype, enable_if_t<std::is_integral<dtype>::value, int> = 0>
83  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2) noexcept
84  {
85  return inValue1 == inValue2;
86  }
87 
88  //============================================================================
97  template<typename dtype, enable_if_t<std::is_floating_point<dtype>::value, int> = 0>
98  bool essentiallyEqual(const std::complex<dtype>& inValue1,
99  const std::complex<dtype>& inValue2,
100  const std::complex<dtype>& inEpsilon) noexcept
101  {
102  const auto absValue1 = std::abs(inValue1);
103  const auto absValue2 = std::abs(inValue2);
104  return std::abs(inValue1 - inValue2) <=
105  ((absValue1 > absValue2 ? absValue2 : absValue1) * std::abs(inEpsilon));
106  }
107 
108  //============================================================================
116  template<typename dtype, enable_if_t<std::is_floating_point<dtype>::value, int> = 0>
117  bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
118  {
119  return essentiallyEqual(inValue1, inValue2, DtypeInfo<dtype>::epsilon());
120  }
121 
122  //============================================================================
130  template<typename dtype, enable_if_t<std::is_floating_point<dtype>::value, int> = 0>
131  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2) noexcept
132  {
133  return essentiallyEqual(inValue1, inValue2, DtypeInfo<std::complex<dtype>>::epsilon());
134  }
135  } // namespace utils
136 } // namespace nc
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:51
Definition: Coordinate.hpp:45
auto abs(dtype inValue) noexcept
Definition: abs.hpp:49