WPILibC++ 2024.3.2
sin.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 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
30#include <cmath>
31#include <type_traits>
32
33namespace gcem
34{
35
36namespace internal
37{
38
39template<typename T>
40constexpr
41T
42sin_compute(const T x)
43noexcept
44{
45 return T(2)*x/(T(1) + x*x);
46}
47
48template<typename T>
49constexpr
50T
51sin_check(const T x)
52noexcept
53{
54 return( // NaN check
55 is_nan(x) ? \
57 // indistinguishable from zero
58 GCLIM<T>::min() > abs(x) ? \
59 T(0) :
60 // special cases: pi/2 and pi
61 GCLIM<T>::min() > abs(x - T(GCEM_HALF_PI)) ? \
62 T(1) :
63 GCLIM<T>::min() > abs(x + T(GCEM_HALF_PI)) ? \
64 - T(1) :
65 GCLIM<T>::min() > abs(x - T(GCEM_PI)) ? \
66 T(0) :
67 GCLIM<T>::min() > abs(x + T(GCEM_PI)) ? \
68 - T(0) :
69 // else
70 sin_compute( tan(x/T(2)) ) );
71}
72
73}
74
75/**
76 * Compile-time sine function
77 *
78 * @param x a real-valued input.
79 * @return the sine function using \f[ \sin(x) = \frac{2\tan(x/2)}{1+\tan^2(x/2)} \f]
80 */
81
82template<typename T>
83constexpr
84return_t<T>
85sin(const T x)
86noexcept
87{
89 return internal::sin_check( static_cast<return_t<T>>(x) );
90 } else {
91 return std::sin(x);
92 }
93}
94
95}
96
97#endif
#define GCEM_PI
Definition: gcem_options.hpp:98
#define GCEM_HALF_PI
Definition: gcem_options.hpp:118
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr T sin_check(const T x) noexcept
Definition: sin.hpp:51
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr T sin_compute(const T x) noexcept
Definition: sin.hpp:42
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 > sin(const T x) noexcept
Compile-time sine function.
Definition: sin.hpp:85
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