WPILibC++ 2024.3.2
acos.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 arccosine function
23 */
24
25#ifndef _gcem_acos_HPP
26#define _gcem_acos_HPP
27
28#include <cmath>
29#include <type_traits>
30
31namespace gcem
32{
33
34namespace internal
35{
36
37template<typename T>
38constexpr
39T
40acos_compute(const T x)
41noexcept
42{
43 return( // only defined on [-1,1]
44 abs(x) > T(1) ? \
46 // indistinguishable from one or zero
47 GCLIM<T>::min() > abs(x - T(1)) ? \
48 T(0) :
49 GCLIM<T>::min() > abs(x) ? \
50 T(GCEM_HALF_PI) :
51 // else
52 atan( sqrt(T(1) - x*x)/x ) );
53}
54
55template<typename T>
56constexpr
57T
58acos_check(const T x)
59noexcept
60{
61 return( // NaN check
62 is_nan(x) ? \
64 //
65 x > T(0) ? \
66 // if
67 acos_compute(x) :
68 // else
69 T(GCEM_PI) - acos_compute(-x) );
70}
71
72}
73
74/**
75 * Compile-time arccosine function
76 *
77 * @param x a real-valued input, where \f$ x \in [-1,1] \f$.
78 * @return the inverse cosine function using \f[ \text{acos}(x) = \text{atan} \left( \frac{\sqrt{1-x^2}}{x} \right) \f]
79 */
80
81template<typename T>
82constexpr
83return_t<T>
84acos(const T x)
85noexcept
86{
88 return internal::acos_check( static_cast<return_t<T>>(x) );
89 } else {
90 return std::acos(x);
91 }
92}
93
94}
95
96#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 bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr T acos_compute(const T x) noexcept
Definition: acos.hpp:40
constexpr T acos_check(const T x) noexcept
Definition: acos.hpp:58
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
constexpr return_t< T > sqrt(const T x) noexcept
Compile-time square-root function.
Definition: sqrt.hpp:109
constexpr return_t< T > acos(const T x) noexcept
Compile-time arccosine function.
Definition: acos.hpp:84
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