This commit introduce RelativeEpsilonEqual comparison algorithm.
Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
--- /dev/null
+/*
+ * 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__
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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));
+}