2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-30 20:12:09 +00:00

Add explicit casting for int to floating point types

This commit is contained in:
Matt Borland
2022-07-03 19:50:17 -07:00
parent 69e6643cbd
commit 28ff5ed847

View File

@@ -12,7 +12,7 @@
namespace boost::math::tools {
// Algorithm 1 of https://people.mpim-bonn.mpg.de/zagier/files/exp-math-9/fulltext.pdf
// Convergence Acceleration of Alternating Series: Henri Cohen, Fernando Rodriguez Villegas, and Don Zagier
// Convergence Acceleration of Alternating Series: Henri Cohen, Fernando Rodriguez Villegas, and Don Zagier
template<class G>
auto cohen_acceleration(G& generator, std::int64_t n = -1)
{
@@ -23,16 +23,16 @@ auto cohen_acceleration(G& generator, std::int64_t n = -1)
using std::pow;
using std::ceil;
using std::sqrt;
Real n_ = n;
auto n_ = static_cast<Real>(n);
if (n < 0)
{
// relative error grows as 2*5.828^-n; take 5.828^-n < eps/4 => -nln(5.828) < ln(eps/4) => n > ln(4/eps)/ln(5.828).
// Is there a way to do it rapidly with std::log2? (Yes, of course; but for primitive types it's computed at compile-time anyway.)
n_ = ceil(log(4/std::numeric_limits<Real>::epsilon())*0.5672963285532555);
n_ = static_cast<Real>(ceil(log(4/std::numeric_limits<Real>::epsilon())*0.5672963285532555));
n = static_cast<std::int64_t>(n_);
}
// d can get huge and overflow if you pick n too large:
Real d = pow(3 + sqrt(Real(8)), n);
auto d = static_cast<Real>(pow(3 + sqrt(Real(8)), n));
d = (d + 1/d)/2;
Real b = -1;
Real c = -d;