Skip to content

Commit 8422f53

Browse files
committed
add C++14 implementation of exp
1 parent 59e90b0 commit 8422f53

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

include/gcem_incl/exp.hpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@
2828
namespace internal
2929
{
3030

31+
// see https://en.wikipedia.org/wiki/Euler%27s_continued_fraction_formula
32+
33+
#if __cplusplus >= 201402L // C++14 version
34+
35+
template<typename T>
36+
constexpr
37+
T
38+
exp_cf_recur(const T x, const int depth_end)
39+
noexcept
40+
{
41+
int depth = GCEM_EXP_MAX_ITER_SMALL - 1;
42+
T res = T(1);
43+
44+
while (depth > depth_end - 1) {
45+
res = T(1) + x/T(depth - 1) - x/depth/res;
46+
47+
--depth;
48+
}
49+
50+
return res;
51+
}
52+
53+
#else // C++11 version
54+
3155
template<typename T>
3256
constexpr
3357
T
@@ -36,20 +60,20 @@ noexcept
3660
{
3761
return( depth < GCEM_EXP_MAX_ITER_SMALL ? \
3862
// if
39-
depth == 1 ? \
40-
T(1) - x/exp_cf_recur(x,depth+1) :
41-
T(1) + x/T(depth - 1) - x/depth/exp_cf_recur(x,depth+1) :
63+
T(1) + x/T(depth - 1) - x/depth/exp_cf_recur(x,depth+1) :
4264
// else
4365
T(1) );
4466
}
4567

68+
#endif
69+
4670
template<typename T>
4771
constexpr
4872
T
4973
exp_cf(const T x)
5074
noexcept
5175
{
52-
return( T(1)/exp_cf_recur(x,1) );
76+
return( T(1) / (T(1) - x / exp_cf_recur(x,2)) );
5377
}
5478

5579
template<typename T>
@@ -72,7 +96,7 @@ noexcept
7296
//
7397
is_neginf(x) ? \
7498
T(0) :
75-
//
99+
// indistinguishable from zero
76100
GCLIM<T>::min() > abs(x) ? \
77101
T(1) :
78102
//

0 commit comments

Comments
 (0)