-
Notifications
You must be signed in to change notification settings - Fork 58
/
tan.hpp
140 lines (127 loc) · 3.73 KB
/
tan.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*################################################################################
##
## Copyright (C) 2016-2024 Keith O'Hara
##
## This file is part of the GCE-Math C++ library.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
################################################################################*/
/*
* compile-time tangent function
*/
#ifndef _gcem_tan_HPP
#define _gcem_tan_HPP
namespace internal
{
template<typename T>
constexpr
T
tan_series_exp_long(const T z)
noexcept
{ // this is based on a fourth-order expansion of tan(z) using Bernoulli numbers
return( - 1/z + (z/3 + (pow_integral(z,3)/45 + (2*pow_integral(z,5)/945 + pow_integral(z,7)/4725))) );
}
template<typename T>
constexpr
T
tan_series_exp(const T x)
noexcept
{
return( GCLIM<T>::min() > abs(x - T(GCEM_HALF_PI)) ? \
// the value tan(pi/2) is somewhat of a convention;
// technically the function is not defined at EXACTLY pi/2,
// but this is floating point pi/2
T(1.633124e+16) :
// otherwise we use an expansion around pi/2
tan_series_exp_long(x - T(GCEM_HALF_PI))
);
}
template<typename T>
constexpr
T
tan_cf_recur(const T xx, const int depth, const int max_depth)
noexcept
{
return( depth < max_depth ? \
// if
T(2*depth - 1) - xx/tan_cf_recur(xx,depth+1,max_depth) :
// else
T(2*depth - 1) );
}
template<typename T>
constexpr
T
tan_cf_main(const T x)
noexcept
{
return( (x > T(1.55) && x < T(1.60)) ? \
tan_series_exp(x) : // deals with a singularity at tan(pi/2)
//
x > T(1.4) ? \
x/tan_cf_recur(x*x,1,45) :
x > T(1) ? \
x/tan_cf_recur(x*x,1,35) :
// else
x/tan_cf_recur(x*x,1,25) );
}
template<typename T>
constexpr
T
tan_begin(const T x, const int count = 0)
noexcept
{ // tan(x) = tan(x + pi)
return( x > T(GCEM_PI) ? \
// if
count > 1 ? GCLIM<T>::quiet_NaN() : // protect against undefined behavior
tan_begin( x - T(GCEM_PI) * internal::floor_check(x/T(GCEM_PI)), count+1 ) :
// else
tan_cf_main(x) );
}
template<typename T>
constexpr
T
tan_check(const T x)
noexcept
{
return( // NaN check
is_nan(x) ? \
GCLIM<T>::quiet_NaN() :
// indistinguishable from zero
GCLIM<T>::min() > abs(x) ? \
T(0) :
// else
x < T(0) ? \
- tan_begin(-x) :
tan_begin( x) );
}
}
/**
* Compile-time tangent function
*
* @param x a real-valued input.
* @return the tangent function using
* \f[ \tan(x) = \dfrac{x}{1 - \dfrac{x^2}{3 - \dfrac{x^2}{5 - \ddots}}} \f]
* To deal with a singularity at \f$ \pi / 2 \f$, the following expansion is employed:
* \f[ \tan(x) = - \frac{1}{x-\pi/2} - \sum_{k=1}^\infty \frac{(-1)^k 2^{2k} B_{2k}}{(2k)!} (x - \pi/2)^{2k - 1} \f]
* where \f$ B_n \f$ is the n-th Bernoulli number.
*/
template<typename T>
constexpr
return_t<T>
tan(const T x)
noexcept
{
return internal::tan_check( static_cast<return_t<T>>(x) );
}
#endif