[libcxx-commits] [libcxx] [libc++][math] Fix undue overflowing of `std::hypot(x, y, z)` (PR #93350)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 18 13:09:47 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff f38baad3e7395af94290aac21e587bc1c5f00946 c276104420491039c46a09a09797a0097176d11f --extensions ,cpp,h -- libcxx/include/__math/hypot.h libcxx/include/cmath libcxx/test/std/numerics/c.math/cmath.pass.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libcxx/test/std/numerics/c.math/cmath.pass.cpp b/libcxx/test/std/numerics/c.math/cmath.pass.cpp
index 6ed37c2721..2fa94d388b 100644
--- a/libcxx/test/std/numerics/c.math/cmath.pass.cpp
+++ b/libcxx/test/std/numerics/c.math/cmath.pass.cpp
@@ -1117,41 +1117,41 @@ void test_fmin()
 }
 
 struct TestHypot3 {
-    template <class Real>
-    void operator()() const {
-        const auto check = [](Real elem, Real abs_tol) {
-            assert(std::isfinite(std::hypot(elem, Real(0), Real(0))));
-            assert(fptest_close(std::hypot(elem, Real(0), Real(0)), elem, abs_tol));
-            assert(std::isfinite(std::hypot(elem, elem, Real(0))));
-            assert(fptest_close(std::hypot(elem, elem, Real(0)), std::sqrt(Real(2)) * elem, abs_tol));
-            assert(std::isfinite(std::hypot(elem, elem, elem)));
-            assert(fptest_close(std::hypot(elem, elem, elem), std::sqrt(Real(3)) * elem, abs_tol));
-        };
-
-        { // check for overflow
-            const auto [elem, abs_tol] = []() -> std::array<Real, 2> {
-                if constexpr (std::is_same_v<Real, float>)
-                    return {1e20f, 1e16f};
-                else if constexpr (std::is_same_v<Real, double>)
-                    return {1e300, 1e287};
-                else // long double
-                    return {1e4000l, 1e3985l};
-            }();
-            check(elem, abs_tol);
-        }
-
-        { // check for underflow
-            const auto [elem, abs_tol] = []() -> std::array<Real, 2> {
-                if constexpr (std::is_same_v<Real, float>)
-                    return {1e-20f, 1e-24f};
-                else if constexpr (std::is_same_v<Real, double>)
-                    return {1e-287, 1e-300};
-                else // long double
-                    return {1e-3985l, 1e-4000l};
-            }();
-            check(elem, abs_tol);
-        }
+  template <class Real>
+  void operator()() const {
+    const auto check = [](Real elem, Real abs_tol) {
+      assert(std::isfinite(std::hypot(elem, Real(0), Real(0))));
+      assert(fptest_close(std::hypot(elem, Real(0), Real(0)), elem, abs_tol));
+      assert(std::isfinite(std::hypot(elem, elem, Real(0))));
+      assert(fptest_close(std::hypot(elem, elem, Real(0)), std::sqrt(Real(2)) * elem, abs_tol));
+      assert(std::isfinite(std::hypot(elem, elem, elem)));
+      assert(fptest_close(std::hypot(elem, elem, elem), std::sqrt(Real(3)) * elem, abs_tol));
+    };
+
+    { // check for overflow
+      const auto [elem, abs_tol] = []() -> std::array<Real, 2> {
+        if constexpr (std::is_same_v<Real, float>)
+          return {1e20f, 1e16f};
+        else if constexpr (std::is_same_v<Real, double>)
+          return {1e300, 1e287};
+        else // long double
+          return {1e4000l, 1e3985l};
+      }();
+      check(elem, abs_tol);
     }
+
+    { // check for underflow
+      const auto [elem, abs_tol] = []() -> std::array<Real, 2> {
+        if constexpr (std::is_same_v<Real, float>)
+          return {1e-20f, 1e-24f};
+        else if constexpr (std::is_same_v<Real, double>)
+          return {1e-287, 1e-300};
+        else // long double
+          return {1e-3985l, 1e-4000l};
+      }();
+      check(elem, abs_tol);
+    }
+  }
 };
 
 void test_hypot()

``````````

</details>


https://github.com/llvm/llvm-project/pull/93350


More information about the libcxx-commits mailing list