Skip to content

Commit

Permalink
Fix logic when two different exponents are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
pitrou committed Dec 3, 2024
1 parent 8dba1e1 commit 65eb9fd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cpp/src/arrow/testing/gtest_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ TEST(TestWithinUlp, Double) {
CheckWithinUlp(1.0, 1.0000000000000007, 3);
CheckNotWithinUlp(1.0, 1.0000000000000007, 2);
CheckNotWithinUlp(1.0, 1.0000000000000007, 1);
// left and right have a different exponent but are still very close
CheckWithinUlp(1.0, 0.9999999999999999, 1);
CheckWithinUlp(1.0, 0.9999999999999988, 11);
CheckNotWithinUlp(1.0, 0.9999999999999988, 10);

CheckWithinUlp(123.4567, 123.45670000000015, 11);
CheckNotWithinUlp(123.4567, 123.45670000000015, 10);
Expand All @@ -253,6 +257,10 @@ TEST(TestWithinUlp, Float) {
CheckWithinUlp(1.0f, 1.0000001f, 1);
CheckWithinUlp(1.0f, 1.0000013f, 11);
CheckNotWithinUlp(1.0f, 1.0000013f, 10);
// left and right have a different exponent but are still very close
CheckWithinUlp(1.0f, 0.99999994f, 1);
CheckWithinUlp(1.0f, 0.99999934f, 11);
CheckNotWithinUlp(1.0f, 0.99999934f, 10);

CheckWithinUlp(123.456f, 123.456085f, 11);
CheckNotWithinUlp(123.456f, 123.456085f, 10);
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/testing/math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ bool WithinUlpGeneric(Float left, Float right, int n_ulp) {
if (!std::isfinite(left) || !std::isfinite(right)) {
return left == right;
}
return WithinUlpOneWay(left, right, n_ulp) || WithinUlpOneWay(right, left, n_ulp);
return (std::abs(left) <= std::abs(right)) ? WithinUlpOneWay(left, right, n_ulp)
: WithinUlpOneWay(right, left, n_ulp);
}

template <typename Float>
Expand Down

0 comments on commit 65eb9fd

Please sign in to comment.