WPILibC++ 2024.3.2
log1p.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 natural logarithm(x+1) function
23 */
24
25#ifndef _gcem_log1p_HPP
26#define _gcem_log1p_HPP
27
28#include <cmath>
29#include <type_traits>
30
31namespace gcem
32{
33
34namespace internal
35{
36
37// see:
38// http://functions.wolfram.com/ElementaryFunctions/Log/06/01/04/01/0003/
39
40
41template<typename T>
42constexpr
43T
44log1p_compute(const T x)
45noexcept
46{
47 // 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)
48 return x + x * ( - x/T(2) + x * ( x/T(3) + x * ( -x/T(4) + x*x/T(5) ) ) ); // O(x^6)
49}
50
51template<typename T>
52constexpr
53T
54log1p_check(const T x)
55noexcept
56{
57 return( // NaN check
58 is_nan(x) ? \
60 //
61 abs(x) > T(1e-04) ? \
62 // if
63 log(T(1) + x) :
64 // else
65 log1p_compute(x) );
66}
67
68}
69
70/**
71 * Compile-time natural-logarithm-plus-1 function
72 *
73 * @param x a real-valued input.
74 * @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]
75 */
76
77template<typename T>
78constexpr
79return_t<T>
80log1p(const T x)
81noexcept
82{
84 return internal::log1p_check( static_cast<return_t<T>>(x) );
85 } else {
86 return std::log1p(x);
87 }
88}
89
90}
91
92#endif
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr T log1p_check(const T x) noexcept
Definition: log1p.hpp:54
constexpr T log1p_compute(const T x) noexcept
Definition: log1p.hpp:44
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 > log(const T x) noexcept
Compile-time natural logarithm function.
Definition: log.hpp:186
constexpr return_t< T > log1p(const T x) noexcept
Compile-time natural-logarithm-plus-1 function.
Definition: log1p.hpp:80
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
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.