WPILibC++ 2023.4.3
acos.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 arccosine function
23 */
24
25#ifndef _gcem_acos_HPP
26#define _gcem_acos_HPP
27
28namespace internal
29{
30
31template<typename T>
32constexpr
33T
34acos_compute(const T x)
35noexcept
36{
37 return( // only defined on [-1,1]
38 abs(x) > T(1) ? \
39 GCLIM<T>::quiet_NaN() :
40 // indistinguishable from one or zero
41 GCLIM<T>::min() > abs(x - T(1)) ? \
42 T(0) :
43 GCLIM<T>::min() > abs(x) ? \
44 T(GCEM_HALF_PI) :
45 // else
46 atan( sqrt(T(1) - x*x)/x ) );
47}
48
49template<typename T>
50constexpr
51T
52acos_check(const T x)
53noexcept
54{
55 return( // NaN check
56 is_nan(x) ? \
57 GCLIM<T>::quiet_NaN() :
58 //
59 x > T(0) ? \
60 // if
61 acos_compute(x) :
62 // else
63 T(GCEM_PI) - acos_compute(-x) );
64}
65
66}
67
68/**
69 * Compile-time arccosine function
70 *
71 * @param x a real-valued input, where \f$ x \in [-1,1] \f$.
72 * @return the inverse cosine function using \f[ \text{acos}(x) = \text{atan} \left( \frac{\sqrt{1-x^2}}{x} \right) \f]
73 */
74
75template<typename T>
76constexpr
77return_t<T>
78acos(const T x)
79noexcept
80{
81 return internal::acos_check( static_cast<return_t<T>>(x) );
82}
83
84#endif
constexpr return_t< T > acos(const T x) noexcept
Compile-time arccosine function.
Definition: acos.hpp:78
#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
angle::radian_t atan(const ScalarUnit x) noexcept
Compute arc tangent.
Definition: math.h:159
auto sqrt(const UnitType &value) noexcept -> unit_t< square_root< typename units::traits::unit_t_traits< UnitType >::unit_type >, typename units::traits::unit_t_traits< UnitType >::underlying_type, linear_scale >
computes the square root of value
Definition: math.h:483
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 acos_compute(const T x) noexcept
Definition: acos.hpp:34
constexpr T acos_check(const T x) noexcept
Definition: acos.hpp:52