WPILibC++ 2023.4.3-108-ge5452e3
sin.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 sine function using tan(x/2)
23 *
24 * see eq. 5.4.8 in Numerical Recipes
25 */
26
27#ifndef _gcem_sin_HPP
28#define _gcem_sin_HPP
29
30namespace internal
31{
32
33template<typename T>
34constexpr
35T
36sin_compute(const T x)
37noexcept
38{
39 return T(2)*x/(T(1) + x*x);
40}
41
42template<typename T>
43constexpr
44T
45sin_check(const T x)
46noexcept
47{
48 return( // NaN check
49 is_nan(x) ? \
50 GCLIM<T>::quiet_NaN() :
51 // indistinguishable from zero
52 GCLIM<T>::min() > abs(x) ? \
53 T(0) :
54 // special cases: pi/2 and pi
55 GCLIM<T>::min() > abs(x - T(GCEM_HALF_PI)) ? \
56 T(1) :
57 GCLIM<T>::min() > abs(x + T(GCEM_HALF_PI)) ? \
58 - T(1) :
59 GCLIM<T>::min() > abs(x - T(GCEM_PI)) ? \
60 T(0) :
61 GCLIM<T>::min() > abs(x + T(GCEM_PI)) ? \
62 - T(0) :
63 // else
64 sin_compute( tan(x/T(2)) ) );
65}
66
67}
68
69/**
70 * Compile-time sine function
71 *
72 * @param x a real-valued input.
73 * @return the sine function using \f[ \sin(x) = \frac{2\tan(x/2)}{1+\tan^2(x/2)} \f]
74 */
75
76template<typename T>
77constexpr
78return_t<T>
79sin(const T x)
80noexcept
81{
82 return internal::sin_check( static_cast<return_t<T>>(x) );
83}
84
85#endif
#define GCEM_PI
Definition: gcem_options.hpp:98
#define GCEM_HALF_PI
Definition: gcem_options.hpp:118
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 sin_compute(const T x) noexcept
Definition: sin.hpp:36
constexpr T sin_check(const T x) noexcept
Definition: sin.hpp:45
constexpr return_t< T > sin(const T x) noexcept
Compile-time sine function.
Definition: sin.hpp:79