[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master d242e40e 1/2: Prevent division by zero
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master d242e40e 1/2: Prevent division by zero |
Date: |
Thu, 16 Jun 2022 16:34:38 -0400 (EDT) |
branch: master
commit d242e40eb5f9511d24a49df2180b49ee7604b202
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Prevent division by zero
This local function is nearly ready to move out of the unit test and
into a header (as a template) that can be used elsewhere. But there's
other UB to confront first.
---
math_functions_test.cpp | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/math_functions_test.cpp b/math_functions_test.cpp
index 60fa695e..be34b7a1 100644
--- a/math_functions_test.cpp
+++ b/math_functions_test.cpp
@@ -634,11 +634,29 @@ void test_expm1_log1p()
// 0.025940753546620676 = 3F9A903680771FB0 fdlibm
// 0.025940753546620673 = 3F9A903680771FAF glibc
+ constexpr double inf {std::numeric_limits<double>::infinity()};
+ constexpr double big {std::numeric_limits<double>::max()};
// Absolute value of relative error.
auto rel_err = [](double t, double u)
{
- return std::fabs(t - u) / std::min(std::fabs(t), std::fabs(u));
+ auto const denominator {std::min(std::fabs(t), std::fabs(u))};
+ return
+ (0.0 == t && 0.0 == u) ? 0.0
+ : (0.0 == denominator) ? inf
+ : std::fabs(t - u) / denominator
+ ;
};
+ LMI_TEST_EQUAL(inf, rel_err(0.0, -2.0));
+ LMI_TEST_EQUAL(inf, rel_err(0.0, -1.0));
+ LMI_TEST_EQUAL(inf, rel_err(0.0, -0.5));
+ LMI_TEST_EQUAL(0.0, rel_err(0.0, 0.0));
+ LMI_TEST_EQUAL(inf, rel_err(0.0, 0.5));
+ LMI_TEST_EQUAL(inf, rel_err(0.0, 1.0));
+ LMI_TEST_EQUAL(inf, rel_err(0.0, 2.0));
+ LMI_TEST_EQUAL(0.0, rel_err(1.0, 1.0));
+ LMI_TEST_EQUAL(2.0, rel_err(1.0, -1.0));
+ LMI_TEST_EQUAL(big, rel_err(1.0, big));
+ LMI_TEST_EQUAL(inf, rel_err(big, -big));
// Test fdlibm vs. C RTL for many parameters.
int err_count0 {0};