[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 77cc4d65 11/11: Prefer nonstd::power() to std
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 77cc4d65 11/11: Prefer nonstd::power() to std::pow() in a particular case |
Date: |
Thu, 26 May 2022 18:14:25 -0400 (EDT) |
branch: master
commit 77cc4d65ddb3b1b322041c695a917c34a3f65594
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Prefer nonstd::power() to std::pow() in a particular case
The exponent in this case is always an integer in {1, 3, 6, 12}.
For x86_64, pc-linux-gnu and MinGW-w64 yielded different results
when std::pow() was used; nonstd::power() makes results identical.
The number of multiplications is low, so there's little opportunity
for error to accumulate. And, because the exponent is lower than 15,
right-to-left binary exponentiation uses the optimal addition chain,
so the number of multiplications is the lowest possible.
Although 'i' may be small (so that 'v' is close to unity), 'p' is
likely to be large, and therefore 'vp' is generally outside the range
where using
vp^N <=> 1 + expm1(log1p(vp - 1) * N)
would be significantly more accurate.
---
commutation_functions.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/commutation_functions.cpp b/commutation_functions.cpp
index a456833a..4614280f 100644
--- a/commutation_functions.cpp
+++ b/commutation_functions.cpp
@@ -26,9 +26,9 @@
#include "assert_lmi.hpp"
#include "et_vector.hpp" // [VECTORIZE]
#include "ssize_lmi.hpp"
+#include "stl_extensions.hpp" // nonstd::power()
#include <algorithm> // rotate_copy() [VECTORIZE]
-#include <cmath> // pow()
#include <functional> // multiplies [VECTORIZE]
#include <numeric> // partial_sum()
@@ -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 = std::pow(vp, 12);
- double vpn = std::pow(vp, periods_per_year);
+ double vp12 = nonstd::power(vp, 12);
+ double vpn = nonstd::power(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 - std::pow(vp, 6));
-// double qa = (1.0 - vp12) / (1.0 - std::pow(vp, 3));
+// double sa = (1.0 - vp12) / (1.0 - nonstd::power(vp, 6));
+// double qa = (1.0 - vp12) / (1.0 - nonstd::power(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 - std::pow(vp, months_per_period));
+ ka = (1.0 - vp12) / (1.0 - nonstd::power(vp, months_per_period));
}
kd[j] = ka * ad[j];
kc[j] = ka * ad[j] * v * q;
- [lmi-commits] [lmi] master 1b9395f2 03/11: Improve documentation, (continued)
- [lmi-commits] [lmi] master 1b9395f2 03/11: Improve documentation, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master 699308ab 01/11: Improve documentation, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master 086ce6b3 06/11: Fix a probable defect introduced 20210314T1905Z, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master 284efedf 08/11: For immediate reversion: binary exponentiation, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master f920e584 10/11: Improve documentation, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master 43bbe463 05/11: Use lmi::expm1() and lmi::log1p() in a unit-test function, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master 2c0ec871 07/11: Use lmi rather than std functions, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master b1b2ed4f 09/11: Revert "For immediate reversion: binary exponentiation", Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master b6752650 02/11: Improve documentation, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master a110f9a0 04/11: Write fdlibm forwarding functions out of line, Greg Chicares, 2022/05/26
- [lmi-commits] [lmi] master 77cc4d65 11/11: Prefer nonstd::power() to std::pow() in a particular case,
Greg Chicares <=