WPILibC++ 2024.3.2
exp.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2023 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21/*
22 * compile-time exponential function
23 */
24
25#ifndef _gcem_exp_HPP
26#define _gcem_exp_HPP
27
28#include <cmath>
29#include <type_traits>
30
31namespace gcem
32{
33
34namespace internal
35{
36
37// see https://en.wikipedia.org/wiki/Euler%27s_continued_fraction_formula
38
39#if __cplusplus >= 201402L // C++14 version
40
41template<typename T>
42constexpr
43T
44exp_cf_recur(const T x, const int depth_end)
45noexcept
46{
47 int depth = GCEM_EXP_MAX_ITER_SMALL - 1;
48 T res = T(1);
49
50 while (depth > depth_end - 1) {
51 res = T(1) + x/T(depth - 1) - x/depth/res;
52
53 --depth;
54 }
55
56 return res;
57}
58
59#else // C++11 version
60
61template<typename T>
62constexpr
63T
64exp_cf_recur(const T x, const int depth)
65noexcept
66{
67 return( depth < GCEM_EXP_MAX_ITER_SMALL ? \
68 // if
69 T(1) + x/T(depth - 1) - x/depth/exp_cf_recur(x,depth+1) :
70 // else
71 T(1) );
72}
73
74#endif
75
76template<typename T>
77constexpr
78T
79exp_cf(const T x)
80noexcept
81{
82 return( T(1) / (T(1) - x / exp_cf_recur(x,2)) );
83}
84
85template<typename T>
86constexpr
87T
88exp_split(const T x)
89noexcept
90{
91 return( static_cast<T>(pow_integral(GCEM_E,find_whole(x))) * exp_cf(find_fraction(x)) );
92}
93
94template<typename T>
95constexpr
96T
97exp_check(const T x)
98noexcept
99{
100 return( is_nan(x) ? \
102 //
103 is_neginf(x) ? \
104 T(0) :
105 // indistinguishable from zero
106 GCLIM<T>::min() > abs(x) ? \
107 T(1) :
108 //
109 is_posinf(x) ? \
111 //
112 abs(x) < T(2) ? \
113 exp_cf(x) : \
114 exp_split(x) );
115}
116
117}
118
119/**
120 * Compile-time exponential function
121 *
122 * @param x a real-valued input.
123 * @return \f$ \exp(x) \f$ using \f[ \exp(x) = \dfrac{1}{1-\dfrac{x}{1+x-\dfrac{\frac{1}{2}x}{1 + \frac{1}{2}x - \dfrac{\frac{1}{3}x}{1 + \frac{1}{3}x - \ddots}}}} \f]
124 * The continued fraction argument is split into two parts: \f$ x = n + r \f$, where \f$ n \f$ is an integer and \f$ r \in [-0.5,0.5] \f$.
125 */
126
127template<typename T>
128constexpr
129return_t<T>
130exp(const T x)
131noexcept
132{
134 return internal::exp_check( static_cast<return_t<T>>(x) );
135 } else {
136 return std::exp(x);
137 }
138}
139
140}
141
142#endif
#define GCEM_EXP_MAX_ITER_SMALL
Definition: gcem_options.hpp:145
#define GCEM_E
Definition: gcem_options.hpp:130
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr T exp_cf_recur(const T x, const int depth) noexcept
Definition: exp.hpp:64
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr T1 pow_integral(const T1 base, const T2 exp_term) noexcept
Definition: pow_integral.hpp:123
constexpr bool is_posinf(const T x) noexcept
Definition: is_inf.hpp:84
constexpr llint_t find_whole(const T x) noexcept
Definition: find_whole.hpp:37
constexpr bool is_neginf(const T x) noexcept
Definition: is_inf.hpp:37
constexpr T exp_check(const T x) noexcept
Definition: exp.hpp:97
constexpr T exp_split(const T x) noexcept
Definition: exp.hpp:88
constexpr T find_fraction(const T x) noexcept
Definition: find_fraction.hpp:37
constexpr T exp_cf(const T x) noexcept
Definition: exp.hpp:79
Definition: is_even.hpp:29
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition: abs.hpp:40
constexpr return_t< T > exp(const T x) noexcept
Compile-time exponential function.
Definition: exp.hpp:130
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74
typename std::conditional< std::is_integral< T >::value, double, T >::type return_t
Definition: gcem_options.hpp:77