WPILibC++ 2023.4.3-108-ge5452e3
exp.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2022 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
28namespace internal
29{
30
31template<typename T>
32constexpr
33T
34exp_cf_recur(const T x, const int depth)
35noexcept
36{
37 return( depth < GCEM_EXP_MAX_ITER_SMALL ? \
38 // 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) :
42 // else
43 T(1) );
44}
45
46template<typename T>
47constexpr
48T
49exp_cf(const T x)
50noexcept
51{
52 return( T(1)/exp_cf_recur(x,1) );
53}
54
55template<typename T>
56constexpr
57T
58exp_split(const T x)
59noexcept
60{
61 return( static_cast<T>(pow_integral(GCEM_E,find_whole(x))) * exp_cf(find_fraction(x)) );
62}
63
64template<typename T>
65constexpr
66T
67exp_check(const T x)
68noexcept
69{
70 return( is_nan(x) ? \
71 GCLIM<T>::quiet_NaN() :
72 //
73 is_neginf(x) ? \
74 T(0) :
75 //
76 GCLIM<T>::min() > abs(x) ? \
77 T(1) :
78 //
79 is_posinf(x) ? \
80 GCLIM<T>::infinity() :
81 //
82 abs(x) < T(2) ? \
83 exp_cf(x) : \
84 exp_split(x) );
85}
86
87}
88
89/**
90 * Compile-time exponential function
91 *
92 * @param x a real-valued input.
93 * @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]
94 * 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$.
95 */
96
97template<typename T>
98constexpr
99return_t<T>
100exp(const T x)
101noexcept
102{
103 return internal::exp_check( static_cast<return_t<T>>(x) );
104}
105
106#endif
constexpr return_t< T > exp(const T x) noexcept
Compile-time exponential function.
Definition: exp.hpp:100
#define GCEM_EXP_MAX_ITER_SMALL
Definition: gcem_options.hpp:145
#define GCEM_E
Definition: gcem_options.hpp:130
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
Definition: Eigen_Colamd.h:50
constexpr bool is_posinf(const T x) noexcept
Definition: is_inf.hpp:81
constexpr T exp_split(const T x) noexcept
Definition: exp.hpp:58
constexpr bool is_neginf(const T x) noexcept
Definition: is_inf.hpp:34
constexpr T exp_cf(const T x) noexcept
Definition: exp.hpp:49
constexpr T1 pow_integral(const T1 base, const T2 exp_term) noexcept
Definition: pow_integral.hpp:120
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr T exp_cf_recur(const T x, const int depth) noexcept
Definition: exp.hpp:34
constexpr T exp_check(const T x) noexcept
Definition: exp.hpp:67
constexpr T find_fraction(const T x) noexcept
Definition: find_fraction.hpp:34
constexpr llint_t find_whole(const T x) noexcept
Definition: find_whole.hpp:34