consteig
Compile-time eigenvalue and eigenvector computation for C++17
Loading...
Searching...
No Matches
exp.hpp
1#ifndef CONSTMATH_EXP_HPP
2#define CONSTMATH_EXP_HPP
3
4#include "../../consteig_options.hpp"
5#include "pow.hpp"
6#include "utilities.hpp"
7
8namespace consteig
9{
10
13
14// Max iterations for continued fraction expansion
15constexpr int EXP_MAX_ITER = 50;
16
17template <typename T>
18constexpr T exp_cf_recur(const T x, const int depth_end) noexcept
19{
20 int depth = EXP_MAX_ITER - 1;
21 T res = static_cast<T>(1);
22 while (depth > depth_end - 1)
23 {
24 res = static_cast<T>(1) + x / static_cast<T>(depth - 1) -
25 x / static_cast<T>(depth) / res;
26 --depth;
27 }
28 return res;
29}
30
31template <typename T> constexpr T exp_cf(const T x) noexcept
32{
33 return static_cast<T>(1) / (static_cast<T>(1) - x / exp_cf_recur(x, 2));
34}
35
36template <typename T> constexpr T find_whole(const T x) noexcept
37{
38 return static_cast<T>(static_cast<long long>(x));
39}
40
41template <typename T> constexpr T find_fraction(const T x) noexcept
42{
43 return x - find_whole(x);
44}
45
46template <typename T> constexpr T exp_split(const T x) noexcept
47{
48 return pow(static_cast<T>(E_CONST), static_cast<int>(find_whole(x))) *
49 exp_cf(find_fraction(x));
50}
51
52template <typename T> constexpr T exp_check(const T x) noexcept
53{
54 return (x < static_cast<T>(0) ? static_cast<T>(1) / exp_check(-x)
55 : x < static_cast<T>(2) ? exp_cf(x)
56 : exp_split(x));
57}
58
62template <typename T> constexpr auto exp(const T x) noexcept
63{
64 if constexpr (!is_float<T>())
65 {
66 return exp_check(static_cast<double>(x));
67 }
68 else
69 {
70 return exp_check(x);
71 }
72}
73
75
76} // namespace consteig
77
78#endif
#define E_CONST
Euler's number e to 50 significant digits.
Definition consteig_options.hpp:50
constexpr T pow(const T x, const unsigned int n)
Raise x to an unsigned integer power via exponentiation by squaring.
Definition pow.hpp:17
constexpr auto exp(const T x) noexcept
Computes the exponential of a real number.
Definition exp.hpp:62
constexpr T epsilon()
Machine epsilon for type T.
Definition utilities.hpp:82