lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 799af61 1/4: Make a unit test pass both with


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 799af61 1/4: Make a unit test pass both with and without optimization
Date: Sun, 18 Mar 2018 20:02:19 -0400 (EDT)

branch: master
commit 799af61bf413fbecacaee6febfba7a27177a9b25
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Make a unit test pass both with and without optimization
    
    Made the smallest (for now, rather than the cleanest) change to make
    the failing test pass without optimization. Motivation: to emphasize
    the mistake.
    
    The base-10 logarithm L of the maximal value is calculated for exactly
    one purpose: 1+floor(L) is a proxy for the number of digits to the left
    of the decimal point in that value's decimal representation. Although
    this logarithm is a floating-point quantity, only its integer part is
    actually wanted. Therefore, it should be immediately converted to an
    integer. To perform inexact floating-point operations upon it before
    conversion to integer is to invite preventible inexactness into the
    calculation.
    
    In this context, truncation is appropriate: no choice need be made
    between floor() and trunc() semantics, because only a natural number is
    wanted. Naturally, a later revision of the changed line:
        double d = static_cast<int>(std::log10(widest));
    will store the integer value in an integer variable.
---
 miscellany.cpp      | 2 +-
 miscellany_test.cpp | 4 +---
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/miscellany.cpp b/miscellany.cpp
index 5d886d4..7cd0fff 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -135,7 +135,7 @@ int scale_power(int max_power, double min_value, double 
max_value)
         return 0;
         }
 
-    double d = std::log10(widest);
+    double d = static_cast<int>(std::log10(widest));
     d = std::floor(d / 3.0);
     int k = 3 * static_cast<int>(d);
     k = k - 6;
diff --git a/miscellany_test.cpp b/miscellany_test.cpp
index 473ca1a..59f27e1 100644
--- a/miscellany_test.cpp
+++ b/miscellany_test.cpp
@@ -333,9 +333,7 @@ void test_scale_power()
     BOOST_TEST_EQUAL( 3, scale_power( 9, 0.0,                   
999'999'999.1));
     BOOST_TEST_EQUAL( 3, scale_power( 9, 0.0,               
999'999'999'999.0));
     BOOST_TEST_EQUAL( 6, scale_power( 9, 0.0,               
999'999'999'999.1));
-    // This test passes with MinGW-w64 gcc-7.2.0 with optimization,
-    // but fails with an optimized 'safestdlib' build:
-//  BOOST_TEST_EQUAL( 6, scale_power( 9, 0.0,           
999'999'999'999'999.0));
+    BOOST_TEST_EQUAL( 6, scale_power( 9, 0.0,           
999'999'999'999'999.0));
     BOOST_TEST_EQUAL( 9, scale_power( 9, 0.0,           
999'999'999'999'999.1));
 
     // In the last test above, the threshold is not     999'999'999'999'999.01



reply via email to

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