zanmato1984 commented on code in PR #44906:
URL: https://github.com/apache/arrow/pull/44906#discussion_r1875367310


##########
cpp/src/arrow/testing/math.cc:
##########
@@ -0,0 +1,95 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 "arrow/testing/math.h"
+
+#include <cmath>
+#include <limits>
+
+#include <gtest/gtest.h>
+
+#include "arrow/util/logging.h"
+
+namespace arrow {
+namespace {
+
+template <typename Float>
+constexpr Float kOneUlp;
+template <>
+constexpr float kOneUlp<float> = 5.9604645e-08f;
+template <>
+constexpr double kOneUlp<double> = 1.1102230246251565e-16;
+
+template <typename Float>
+bool WithinUlpOneWay(Float left, Float right, int n_ulp) {
+  // Ideally these would be compile-time (constexpr) checks
+  DCHECK_LT(Float(1.0) - kOneUlp<Float>, Float(1.0));
+  DCHECK_EQ(std::nextafter(Float(1.0) - kOneUlp<Float>, Float(1.0)), 
Float(1.0));

Review Comment:
   How can these `DCHECK`s fail (suppose no one ever changes the `kOneUlp` 
definition)? Platform-specific issues?



##########
cpp/src/arrow/testing/gtest_util_test.cc:
##########
@@ -171,4 +175,108 @@ TEST_F(TestTensorFromJSON, FromJSON) {
   EXPECT_TRUE(tensor_expected->Equals(*result));
 }
 
+template <typename Float>
+void CheckWithinUlpSingle(Float x, Float y, int n_ulp) {
+  ARROW_SCOPED_TRACE("x = ", x, ", y = ", y, ", n_ulp = ", n_ulp);
+  ASSERT_TRUE(WithinUlp(x, y, n_ulp));
+}
+
+template <typename Float>
+void CheckNotWithinUlpSingle(Float x, Float y, int n_ulp) {
+  ARROW_SCOPED_TRACE("x = ", x, ", y = ", y, ", n_ulp = ", n_ulp);
+  ASSERT_FALSE(WithinUlp(x, y, n_ulp));
+}
+
+template <typename Float>
+void CheckWithinUlp(Float x, Float y, int n_ulp) {
+  CheckWithinUlpSingle(x, y, n_ulp);
+  CheckWithinUlpSingle(y, x, n_ulp);
+  CheckWithinUlpSingle(x, y, n_ulp + 1);
+  CheckWithinUlpSingle(y, x, n_ulp + 1);
+  CheckWithinUlpSingle(-x, -y, n_ulp);
+  CheckWithinUlpSingle(-y, -x, n_ulp);
+
+  for (int exp : {1, -1, 10, -10}) {
+    Float x_scaled = std::ldexp(x, exp);
+    Float y_scaled = std::ldexp(y, exp);
+    CheckWithinUlpSingle(x_scaled, y_scaled, n_ulp);
+    CheckWithinUlpSingle(y_scaled, x_scaled, n_ulp);
+  }
+}
+
+template <typename Float>
+void CheckNotWithinUlp(Float x, Float y, int n_ulp) {
+  CheckNotWithinUlpSingle(x, y, n_ulp);
+  CheckNotWithinUlpSingle(y, x, n_ulp);
+  if (n_ulp > 1) {
+    CheckNotWithinUlpSingle(x, y, n_ulp - 1);
+    CheckNotWithinUlpSingle(y, x, n_ulp - 1);
+  }
+  CheckNotWithinUlpSingle(-x, -y, n_ulp);
+  CheckNotWithinUlpSingle(-y, -x, n_ulp);

Review Comment:
   ```suggestion
     CheckNotWithinUlpSingle(-x, -y, n_ulp);
     CheckNotWithinUlpSingle(-y, -x, n_ulp);
     if (n_ulp > 1) {
       CheckNotWithinUlpSingle(x, y, n_ulp - 1);
       CheckNotWithinUlpSingle(y, x, n_ulp - 1);
       CheckNotWithinUlpSingle(-x, -y, n_ulp - 1);
       CheckNotWithinUlpSingle(-y, -x, n_ulp - 1);
     }
   ```



##########
cpp/src/arrow/testing/math.cc:
##########
@@ -0,0 +1,95 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 "arrow/testing/math.h"
+
+#include <cmath>
+#include <limits>
+
+#include <gtest/gtest.h>
+
+#include "arrow/util/logging.h"
+
+namespace arrow {
+namespace {
+
+template <typename Float>
+constexpr Float kOneUlp;
+template <>
+constexpr float kOneUlp<float> = 5.9604645e-08f;
+template <>
+constexpr double kOneUlp<double> = 1.1102230246251565e-16;

Review Comment:
   Are these values `Float(1.0) - std::nextafter(Float(1.0), 
std::numeric_limits<Float>::min())`?
   
   The names `kOneUlp` could be misleading as it can be also interpreted as 
`std::nextafter(Float(1.0), std::numeric_limits<Float>::max()) - Float(1.0)` 
(results in values being twice big).



##########
cpp/src/arrow/testing/math.cc:
##########
@@ -0,0 +1,95 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 "arrow/testing/math.h"
+
+#include <cmath>
+#include <limits>
+
+#include <gtest/gtest.h>
+
+#include "arrow/util/logging.h"
+
+namespace arrow {
+namespace {
+
+template <typename Float>
+constexpr Float kOneUlp;
+template <>
+constexpr float kOneUlp<float> = 5.9604645e-08f;
+template <>
+constexpr double kOneUlp<double> = 1.1102230246251565e-16;
+
+template <typename Float>
+bool WithinUlpOneWay(Float left, Float right, int n_ulp) {
+  // Ideally these would be compile-time (constexpr) checks
+  DCHECK_LT(Float(1.0) - kOneUlp<Float>, Float(1.0));
+  DCHECK_EQ(std::nextafter(Float(1.0) - kOneUlp<Float>, Float(1.0)), 
Float(1.0));
+
+  DCHECK_GE(n_ulp, 1);
+
+  if (left == 0) {
+    return left == right;
+  }
+  if (left < 0) {
+    left = -left;
+    right = -right;
+  }
+
+  int left_exp;
+  Float left_mant = std::frexp(left, &left_exp);
+  Float delta = static_cast<Float>(n_ulp) * kOneUlp<Float>;

Review Comment:
   Should we compute the `kOneUlp` on the fly instead of pre-defined constants, 
for the following reasons:
   1. The beginning two `DCHECK`s won't be needed;
   2. The platform-specific logic will be delegated to `std` (instead of doing 
runtime checks using `DCHECK`).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to