WPILibC++ 2023.4.3
log1p.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 natural logarithm(x+1) function
23 */
24
25#ifndef _gcem_log1p_HPP
26#define _gcem_log1p_HPP
27
28namespace internal
29{
30
31// see:
32// http://functions.wolfram.com/ElementaryFunctions/Log/06/01/04/01/0003/
33
34
35template<typename T>
36constexpr
37T
38log1p_compute(const T x)
39noexcept
40{
41 // return x * ( T(1) + x * ( -T(1)/T(2) + x * ( T(1)/T(3) + x * ( -T(1)/T(4) + x/T(5) ) ) ) ); // O(x^6)
42 return x + x * ( - x/T(2) + x * ( x/T(3) + x * ( -x/T(4) + x*x/T(5) ) ) ); // O(x^6)
43}
44
45template<typename T>
46constexpr
47T
48log1p_check(const T x)
49noexcept
50{
51 return( // NaN check
52 is_nan(x) ? \
53 GCLIM<T>::quiet_NaN() :
54 //
55 abs(x) > T(1e-04) ? \
56 // if
57 log(T(1) + x) :
58 // else
59 log1p_compute(x) );
60}
61
62}
63
64/**
65 * Compile-time natural-logarithm-plus-1 function
66 *
67 * @param x a real-valued input.
68 * @return \f$ \log_e(x+1) \f$ using \f[ \log(x+1) = \sum_{k=1}^\infty \dfrac{(-1)^{k-1}x^k}{k}, \ \ |x| < 1 \f]
69 */
70
71template<typename T>
72constexpr
73return_t<T>
74log1p(const T x)
75noexcept
76{
77 return internal::log1p_check( static_cast<return_t<T>>(x) );
78}
79
80#endif
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
dimensionless::scalar_t log(const ScalarUnit x) noexcept
Compute natural logarithm.
Definition: math.h:349
constexpr return_t< T > log1p(const T x) noexcept
Compile-time natural-logarithm-plus-1 function.
Definition: log1p.hpp:74
Definition: Eigen_Colamd.h:50
constexpr T log1p_check(const T x) noexcept
Definition: log1p.hpp:48
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr T log1p_compute(const T x) noexcept
Definition: log1p.hpp:38
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.