[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 8e48e14a 5/6: Don't force long double type fo
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 8e48e14a 5/6: Don't force long double type for reciprocals |
Date: |
Sat, 30 Apr 2022 09:13:23 -0400 (EDT) |
branch: master
commit 8e48e14a5326c9fde9e90072d0898b8ef40da3cd
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Don't force long double type for reciprocals
---
math_functions.hpp | 26 +++++++++++---------------
1 file changed, 11 insertions(+), 15 deletions(-)
diff --git a/math_functions.hpp b/math_functions.hpp
index fa1f75a0..778ee1d4 100644
--- a/math_functions.hpp
+++ b/math_functions.hpp
@@ -137,11 +137,10 @@ struct i_upper_n_over_n_from_i
return -1.0;
}
- static long double const reciprocal_n = T(1) / n;
+ constexpr T 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);
- return static_cast<T>(z);
+ return std::expm1(std::log1p(i) * reciprocal_n);
}
};
@@ -166,8 +165,7 @@ struct i_from_i_upper_n_over_n
{
// naively: (1+i)^n - 1
// substitute: (1+i)^n - 1 <-> std::expm1(std::log1p(i) * n)
- long double z = std::expm1(std::log1p(i) * n);
- return static_cast<T>(z);
+ return std::expm1(std::log1p(i) * n);
}
};
@@ -198,11 +196,10 @@ struct d_upper_n_from_i
throw std::range_error("i equals -100%.");
}
- static long double const reciprocal_n = T(1) / n;
+ constexpr T 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);
- return static_cast<T>(z);
+ return -n * std::expm1(std::log1p(i) * -reciprocal_n);
}
};
@@ -229,7 +226,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 = T(1) / n;
+ constexpr T reciprocal_n = T(1) / n;
// naively:
// (1
// + (1+ i)^(1/n)
@@ -237,15 +234,14 @@ struct net_i_from_gross
// - fee *(1/n)
// )^n - 1
// substitute: (1+i)^n - 1 <-> std::expm1(std::log1p(i) * n)
- long double z = std::expm1
+ return std::expm1
(
n * std::log1p
( std::expm1(reciprocal_n * std::log1p(i))
- std::expm1(reciprocal_n * std::log1p(spread))
- - reciprocal_n * fee
+ - reciprocal_n * fee
)
);
- return static_cast<T>(z);
}
};
@@ -301,16 +297,16 @@ struct coi_rate_from_q
}
else
{
- static long double const reciprocal_12 = T(1) / 12;
+ constexpr T 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);
+ T monthly_q = -std::expm1(std::log1p(-q) * reciprocal_12);
if(T(1) == monthly_q)
{
throw std::logic_error("Monthly q equals unity.");
}
monthly_q = monthly_q / (T(1) - monthly_q);
- return std::min(max_coi, static_cast<T>(monthly_q));
+ return std::min(max_coi, 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 <=
- [lmi-commits] [lmi] master 58b87552 4/6: Don't force long double into double calculations, Greg Chicares, 2022/04/30