NumCpp  2.1.0
A C++ implementation of the Python Numpy library
essentiallyEqual.hpp
Go to the documentation of this file.
1 #pragma once
30 
34 
35 #include <cmath>
36 #include <complex>
37 #include <string>
38 
39 namespace nc
40 {
41  namespace utils
42  {
43  //============================================================================
51  template<typename dtype,
52  enable_if_t<std::is_integral<dtype>::value, int> = 0>
53  bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
54  {
55  return inValue1 == inValue2;
56  }
57 
58  //============================================================================
67  template<typename dtype,
69  bool essentiallyEqual(dtype inValue1, dtype inValue2, dtype inEpsilon) noexcept
70  {
71  return std::abs(inValue1 - inValue2) <= ((std::abs(inValue1) > std::abs(inValue2) ?
72  std::abs(inValue2) : std::abs(inValue1)) * std::abs(inEpsilon));
73  }
74 
75  //============================================================================
83  template<typename dtype,
85  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2) noexcept
86  {
87  return inValue1 == inValue2;
88  }
89 
90  //============================================================================
99  template<typename dtype,
101  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2,
102  const std::complex<dtype>& inEpsilon) noexcept
103  {
104  return std::abs(inValue1 - inValue2) <= ((std::abs(inValue1) > std::abs(inValue2) ?
105  std::abs(inValue2) : std::abs(inValue1)) * std::abs(inEpsilon));
106  }
107 
108  //============================================================================
116  template<typename dtype,
118  bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
119  {
120  return essentiallyEqual(inValue1, inValue2, DtypeInfo<dtype>::epsilon());
121  }
122 
123  //============================================================================
131  template<typename dtype,
132  enable_if_t<std::is_floating_point<dtype>::value, int> = 0>
133  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2) noexcept
134  {
135  return essentiallyEqual(inValue1, inValue2, DtypeInfo<std::complex<dtype>>::epsilon());
136  }
137  } // namespace utils
138 } // namespace nc
nc::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTraits.hpp:41
StdComplexOperators.hpp
nc::utils::essentiallyEqual
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:53
nc
Definition: Coordinate.hpp:45
nc::DtypeInfo
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
DtypeInfo.hpp
TypeTraits.hpp
nc::abs
auto abs(dtype inValue) noexcept
Definition: abs.hpp:52