NumCpp  2.7.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 
33 
34 #include <cmath>
35 #include <complex>
36 #include <string>
37 
38 namespace nc
39 {
40  namespace utils
41  {
42  //============================================================================
50  template<typename dtype,
51  enable_if_t<std::is_integral<dtype>::value, int> = 0>
52  bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
53  {
54  return inValue1 == inValue2;
55  }
56 
57  //============================================================================
66  template<typename dtype,
68  bool essentiallyEqual(dtype inValue1, dtype inValue2, dtype inEpsilon) noexcept
69  {
70  return std::abs(inValue1 - inValue2) <= ((std::abs(inValue1) > std::abs(inValue2) ?
71  std::abs(inValue2) : std::abs(inValue1)) * std::abs(inEpsilon));
72  }
73 
74  //============================================================================
82  template<typename dtype,
84  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2) noexcept
85  {
86  return inValue1 == inValue2;
87  }
88 
89  //============================================================================
98  template<typename dtype,
100  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2,
101  const std::complex<dtype>& inEpsilon) noexcept
102  {
103  return std::abs(inValue1 - inValue2) <= ((std::abs(inValue1) > std::abs(inValue2) ?
104  std::abs(inValue2) : std::abs(inValue1)) * std::abs(inEpsilon));
105  }
106 
107  //============================================================================
115  template<typename dtype,
117  bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
118  {
119  return essentiallyEqual(inValue1, inValue2, DtypeInfo<dtype>::epsilon());
120  }
121 
122  //============================================================================
130  template<typename dtype,
131  enable_if_t<std::is_floating_point<dtype>::value, int> = 0>
132  bool essentiallyEqual(const std::complex<dtype>& inValue1, const std::complex<dtype>& inValue2) noexcept
133  {
134  return essentiallyEqual(inValue1, inValue2, DtypeInfo<std::complex<dtype>>::epsilon());
135  }
136  } // namespace utils
137 } // namespace nc
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:52
Definition: Coordinate.hpp:45
auto abs(dtype inValue) noexcept
Definition: abs.hpp:49
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTraits.hpp:40