Skip to content

Commit 0e396ac

Browse files
committed
Fix logic when two different exponents are involved
1 parent 505c96f commit 0e396ac

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cpp/src/arrow/testing/gtest_util_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ TEST(TestWithinUlp, Double) {
233233
CheckWithinUlp(1.0, 1.0000000000000007, 3);
234234
CheckNotWithinUlp(1.0, 1.0000000000000007, 2);
235235
CheckNotWithinUlp(1.0, 1.0000000000000007, 1);
236+
// left and right have a different exponent but are still very close
237+
CheckWithinUlp(1.0, 0.9999999999999999, 1);
238+
CheckWithinUlp(1.0, 0.9999999999999988, 11);
239+
CheckNotWithinUlp(1.0, 0.9999999999999988, 10);
236240

237241
CheckWithinUlp(123.4567, 123.45670000000015, 11);
238242
CheckNotWithinUlp(123.4567, 123.45670000000015, 10);
@@ -253,6 +257,10 @@ TEST(TestWithinUlp, Float) {
253257
CheckWithinUlp(1.0f, 1.0000001f, 1);
254258
CheckWithinUlp(1.0f, 1.0000013f, 11);
255259
CheckNotWithinUlp(1.0f, 1.0000013f, 10);
260+
// left and right have a different exponent but are still very close
261+
CheckWithinUlp(1.0f, 0.99999994f, 1);
262+
CheckWithinUlp(1.0f, 0.99999934f, 11);
263+
CheckNotWithinUlp(1.0f, 0.99999934f, 10);
256264

257265
CheckWithinUlp(123.456f, 123.456085f, 11);
258266
CheckNotWithinUlp(123.456f, 123.456085f, 10);

cpp/src/arrow/testing/math.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ bool WithinUlpGeneric(Float left, Float right, int n_ulp) {
6363
if (!std::isfinite(left) || !std::isfinite(right)) {
6464
return left == right;
6565
}
66-
return WithinUlpOneWay(left, right, n_ulp) || WithinUlpOneWay(right, left, n_ulp);
66+
return (std::abs(left) <= std::abs(right)) ? WithinUlpOneWay(left, right, n_ulp)
67+
: WithinUlpOneWay(right, left, n_ulp);
6768
}
6869

6970
template <typename Float>

0 commit comments

Comments
 (0)