[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 2576d70c 09/11: Prefer bin_exp() to SGI's pow
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 2576d70c 09/11: Prefer bin_exp() to SGI's power() |
Date: |
Tue, 31 May 2022 17:52:06 -0400 (EDT) |
branch: master
commit 2576d70c47b4bd259556942c9b979158f8594409
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Prefer bin_exp() to SGI's power()
---
commutation_functions.cpp | 12 ++++++------
ihs_basicval.cpp | 6 +++---
ledger_base.cpp | 4 ++--
math_functions_test.cpp | 13 +++++--------
round_to.hpp | 10 +++++-----
round_to_test.cpp | 12 ++++++------
6 files changed, 27 insertions(+), 30 deletions(-)
diff --git a/commutation_functions.cpp b/commutation_functions.cpp
index 4614280f..c63a4ea7 100644
--- a/commutation_functions.cpp
+++ b/commutation_functions.cpp
@@ -24,9 +24,9 @@
#include "commutation_functions.hpp"
#include "assert_lmi.hpp"
+#include "bin_exp.hpp"
#include "et_vector.hpp" // [VECTORIZE]
#include "ssize_lmi.hpp"
-#include "stl_extensions.hpp" // nonstd::power()
#include <algorithm> // rotate_copy() [VECTORIZE]
#include <functional> // multiplies [VECTORIZE]
@@ -161,14 +161,14 @@ ULCommFns::ULCommFns
// Present value of $1 one month hence.
double vp = v * p;
// Present value of $1 twelve months hence.
- double vp12 = nonstd::power(vp, 12);
- double vpn = nonstd::power(vp, periods_per_year);
+ double vp12 = bin_exp(vp, 12);
+ double vpn = bin_exp(vp, periods_per_year);
// Twelve times a'' upper 12 (Eckley equations 28 and 31),
// determined analytically using the geometric series theorem.
// double aa = 1.0;
// // Eckley equation (31).
-// double sa = (1.0 - vp12) / (1.0 - nonstd::power(vp, 6));
-// double qa = (1.0 - vp12) / (1.0 - nonstd::power(vp, 3));
+// double sa = (1.0 - vp12) / (1.0 - bin_exp(vp, 6));
+// double qa = (1.0 - vp12) / (1.0 - bin_exp(vp, 3));
// // Eckley equation (28).
// double ma = (1.0 - vp12) / (1.0 - vp);
// The prefix k indicates the processing mode, which is
@@ -176,7 +176,7 @@ ULCommFns::ULCommFns
double ka = 1.0;
if(1.0 != vp)
{
- ka = (1.0 - vp12) / (1.0 - nonstd::power(vp, months_per_period));
+ ka = (1.0 - vp12) / (1.0 - bin_exp(vp, months_per_period));
}
kd[j] = ka * ad[j];
kc[j] = ka * ad[j] * v * q;
diff --git a/ihs_basicval.cpp b/ihs_basicval.cpp
index 73103859..9dadcd63 100644
--- a/ihs_basicval.cpp
+++ b/ihs_basicval.cpp
@@ -26,6 +26,7 @@
#include "alert.hpp"
#include "assert_lmi.hpp"
#include "basic_tables.hpp" // cvat_corridor_factors()
+#include "bin_exp.hpp"
#include "bourn_cast.hpp"
#include "contains.hpp"
#include "data_directory.hpp" // AddDataDir()
@@ -49,7 +50,6 @@
#include "outlay.hpp"
#include "premium_tax.hpp"
#include "rounding_rules.hpp"
-#include "stl_extensions.hpp" // nonstd::power()
#include "stratified_charges.hpp"
#include "ul_utilities.hpp" // list_bill_premium(),
max_modal_premium()
@@ -1026,7 +1026,7 @@ currency BasicValues::GetModalPremCorridor
{
double const rate = GetCorridorFactor()[0];
int const k = round_corridor_factor().decimals();
- double const s = nonstd::power(10, k);
+ double const s = bin_exp(10.0, k);
// Do not save and restore prior rounding direction because
// lmi generally expects rounding to nearest everywhere.
std::fesetround(FE_TONEAREST);
@@ -1530,7 +1530,7 @@ currency BasicValues::GetModalSpecAmtCorridor(currency
annualized_pmt) const
{
double const rate = GetCorridorFactor()[0];
int const k = round_corridor_factor().decimals();
- double const s = nonstd::power(10, k);
+ double const s = bin_exp(10.0, k);
// Do not save and restore prior rounding direction because
// lmi generally expects rounding to nearest everywhere.
std::fesetround(FE_TONEAREST);
diff --git a/ledger_base.cpp b/ledger_base.cpp
index 9f9d0b15..1b8f863c 100644
--- a/ledger_base.cpp
+++ b/ledger_base.cpp
@@ -25,9 +25,9 @@
#include "alert.hpp"
#include "assert_lmi.hpp"
+#include "bin_exp.hpp"
#include "crc32.hpp"
#include "et_vector.hpp"
-#include "stl_extensions.hpp" // nonstd::power()
#include "value_cast.hpp"
#include <algorithm> // max(), min()
@@ -428,7 +428,7 @@ void LedgerBase::apply_scale_factor(int decimal_power)
return;
}
- double const scale_factor = 1.0 / nonstd::power(10.0, scale_power_);
+ double const scale_factor = bin_exp(10.0, -scale_power_);
for(auto& i : ScalableVectors)
{
*i.second *= scale_factor;
diff --git a/math_functions_test.cpp b/math_functions_test.cpp
index 554b0f52..acddfa28 100644
--- a/math_functions_test.cpp
+++ b/math_functions_test.cpp
@@ -23,10 +23,10 @@
#include "math_functions.hpp"
+#include "bin_exp.hpp"
#include "fenv_lmi.hpp"
#include "materially_equal.hpp"
#include "miscellany.hpp" // stifle_unused_warning()
-#include "stl_extensions.hpp" // nonstd::power()
#include "test_tools.hpp"
#include "timer.hpp"
@@ -192,20 +192,17 @@ void mete3()
}
// These 'mete[45]' functions calculate 10^-9 in different ways.
-// The SGI extension is about eight times as fast as calling a
-// transcendental function; that outcome is not surprising, but
-// quantifying it is useful. Of course, it would not be surprising
-// to find that a table lookup would be even faster for "reasonable"
-// powers of ten.
+// Binary exponentiation is much faster than a transcendental
+// calculation; that's not surprising, but worth measuring.
void mete4()
{
double volatile base = 10.0;
- int volatile exponent = 9;
+ int volatile exponent = -9;
double volatile x;
for(int j = 0; j < 100000; ++j)
{
- x = 1.0 / nonstd::power(base, exponent);
+ x = bin_exp(base, exponent);
}
stifle_unused_warning(x);
}
diff --git a/round_to.hpp b/round_to.hpp
index 80de8de1..5390acf7 100644
--- a/round_to.hpp
+++ b/round_to.hpp
@@ -24,9 +24,9 @@
#include "config.hpp"
+#include "bin_exp.hpp"
#include "currency.hpp"
#include "mc_enum_type_enums.hpp" // rounding_style
-#include "stl_extensions.hpp" // nonstd::power()
#include <cmath> // fabs(), floor(), rint()
#include <limits>
@@ -219,23 +219,23 @@ round_to<RealType>::round_to(int a_decimals,
rounding_style a_style)
if(0 <= decimals_)
{
- scale_fwd_ = nonstd::power(ten, decimals_);
+ scale_fwd_ = bin_exp(ten, decimals_);
scale_back_ = one / scale_fwd_;
}
else
{
- scale_back_ = nonstd::power(ten, -decimals_);
+ scale_back_ = bin_exp(ten, -decimals_);
scale_fwd_ = one / scale_back_;
}
if(0 <= decimals_cents_)
{
- scale_fwd_cents_ = nonstd::power(ten, decimals_cents_);
+ scale_fwd_cents_ = bin_exp(ten, decimals_cents_);
scale_back_cents_ = one / scale_fwd_cents_;
}
else
{
- scale_back_cents_ = nonstd::power(ten, -decimals_cents_);
+ scale_back_cents_ = bin_exp(ten, -decimals_cents_);
scale_fwd_cents_ = one / scale_back_cents_;
}
diff --git a/round_to_test.cpp b/round_to_test.cpp
index da324681..22b3738e 100644
--- a/round_to_test.cpp
+++ b/round_to_test.cpp
@@ -23,10 +23,10 @@
#include "round_to.hpp"
+#include "bin_exp.hpp"
#include "currency.hpp" // currency::cents_digits
#include "fenv_lmi.hpp"
#include "miscellany.hpp" // floating_rep(), scoped_ios_format
-#include "stl_extensions.hpp" // nonstd::power()
#include "test_tools.hpp"
#include <algorithm> // max()
@@ -301,8 +301,8 @@ void round_to_test::test_various_float_types
int const inverse_decimals = -decimals;
long double factor =
(0 <= inverse_decimals)
- ? nonstd::power(10.0L, inverse_decimals)
- : 1.0L / nonstd::power(10.0L, -inverse_decimals)
+ ? bin_exp(10.0L, inverse_decimals)
+ : 1.0L / bin_exp(10.0L, -inverse_decimals)
;
long double u = unrounded * factor;
long double e = expected * factor;
@@ -626,9 +626,9 @@ void round_to_test::test_fundamentals()
// Try to provoke division by zero in ctor-initializer.
//
- // nonstd::power() negates a negative exponent, but negating
- // INT_MIN constitutes UB, so add one, plus currency::cents_digits
- // because of the interplay between classes currency and round_to.
+ // bin_exp() negates a negative exponent, but negating INT_MIN
+ // constitutes UB, so add one, plus currency::cents_digits because
+ // of the interplay between classes currency and round_to.
LMI_TEST_THROW
(round_to<double>(1 + currency::cents_digits + INT_MIN, r_to_nearest)
,std::domain_error
- [lmi-commits] [lmi] master updated (44ea8a5b -> fd325f5e), Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master d36e5145 03/11: Suppress undefined behavior; add a test with defined behavior, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 28055a62 02/11: Realize the actual intention of a comparison, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master a3278e2b 10/11: Expunge 'stl_extensions.hpp', Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master c6dc35ea 05/11: Demonstrate equivalence of bin_exp() to SGI's power(), Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 8922e7a1 06/11: Explain restrictions on bin_exp() argument types, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 8cbc3001 07/11: Make a value definitely exact and definitely constexpr, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 2c8cc917 04/11: Don't bother calculating 2^64 - 1, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 7727d36d 08/11: Prefer bin_exp() to SGI's power(), Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master d9655b77 01/11: Refactor: rename local variables, Greg Chicares, 2022/05/31
- [lmi-commits] [lmi] master 2576d70c 09/11: Prefer bin_exp() to SGI's power(),
Greg Chicares <=
- [lmi-commits] [lmi] master fd325f5e 11/11: Record speed measurements, Greg Chicares, 2022/05/31