WPILibC++ 2023.4.3-108-ge5452e3
atan2.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 two-argument arctangent function
23 */
24
25#ifndef _gcem_atan2_HPP
26#define _gcem_atan2_HPP
27
28namespace internal
29{
30
31template<typename T>
32constexpr
33T
34atan2_compute(const T y, const T x)
35noexcept
36{
37 return( // NaN check
38 any_nan(y,x) ? \
39 GCLIM<T>::quiet_NaN() :
40 //
41 GCLIM<T>::min() > abs(x) ? \
42 //
43 GCLIM<T>::min() > abs(y) ? \
44 neg_zero(y) ? \
45 neg_zero(x) ? - T(GCEM_PI) : - T(0) :
46 neg_zero(x) ? T(GCEM_PI) : T(0) :
47 y > T(0) ? \
48 T(GCEM_HALF_PI) : - T(GCEM_HALF_PI) :
49 //
50 x < T(0) ? \
51 y < T(0) ? \
52 atan(y/x) - T(GCEM_PI) :
53 atan(y/x) + T(GCEM_PI) :
54 //
55 atan(y/x) );
56}
57
58template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
59constexpr
60TC
61atan2_type_check(const T1 y, const T2 x)
62noexcept
63{
64 return atan2_compute(static_cast<TC>(x),static_cast<TC>(y));
65}
66
67}
68
69/**
70 * Compile-time two-argument arctangent function
71 *
72 * @param y a real-valued input.
73 * @param x a real-valued input.
74 * @return \f[ \text{atan2}(y,x) = \begin{cases} \text{atan}(y/x) & \text{ if } x > 0 \\ \text{atan}(y/x) + \pi & \text{ if } x < 0 \text{ and } y \geq 0 \\ \text{atan}(y/x) - \pi & \text{ if } x < 0 \text{ and } y < 0 \\ + \pi/2 & \text{ if } x = 0 \text{ and } y > 0 \\ - \pi/2 & \text{ if } x = 0 \text{ and } y < 0 \end{cases} \f]
75 * The function is undefined at the origin, however the following conventions are used.
76 * \f[ \text{atan2}(y,x) = \begin{cases} +0 & \text{ if } x = +0 \text{ and } y = +0 \\ -0 & \text{ if } x = +0 \text{ and } y = -0 \\ +\pi & \text{ if } x = -0 \text{ and } y = +0 \\ - \pi & \text{ if } x = -0 \text{ and } y = -0 \end{cases} \f]
77 */
78
79template<typename T1, typename T2>
80constexpr
81common_return_t<T1,T2>
82atan2(const T1 y, const T2 x)
83noexcept
84{
86}
87
88#endif
constexpr common_return_t< T1, T2 > atan2(const T1 y, const T2 x) noexcept
Compile-time two-argument arctangent function.
Definition: atan2.hpp:82
#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
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
const Scalar & y
Definition: MathFunctions.h:821
Definition: Eigen_Colamd.h:50
constexpr TC atan2_type_check(const T1 y, const T2 x) noexcept
Definition: atan2.hpp:61
constexpr bool neg_zero(const T x) noexcept
Definition: neg_zero.hpp:31
constexpr T atan2_compute(const T y, const T x) noexcept
Definition: atan2.hpp:34
constexpr bool any_nan(const T1 x, const T2 y) noexcept
Definition: is_nan.hpp:45