[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 58b87552 4/6: Don't force long double into do
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 58b87552 4/6: Don't force long double into double calculations |
Date: |
Sat, 30 Apr 2022 09:13:22 -0400 (EDT) |
branch: master
commit 58b8755260d73af08e3f58e8ec1314e0bfdfe1aa
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Don't force long double into double calculations
Storing a reciprocal as long double, for later multiplication, does
give slightly greater accuracy; but hardware manufacturers, sadly,
have deprecated extended precision.
---
math_functions.hpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/math_functions.hpp b/math_functions.hpp
index 14da7080..fa1f75a0 100644
--- a/math_functions.hpp
+++ b/math_functions.hpp
@@ -137,7 +137,7 @@ struct i_upper_n_over_n_from_i
return -1.0;
}
- static long double const reciprocal_n = 1.0L / n;
+ static long double const reciprocal_n = T(1) / n;
// naively: (1+i)^(1/n) - 1
// substitute: (1+i)^n - 1 <-> std::expm1(std::log1p(i) * n)
long double z = std::expm1(std::log1p(i) * reciprocal_n);
@@ -198,7 +198,7 @@ struct d_upper_n_from_i
throw std::range_error("i equals -100%.");
}
- static long double const reciprocal_n = 1.0L / n;
+ static long double const reciprocal_n = T(1) / n;
// naively: n * (1 - (1+i)^(-1/n))
// substitute: (1+i)^n - 1 <-> std::expm1(std::log1p(i) * n)
long double z = -n * std::expm1(std::log1p(i) * -reciprocal_n);
@@ -229,7 +229,7 @@ struct net_i_from_gross
static_assert(0 < n);
T operator()(T const& i, T const& spread, T const& fee) const
{
- static long double const reciprocal_n = 1.0L / n;
+ static long double const reciprocal_n = T(1) / n;
// naively:
// (1
// + (1+ i)^(1/n)
@@ -301,15 +301,15 @@ struct coi_rate_from_q
}
else
{
- static long double const reciprocal_12 = 1.0L / 12;
+ static long double const reciprocal_12 = T(1) / 12;
// naively: 1 - (1-q)^(1/12)
// substitute: (1+i)^n - 1 <-> std::expm1(std::log1p(i) * n)
long double monthly_q = -std::expm1(std::log1p(-q) *
reciprocal_12);
- if(1.0L == monthly_q)
+ if(T(1) == monthly_q)
{
throw std::logic_error("Monthly q equals unity.");
}
- monthly_q = monthly_q / (1.0L - monthly_q);
+ monthly_q = monthly_q / (T(1) - monthly_q);
return std::min(max_coi, static_cast<T>(monthly_q));
}
}
- [lmi-commits] [lmi] master updated (92d90368 -> d7c5304d), Greg Chicares, 2022/04/30
- [lmi-commits] [lmi] master 57a1b38c 1/6: Improve documentation, Greg Chicares, 2022/04/30
- [lmi-commits] [lmi] master d7c5304d 6/6: Don't store unhelpful reciprocals, Greg Chicares, 2022/04/30
- [lmi-commits] [lmi] master 7ea80507 2/6: Prefer 'double' to 'long double' in a comment, Greg Chicares, 2022/04/30
- [lmi-commits] [lmi] master 44707d65 3/6: Use only unsuffixed expm1() and log1p(), Greg Chicares, 2022/04/30
- [lmi-commits] [lmi] master 8e48e14a 5/6: Don't force long double type for reciprocals, Greg Chicares, 2022/04/30
- [lmi-commits] [lmi] master 58b87552 4/6: Don't force long double into double calculations,
Greg Chicares <=