[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 13dd6ae 1/4: Use overloading instead of a def
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 13dd6ae 1/4: Use overloading instead of a default argument |
Date: |
Thu, 19 Aug 2021 07:03:52 -0400 (EDT) |
branch: master
commit 13dd6ae8e75973ed5c3df54b6c3872d844ff9d2b
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Use overloading instead of a default argument
Defaulting a std::ostream& argument is unclean--see:
https://lists.nongnu.org/archive/html/lmi/2021-08/msg00001.html
et seqq. Reworked by overloading instead. Now each invocation gets its
own "null" ostream instead of sharing the same static instance.
Moved the std::ostream& argument to an earlier position in argument
lists because all defaulted arguments must come last.
---
ihs_avsolve.cpp | 2 +-
zero.hpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
zero_test.cpp | 10 ++++----
3 files changed, 75 insertions(+), 10 deletions(-)
diff --git a/ihs_avsolve.cpp b/ihs_avsolve.cpp
index bc3c47e..98c9388 100644
--- a/ihs_avsolve.cpp
+++ b/ihs_avsolve.cpp
@@ -488,8 +488,8 @@ currency AccountValue::Solve
,upper_bound
,bias
,decimals
- ,64
,os_trace
+ ,64
);
currency const solution_cents = round_minutiae().c(solution.root);
diff --git a/zero.hpp b/zero.hpp
index 1bbc2c3..ee123bc 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -432,8 +432,8 @@ root_type lmi_root
,double bound0
,double bound1
,double tolerance
+ ,std::ostream& os_trace
,int sprauchling_limit = INT_MAX
- ,std::ostream& os_trace = null_stream()
,root_bias bias = bias_none
)
{
@@ -672,6 +672,29 @@ root_type lmi_root
}
}
+template<typename FunctionalType>
+root_type lmi_root
+ (FunctionalType& f
+ ,double bound0
+ ,double bound1
+ ,double tolerance
+ ,int sprauchling_limit = INT_MAX
+ ,root_bias bias = bias_none
+ )
+{
+ std::ostream null_ostream(&null_streambuf());
+ null_ostream.setstate(std::ios::badbit);
+ return lmi_root
+ (f
+ ,bound0
+ ,bound1
+ ,tolerance
+ ,null_ostream
+ ,sprauchling_limit
+ ,bias
+ );
+}
+
/// Return a rounded zero z of a function f within input bounds [a,b].
///
/// Intended to be used where f would round its argument anyway.
@@ -747,8 +770,8 @@ root_type decimal_root
,double bound1
,root_bias bias
,int decimals
+ ,std::ostream& os_trace
,int sprauchling_limit = INT_MAX
- ,std::ostream& os_trace = null_stream()
)
{
round_to<double> const round_dec {decimals, r_to_nearest};
@@ -782,8 +805,8 @@ root_type decimal_root
,round_dec(bound0)
,round_dec(bound1)
,0.5 * std::pow(10.0, -decimals)
- ,sprauchling_limit
,os_trace
+ ,sprauchling_limit
,bias
);
z.root = round_dec(z.root);
@@ -794,6 +817,29 @@ root_type decimal_root
return z;
}
+template<typename FunctionalType>
+root_type decimal_root
+ (FunctionalType& f
+ ,double bound0
+ ,double bound1
+ ,root_bias bias
+ ,int decimals
+ ,int sprauchling_limit = INT_MAX
+ )
+{
+ std::ostream null_ostream(&null_streambuf());
+ null_ostream.setstate(std::ios::badbit);
+ return decimal_root
+ (f
+ ,bound0
+ ,bound1
+ ,bias
+ ,decimals
+ ,null_ostream
+ ,sprauchling_limit
+ );
+}
+
/// An instrumented translation of Brent's reference implementation.
///
/// Deviation from the original ALGOL:
@@ -822,7 +868,7 @@ double brent_zero
,double a
,double b
,double t
- ,std::ostream& os_trace = null_stream()
+ ,std::ostream& os_trace
)
{
// Returns a zero of the function f in the given interval [a,b],
@@ -984,6 +1030,25 @@ double brent_zero
return b;
}
+template<typename FunctionalType>
+double brent_zero
+ (FunctionalType& f
+ ,double a
+ ,double b
+ ,double t
+ )
+{
+ std::ostream null_ostream(&null_streambuf());
+ null_ostream.setstate(std::ios::badbit);
+ return brent_zero
+ (f
+ ,a
+ ,b
+ ,t
+ ,null_ostream
+ );
+}
+
/// A C++ translation of Brent's algol60 reference implementation.
template<typename FunctionalType>
diff --git a/zero_test.cpp b/zero_test.cpp
index 45a99b0..3206507 100644
--- a/zero_test.cpp
+++ b/zero_test.cpp
@@ -147,7 +147,7 @@ void test_a_function
std::ostringstream os1;
os1.precision(DECIMAL_DIG);
- root_type r = lmi_root(f, bound0, bound1, tol, INT_MAX, os1);
+ root_type r = lmi_root(f, bound0, bound1, tol, os1);
INVOKE_LMI_TEST(root_is_valid == r.validity, file, line);
error = r.root - exact_root;
INVOKE_LMI_TEST_RELATION(std::fabs(error),<=,maximum_error,file,line);
@@ -346,7 +346,7 @@ void test_fundamentals()
// Same, with expatiation.
std::ostringstream oss;
- r = decimal_root(e_function, 0.5, 5.0, bias_none, 9, INT_MAX, oss);
+ r = decimal_root(e_function, 0.5, 5.0, bias_none, 9, oss);
std::cout << oss.str() << std::endl;
// Test use with function object.
@@ -821,7 +821,7 @@ void test_celebrated_equation()
auto f = [](double x) {return x * x * x - 2.0 * x - 5.0;};
std::ostringstream oss;
oss.precision(17);
- root_type r = decimal_root(f, -2.56, 2.56, bias_none, 21, INT_MAX, oss);
+ root_type r = decimal_root(f, -2.56, 2.56, bias_none, 21, oss);
LMI_TEST(root_is_valid == r.validity);
// This constant is from the cited blog; lmi yields this,
// which agrees to sixteen significant digits:
@@ -895,7 +895,7 @@ void test_wikipedia_example()
{
auto f = [](double x) {return (x + 3.0) * (x - 1.0) * (x - 1.0);};
std::ostringstream oss;
- root_type r = decimal_root(f, -4.0, 4.0 / 3.0, bias_none, 15, INT_MAX,
oss);
+ root_type r = decimal_root(f, -4.0, 4.0 / 3.0, bias_none, 15, oss);
LMI_TEST(root_is_valid == r.validity);
LMI_TEST(std::fabs(-3.0 - r.root) <= 1.0e-15);
// Display this to investigate further:
@@ -1185,7 +1185,7 @@ void test_hodgepodge()
LMI_TEST_EQUAL(55, r.n_eval); // weak
std::ostringstream oss;
- r = lmi_root(signum_offset, -1.0e300, 1.0e300, 5.0e-19, INT_MAX, oss);
+ r = lmi_root(signum_offset, -1.0e300, 1.0e300, 5.0e-19, oss);
LMI_TEST(root_is_valid == r.validity);
LMI_TEST(materially_equal(-1.0 / 3.0, r.root));
LMI_TEST(r.n_eval <= 3023);