lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master b954393 2/8: Don't hard-code sqrt(2)


From: Greg Chicares
Subject: [lmi-commits] [lmi] master b954393 2/8: Don't hard-code sqrt(2)
Date: Fri, 30 Jul 2021 16:14:47 -0400 (EDT)

branch: master
commit b954393f338f078bd675be8c17ac45c4d59b08fa
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Don't hard-code sqrt(2)
    
    Calling sqrt() seems preferable. See this IEEE publication:
    
      https://ieeexplore.ieee.org/document/910638
    | The IEEE 754 standard for floating-point arithmetic requires that the
    | four arithmetic operations and the square root should be correctly
    | rounded.
    
    It's not entirely clear what it means to "require" that something
    "should" be done. Presumably a mandative subjunctive is intended:
      requires that...the square root be correctly rounded
      ["should" inserted only because UK English lacks the subjunctive]
    because the IEEE 754 standard itself groups squareRoot in section
      "5.4.1 Arithmetic operations"
    along with {+,-,×,÷}, whereas another section
      "Table 9.1 — Recommended correctly rounded functions"
    lists functions, like reciprocal-sqrt and the usual transcendentals,
    for which correct rounding would be ideal but was not originally
    required (the authors go on to show that it is actually feasible).
---
 zero_test.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/zero_test.cpp b/zero_test.cpp
index dc88d6b..1926b23 100644
--- a/zero_test.cpp
+++ b/zero_test.cpp
@@ -29,7 +29,7 @@
 #include "test_tools.hpp"
 
 #include <cfloat>                       // DECIMAL_DIG
-#include <cmath>                        // exp(), fabs(), log(), pow()
+#include <cmath>                        // exp(), fabs(), log(), pow(), sqrt()
 #include <limits>
 #include <sstream>
 
@@ -882,7 +882,7 @@ void test_various_functions()
     test_a_function        (f03, root_03, -0.01, 0.8, 1.0e-15, __LINE__);
 
     auto f04 = [](double x) {return std::pow(x, 2.0) - 2.0;};
-    auto root_04 = 1.4142135623730951;
+    auto root_04 = std::sqrt(2.0);
     test_a_decimal_function(f04, root_04, 0.0 , 2.0, 17     , __LINE__, 10);
     test_a_function        (f04, root_04, 0.0 , 2.0, 1.0e-15, __LINE__);
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]