WPILibC++ 2023.4.3
tanh.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 hyperbolic tangent function
23 */
24
25#ifndef _gcem_tanh_HPP
26#define _gcem_tanh_HPP
27
28namespace internal
29{
30
31template<typename T>
32constexpr
33T
34tanh_cf(const T xx, const int depth)
35noexcept
36{
37 return( depth < GCEM_TANH_MAX_ITER ? \
38 // if
39 (2*depth - 1) + xx/tanh_cf(xx,depth+1) :
40 // else
41 T(2*depth - 1) );
42}
43
44template<typename T>
45constexpr
46T
47tanh_begin(const T x)
48noexcept
49{
50 return( x/tanh_cf(x*x,1) );
51}
52
53template<typename T>
54constexpr
55T
56tanh_check(const T x)
57noexcept
58{
59 return( // NaN check
60 is_nan(x) ? \
61 GCLIM<T>::quiet_NaN() :
62 // indistinguishable from zero
63 GCLIM<T>::min() > abs(x) ? \
64 T(0) :
65 // else
66 x < T(0) ? \
67 - tanh_begin(-x) :
68 tanh_begin( x) );
69}
70
71}
72
73/**
74 * Compile-time hyperbolic tangent function
75 *
76 * @param x a real-valued input.
77 * @return the hyperbolic tangent function using \f[ \tanh(x) = \dfrac{x}{1 + \dfrac{x^2}{3 + \dfrac{x^2}{5 + \dfrac{x^2}{7 + \ddots}}}} \f]
78 */
79
80template<typename T>
81constexpr
82return_t<T>
83tanh(const T x)
84noexcept
85{
86 return internal::tanh_check( static_cast<return_t<T>>(x) );
87}
88
89#endif
#define GCEM_TANH_MAX_ITER
Definition: gcem_options.hpp:193
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_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr T tanh_cf(const T xx, const int depth) noexcept
Definition: tanh.hpp:34
constexpr T tanh_check(const T x) noexcept
Definition: tanh.hpp:56
constexpr T tanh_begin(const T x) noexcept
Definition: tanh.hpp:47
constexpr return_t< T > tanh(const T x) noexcept
Compile-time hyperbolic tangent function.
Definition: tanh.hpp:83