lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 4eb11e23 5/8: Use relative_error


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 4eb11e23 5/8: Use relative_error
Date: Fri, 17 Jun 2022 17:16:28 -0400 (EDT)

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

    Use relative_error
    
    Changed files selected from
      git grep -i 'rel.*err'
    to use relative_error() where appropriate.
    
    * ihs_crc_comp.cpp: Tested using some historical regression-test files.
    As expected, relative_error() seems to give an error statistic uniformly
    greater than or equal to the old code (which looks like it might divide
    zero by zero, but that no longer matters).
    
    * round_test.cpp, round_to_test.cpp: The relative_error() documentation
    might seem to imply that these use cases are outside its intended scope,
    because an observed value is tested against a canonical expected value.
    However, the error is tested to ensure it lies within tight bounds, and
    then discarded, so relative_error() is perfectly suitable.
---
 ihs_crc_comp.cpp  |  7 ++-----
 round_test.cpp    | 25 ++-----------------------
 round_to_test.cpp | 25 ++-----------------------
 3 files changed, 6 insertions(+), 51 deletions(-)

diff --git a/ihs_crc_comp.cpp b/ihs_crc_comp.cpp
index 7ba3bc40..54b82fc2 100644
--- a/ihs_crc_comp.cpp
+++ b/ihs_crc_comp.cpp
@@ -24,6 +24,7 @@
 
 #include "contains.hpp"
 #include "main_common.hpp"
+#include "math_functions.hpp"           // relative_error()
 #include "miscellany.hpp"
 #include "value_cast.hpp"
 
@@ -300,11 +301,7 @@ void f_3(std::string const& line1, std::string const& 
line2)
     long double abs_diff = std::fabs(d1 - d2);
     max_abs_diff = std::max(max_abs_diff, abs_diff);
 
-    long double rel_err =
-        std::fabs(
-                (d1 - d2)
-            /   ((0.0 == d1) ? d2 : d1)
-            );
+    long double rel_err = relative_error(d1, d2);
     max_rel_err = std::max(max_rel_err, rel_err);
 
     if(rel_err < 1.0E-11L)
diff --git a/round_test.cpp b/round_test.cpp
index f6a764f4..7395b8c4 100644
--- a/round_test.cpp
+++ b/round_test.cpp
@@ -35,6 +35,7 @@
 #include "round_to.hpp"
 
 #include "fenv_lmi.hpp"
+#include "math_functions.hpp"           // relative_error()
 #include "miscellany.hpp"               // floating_rep(), scoped_ios_format
 #include "test_tools.hpp"
 
@@ -152,29 +153,7 @@ bool test_one_case
     LMI_TEST_EQUAL(std::round(unrounded), observed);
 
     max_prec_real abs_error = std::fabs(observed - expected);
-    // Nonstandardly define relative error in terms of
-    // o(bserved) and e(xpected) as
-    //   |(o-e)/e| if e nonzero, else
-    //   |(o-e)/o| if o nonzero, else
-    //   zero
-    // in order to avoid division by zero.
-    max_prec_real rel_error(0.0);
-    if(max_prec_real(0.0) != expected)
-        {
-        rel_error = std::fabs
-            (
-              (observed - max_prec_real(expected))
-            / expected
-            );
-        }
-    else if(max_prec_real(0.0) != observed)
-        {
-        rel_error = std::fabs
-            (
-              (observed - max_prec_real(expected))
-            / observed
-            );
-        }
+    max_prec_real rel_error = relative_error(observed, expected);
 
     // In general, we can't hope for the relative error to be less than
     // epsilon for the floating-point type being rounded. Suppose a
diff --git a/round_to_test.cpp b/round_to_test.cpp
index bdcffa29..cbc551c3 100644
--- a/round_to_test.cpp
+++ b/round_to_test.cpp
@@ -26,6 +26,7 @@
 #include "bin_exp.hpp"
 #include "currency.hpp"                 // currency::cents_digits
 #include "fenv_lmi.hpp"
+#include "math_functions.hpp"           // relative_error()
 #include "miscellany.hpp"               // floating_rep(), scoped_ios_format
 #include "test_tools.hpp"
 
@@ -163,29 +164,7 @@ bool round_to_test::test_one_case
     RealType observed = f(unrounded);
 
     max_prec_real abs_error = std::fabs(observed - expected);
-    // Nonstandardly define relative error in terms of
-    // o(bserved) and e(xpected) as
-    //   |(o-e)/e| if e nonzero, else
-    //   |(o-e)/o| if o nonzero, else
-    //   zero
-    // in order to avoid division by zero.
-    max_prec_real rel_error(0.0);
-    if(max_prec_real(0.0) != expected)
-        {
-        rel_error = std::fabs
-            (
-              (observed - max_prec_real(expected))
-            / expected
-            );
-        }
-    else if(max_prec_real(0.0) != observed)
-        {
-        rel_error = std::fabs
-            (
-              (observed - max_prec_real(expected))
-            / observed
-            );
-        }
+    max_prec_real rel_error = relative_error(observed, expected);
 
     // In general, we can't hope for the relative error to be less than
     // epsilon for the floating-point type being rounded. Suppose a



reply via email to

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