Skip to content

Commit

Permalink
add C++14 version of erf; update erf tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kthohr committed Jul 30, 2023
1 parent ff96a61 commit 8c2d4b4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
54 changes: 51 additions & 3 deletions include/gcem_incl/erf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ namespace internal
// see
// https://functions.wolfram.com/GammaBetaErf/Erf/10/01/0007/

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

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

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

--depth;
}

return res;
}

#else // C++11 version

template<typename T>
constexpr
T
Expand All @@ -39,11 +61,13 @@ noexcept
{
return( depth < GCEM_ERF_MAX_ITER ? \
// if
x + 2*depth/erf_cf_large_recur(x,depth+1) :
x + 2 * depth / erf_cf_large_recur(x,depth+1) :
// else
x );
}

#endif

template<typename T>
constexpr
T
Expand All @@ -57,6 +81,28 @@ noexcept
// see
// https://functions.wolfram.com/GammaBetaErf/Erf/10/01/0005/

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

template<typename T>
constexpr
T
erf_cf_small_recur(const T xx, const int depth_end)
noexcept
{
int depth = GCEM_ERF_MAX_ITER - 1;
T res = T(2*(depth+1) - 1) - 2 * xx;

while (depth > depth_end - 1) {
res = T(2*depth - 1) - 2 * xx + 4 * depth * xx / res;

--depth;
}

return res;
}

#else // C++11 version

template<typename T>
constexpr
T
Expand All @@ -65,12 +111,14 @@ noexcept
{
return( depth < GCEM_ERF_MAX_ITER ? \
// if
(2*depth - T(1)) - 2*xx \
+ 4*depth*xx / erf_cf_small_recur(xx,depth+1) :
(2*depth - T(1)) - 2 * xx \
+ 4 * depth * xx / erf_cf_small_recur(xx,depth+1) :
// else
(2*depth - T(1)) - 2*xx );
}

#endif

template<typename T>
constexpr
T
Expand Down
5 changes: 5 additions & 0 deletions tests/erf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ int main()
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, -2.099L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, -2.0L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, -1.3L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, -0.7L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, -0.1L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, 0.0L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, 0.1L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, 0.7L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, 1.3L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, 1.3L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, 2.0L);
GCEM_TEST_COMPARE_VALS(gcem::erf, std::erf, 2.099L);
Expand Down

0 comments on commit 8c2d4b4

Please sign in to comment.