WPILibC++ 2023.4.3
expm1.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_expm1_HPP
26#define _gcem_expm1_HPP
27
28namespace internal
29{
30
31template<typename T>
32constexpr
33T
34expm1_compute(const T x)
35noexcept
36{
37 // return x * ( T(1) + x * ( T(1)/T(2) + x * ( T(1)/T(6) + x * ( T(1)/T(24) + x/T(120) ) ) ) ); // O(x^6)
38 return x + x * ( x/T(2) + x * ( x/T(6) + x * ( x/T(24) + x*x/T(120) ) ) ); // O(x^6)
39}
40
41template<typename T>
42constexpr
43T
44expm1_check(const T x)
45noexcept
46{
47 return( // NaN check
48 is_nan(x) ? \
49 GCLIM<T>::quiet_NaN() :
50 //
51 abs(x) > T(1e-04) ? \
52 // if
53 exp(x) - T(1) :
54 // else
55 expm1_compute(x) );
56}
57
58}
59
60/**
61 * Compile-time exponential-minus-1 function
62 *
63 * @param x a real-valued input.
64 * @return \f$ \exp(x) - 1 \f$ using \f[ \exp(x) = \sum_{k=0}^\infty \dfrac{x^k}{k!} \f]
65 */
66
67template<typename T>
68constexpr
69return_t<T>
70expm1(const T x)
71noexcept
72{
73 return internal::expm1_check( static_cast<return_t<T>>(x) );
74}
75
76#endif
constexpr return_t< T > expm1(const T x) noexcept
Compile-time exponential-minus-1 function.
Definition: expm1.hpp:70
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
Definition: Eigen_Colamd.h:50
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr T expm1_compute(const T x) noexcept
Definition: expm1.hpp:34
constexpr T expm1_check(const T x) noexcept
Definition: expm1.hpp:44
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.