lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 7a6262f 5/5: Save and restore stream formatti


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 7a6262f 5/5: Save and restore stream formatting more easily and correctly
Date: Mon, 2 Aug 2021 18:25:16 -0400 (EDT)

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

    Save and restore stream formatting more easily and correctly
    
    This fixes a latent defect. Test your iostream knowledge by finding it:
    
    -    std::ios_base::fmtflags old_flags = std::cout.flags();
    [...manipulators...]
    -    std::cout.setf(old_flags);
    
    SPOILER:
    
    The one-argument version of the function called on the last line is not
    equivalent to calling flags() with the same argument.
---
 round_test.cpp    |  8 +++-----
 round_to_test.cpp | 14 +++++---------
 zero.hpp          |  4 ++++
 zero_test.cpp     |  6 +++---
 4 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/round_test.cpp b/round_test.cpp
index 215a25f..df75471 100644
--- a/round_test.cpp
+++ b/round_test.cpp
@@ -35,7 +35,7 @@
 #include "round_to.hpp"
 
 #include "fenv_lmi.hpp"
-#include "miscellany.hpp"               // floating_rep()
+#include "miscellany.hpp"               // floating_rep(), scoped_ios_format
 #include "test_tools.hpp"
 
 #include <algorithm>                    // max()
@@ -203,9 +203,9 @@ bool test_one_case
 
     if(!error_is_within_tolerance)
         {
+        scoped_ios_format meaningless_name(std::cout);
         std::cout << '\n';
-        std::streamsize old_precision = std::cout.precision(DECIMAL_DIG);
-        std::ios_base::fmtflags old_flags = std::cout.flags();
+        std::cout.precision(DECIMAL_DIG);
 
         std::cout
             << "Rounding   " << get_name_of_float_type<RealType>()
@@ -239,8 +239,6 @@ bool test_one_case
             << "\n  tolerance " << tolerance
             ;
 
-        std::cout.setf(old_flags);
-        std::cout.precision(old_precision);
         std::cout << std::endl;
         }
     return error_is_within_tolerance;
diff --git a/round_to_test.cpp b/round_to_test.cpp
index 6d37987..87a16c5 100644
--- a/round_to_test.cpp
+++ b/round_to_test.cpp
@@ -25,7 +25,7 @@
 
 #include "currency.hpp"                 // currency::cents_digits
 #include "fenv_lmi.hpp"
-#include "miscellany.hpp"               // floating_rep()
+#include "miscellany.hpp"               // floating_rep(), scoped_ios_format
 #include "stl_extensions.hpp"           // nonstd::power()
 #include "test_tools.hpp"
 
@@ -248,9 +248,9 @@ bool round_to_test::test_one_case
 
     if(!error_is_within_tolerance)
         {
+        scoped_ios_format meaningless_name(std::cout);
         std::cout << '\n';
-        std::streamsize old_precision = std::cout.precision(DECIMAL_DIG);
-        std::ios_base::fmtflags old_flags = std::cout.flags();
+        std::cout.precision(DECIMAL_DIG);
 
         std::cout
             << "Rounding   "     << get_name_of_float_type<RealType>()
@@ -286,8 +286,6 @@ bool round_to_test::test_one_case
             << "\n  tolerance " << tolerance
             ;
 
-        std::cout.setf(old_flags);
-        std::cout.precision(old_precision);
         std::cout << std::endl;
         }
     return error_is_within_tolerance;
@@ -555,6 +553,7 @@ void round_to_test::test_all_modes(bool synchronize)
 
 void round_to_test::test_scaling()
 {
+    scoped_ios_format meaningless_name(std::cout);
     double const volatile d0 = 2.71828'18284'59045'23536;
     double const lo = nextafter(d0, -INFINITY);
     double const hi = nextafter(d0,  INFINITY);
@@ -567,8 +566,7 @@ void round_to_test::test_scaling()
     long double const volatile lreciprocal = 1.0L / 1.0e8L;
     double const volatile d4 = static_cast<double>((d0 * 1.0e8L) * 
lreciprocal);
 
-    std::streamsize old_precision = std::cout.precision(DECIMAL_DIG);
-    std::ios_base::fmtflags old_flags = std::cout.flags();
+    std::cout.precision(DECIMAL_DIG);
 
     std::cout
         << lo << std::hexfloat << '\t' << lo << std::defaultfloat << " lo\n"
@@ -580,8 +578,6 @@ void round_to_test::test_scaling()
         << d4 << std::hexfloat << '\t' << d4 << std::defaultfloat << " d4\n"
         ;
 
-    std::cout.setf(old_flags);
-    std::cout.precision(old_precision);
     std::cout << std::endl;
 }
 
diff --git a/zero.hpp b/zero.hpp
index 34bf944..e6967f5 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -25,6 +25,7 @@
 #include "config.hpp"
 
 #include "math_functions.hpp"           // signum()
+#include "miscellany.hpp"               // scoped_ios_format
 #include "null_stream.hpp"
 #include "round_to.hpp"
 #include "ssize_lmi.hpp"
@@ -169,6 +170,7 @@ inline double binary64_midpoint(double d0, double d1)
     double z;
     std::memcpy(&z, &um, sizeof z);
 #if 0 // Temporarily useful for acceptance testing.
+        scoped_ios_format meaningless_name(std::cout);
         std::cout
             << std::dec
             << std::defaultfloat
@@ -400,6 +402,7 @@ root_type lmi_root
 
     auto recapitulate = [&]()
         {
+        scoped_ios_format meaningless_name(os_trace);
         os_trace
             << n_iter << " iterations, "
             << n_eval << " evaluations; final interval:\n"
@@ -732,6 +735,7 @@ double brent_zero
 
     auto recapitulate = [&]()
         {
+        scoped_ios_format meaningless_name(os_trace);
         os_trace
             << n_iter << " iterations, "
             << n_eval << " evaluations; final interval:\n"
diff --git a/zero_test.cpp b/zero_test.cpp
index 149392e..da1588f 100644
--- a/zero_test.cpp
+++ b/zero_test.cpp
@@ -798,9 +798,9 @@ void test_celebrated_equation()
 10 iterations, 12 evaluations; final interval:
  b +2.09455148154232650981 fb -8.88178419700125232339e-16
  c +2.09455148154232739799 fc +9.76996261670137755573e-15
- return value: +2.09455148154232650981 = b
- function evaluations: +12 +12 nominal, actual
- return value: +2.09455148154232650981 (rounded)
+ return value: 2.0945514815423265 = b
+ function evaluations: 12 12 nominal, actual
+ return value: 2.0945514815423265 (rounded)
 )--cut-here--";
 
     LMI_TEST_EQUAL(verified, oss.str());



reply via email to

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