WPILibC++ 2024.3.2
atan2.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 two-argument arctangent function
23 */
24
25#ifndef _gcem_atan2_HPP
26#define _gcem_atan2_HPP
27
28#include <cmath>
29#include <type_traits>
30
31namespace gcem
32{
33
34namespace internal
35{
36
37template<typename T>
38constexpr
39T
40atan2_compute(const T y, const T x)
41noexcept
42{
43 return( // NaN check
44 any_nan(y,x) ? \
46 //
47 GCLIM<T>::min() > abs(x) ? \
48 //
49 GCLIM<T>::min() > abs(y) ? \
50 neg_zero(y) ? \
51 neg_zero(x) ? - T(GCEM_PI) : - T(0) :
52 neg_zero(x) ? T(GCEM_PI) : T(0) :
53 y > T(0) ? \
54 T(GCEM_HALF_PI) : - T(GCEM_HALF_PI) :
55 //
56 x < T(0) ? \
57 y < T(0) ? \
58 atan(y/x) - T(GCEM_PI) :
59 atan(y/x) + T(GCEM_PI) :
60 //
61 atan(y/x) );
62}
63
64template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
65constexpr
66TC
67atan2_type_check(const T1 y, const T2 x)
68noexcept
69{
70 return atan2_compute(static_cast<TC>(x),static_cast<TC>(y));
71}
72
73}
74
75/**
76 * Compile-time two-argument arctangent function
77 *
78 * @param y a real-valued input.
79 * @param x a real-valued input.
80 * @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]
81 * The function is undefined at the origin, however the following conventions are used.
82 * \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]
83 */
84
85template<typename T1, typename T2>
86constexpr
87common_return_t<T1,T2>
88atan2(const T1 y, const T2 x)
89noexcept
90{
93 } else {
94 return std::atan2(y, x);
95 }
96}
97
98}
99
100#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 atan2_compute(const T y, const T x) noexcept
Definition: atan2.hpp:40
constexpr bool neg_zero(const T x) noexcept
Definition: neg_zero.hpp:34
constexpr bool any_nan(const T1 x, const T2 y) noexcept
Definition: is_nan.hpp:48
constexpr TC atan2_type_check(const T1 y, const T2 x) noexcept
Definition: atan2.hpp:67
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 > atan(const T x) noexcept
Compile-time arctangent function.
Definition: atan.hpp:155
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74
constexpr common_return_t< T1, T2 > atan2(const T1 y, const T2 x) noexcept
Compile-time two-argument arctangent function.
Definition: atan2.hpp:88