Skip to content

Commit

Permalink
add C++14 implementation of exp
Browse files Browse the repository at this point in the history
  • Loading branch information
kthohr committed Mar 2, 2023
1 parent 59e90b0 commit 8422f53
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions include/gcem_incl/exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@
namespace internal
{

// see https://en.wikipedia.org/wiki/Euler%27s_continued_fraction_formula

#if __cplusplus >= 201402L // C++14 version

template<typename T>
constexpr
T
exp_cf_recur(const T x, const int depth_end)
noexcept
{
int depth = GCEM_EXP_MAX_ITER_SMALL - 1;
T res = T(1);

while (depth > depth_end - 1) {
res = T(1) + x/T(depth - 1) - x/depth/res;

--depth;
}

return res;
}

#else // C++11 version

template<typename T>
constexpr
T
Expand All @@ -36,20 +60,20 @@ noexcept
{
return( depth < GCEM_EXP_MAX_ITER_SMALL ? \
// if
depth == 1 ? \
T(1) - x/exp_cf_recur(x,depth+1) :
T(1) + x/T(depth - 1) - x/depth/exp_cf_recur(x,depth+1) :
T(1) + x/T(depth - 1) - x/depth/exp_cf_recur(x,depth+1) :
// else
T(1) );
}

#endif

template<typename T>
constexpr
T
exp_cf(const T x)
noexcept
{
return( T(1)/exp_cf_recur(x,1) );
return( T(1) / (T(1) - x / exp_cf_recur(x,2)) );
}

template<typename T>
Expand All @@ -72,7 +96,7 @@ noexcept
//
is_neginf(x) ? \
T(0) :
//
// indistinguishable from zero
GCLIM<T>::min() > abs(x) ? \
T(1) :
//
Expand Down

0 comments on commit 8422f53

Please sign in to comment.