[nike] RelativeEpsilonEqual (#2636)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 12 Dec 2018 08:14:57 +0000 (17:14 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 12 Dec 2018 08:14:57 +0000 (17:14 +0900)
This commit introduce RelativeEpsilonEqual comparison algorithm.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/nike/include/nike/RelativeEpsilonEqual.h [new file with mode: 0644]
contrib/nike/src/RelativeEpsilonEqual.cpp [new file with mode: 0644]
contrib/nike/src/RelativeEpsilonEqual.test.cpp [new file with mode: 0644]

diff --git a/contrib/nike/include/nike/RelativeEpsilonEqual.h b/contrib/nike/include/nike/RelativeEpsilonEqual.h
new file mode 100644 (file)
index 0000000..1b4c04a
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NIKE_RELATIVE_EPSILON_EQUAL_H__
+#define __NIKE_RELATIVE_EPSILON_EQUAL_H__
+
+namespace nike
+{
+
+class RelativeEpsilonEqualFunctor
+{
+public:
+  friend RelativeEpsilonEqualFunctor relative_epsilon_equal(unsigned);
+
+private:
+  RelativeEpsilonEqualFunctor(unsigned tolerance) : _tolerance{tolerance}
+  {
+    // DO NOTHING
+  }
+
+public:
+  bool operator()(float lhs, float rhs) const;
+
+private:
+  unsigned _tolerance;
+};
+
+/**
+ * @note RelativeEpsilonEqualFunctor uses its own rule for NaN values.
+ *
+ * For example, "NAN == NAN" is false but "relative_epsilon_equal(1)(NAN, NAN)" is true.
+ */
+RelativeEpsilonEqualFunctor relative_epsilon_equal(unsigned tolerance);
+
+} // namespace nike
+
+#endif // __NIKE_RELATIVE_EPSILON_EQUAL_H__
diff --git a/contrib/nike/src/RelativeEpsilonEqual.cpp b/contrib/nike/src/RelativeEpsilonEqual.cpp
new file mode 100644 (file)
index 0000000..ac8d2ad
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "nike/RelativeEpsilonEqual.h"
+
+#include <cmath>
+#include <cfloat>
+
+#include <algorithm>
+
+namespace nike
+{
+
+bool RelativeEpsilonEqualFunctor::operator()(float lhs, float rhs) const
+{
+  if (std::isnan(lhs) && std::isnan(rhs))
+  {
+    return true;
+  }
+
+  // TODO How to handle sign difference?
+  auto const delta = std::fabs(lhs - rhs);
+  auto const max = std::max(std::fabs(lhs), std::fabs(rhs));
+
+  return delta <= (max * FLT_EPSILON * _tolerance);
+}
+
+RelativeEpsilonEqualFunctor relative_epsilon_equal(unsigned tolerance)
+{
+  return RelativeEpsilonEqualFunctor{tolerance};
+}
+
+} // namespace nike
diff --git a/contrib/nike/src/RelativeEpsilonEqual.test.cpp b/contrib/nike/src/RelativeEpsilonEqual.test.cpp
new file mode 100644 (file)
index 0000000..7c7d4cf
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "nike/RelativeEpsilonEqual.h"
+
+#include <cmath> // For "NAN"
+
+#include <gtest/gtest.h>
+
+TEST(RelativeEpsilonEqualTest, tolerance_1)
+{
+  auto equal = nike::relative_epsilon_equal(1);
+
+  ASSERT_TRUE(equal(NAN, NAN));
+  ASSERT_TRUE(equal(1.0f, 1.0f));
+  ASSERT_FALSE(equal(1.0f, 2.0f));
+}
+
+TEST(RelativeEpsilonEqualTest, tolerance_2)
+{
+  auto equal = nike::relative_epsilon_equal(2);
+
+  ASSERT_TRUE(equal(1.0f, 1.0f + FLT_EPSILON));
+  ASSERT_FALSE(equal(1.0f, 1.0f + 3 * FLT_EPSILON));
+}