WPILibC++ 2023.4.3
hypot.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 Pythagorean addition function
23 */
24
25// see: https://en.wikipedia.org/wiki/Pythagorean_addition
26
27#ifndef _gcem_hypot_HPP
28#define _gcem_hypot_HPP
29
30namespace internal
31{
32
33template<typename T>
34constexpr
35T
36hypot_compute(const T x, const T ydx)
37noexcept
38{
39 return abs(x) * sqrt( T(1) + (ydx * ydx) );
40}
41
42template<typename T>
43constexpr
44T
45hypot_vals_check(const T x, const T y)
46noexcept
47{
48 return( any_nan(x, y) ? \
49 GCLIM<T>::quiet_NaN() :
50 //
51 any_inf(x,y) ? \
52 GCLIM<T>::infinity() :
53 // indistinguishable from zero or one
54 GCLIM<T>::min() > abs(x) ? \
55 abs(y) :
56 GCLIM<T>::min() > abs(y) ? \
57 abs(x) :
58 // else
59 hypot_compute(x, y/x) );
60}
61
62template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
63constexpr
64TC
65hypot_type_check(const T1 x, const T2 y)
66noexcept
67{
68 return hypot_vals_check(static_cast<TC>(x),static_cast<TC>(y));
69}
70
71}
72
73/**
74 * Compile-time Pythagorean addition function
75 *
76 * @param x a real-valued input.
77 * @param y a real-valued input.
78 * @return Computes \f$ x \oplus y = \sqrt{x^2 + y^2} \f$.
79 */
80
81template<typename T1, typename T2>
82constexpr
83common_return_t<T1,T2>
84hypot(const T1 x, const T2 y)
85noexcept
86{
88}
89
90#endif
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
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_return_t< T1, T2 > hypot(const T1 x, const T2 y) noexcept
Compile-time Pythagorean addition function.
Definition: hypot.hpp:84
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 bool any_inf(const T1 x, const T2 y) noexcept
Definition: is_inf.hpp:137
constexpr T hypot_compute(const T x, const T ydx) noexcept
Definition: hypot.hpp:36
constexpr T hypot_vals_check(const T x, const T y) noexcept
Definition: hypot.hpp:45
constexpr TC hypot_type_check(const T1 x, const T2 y) noexcept
Definition: hypot.hpp:65
constexpr bool any_nan(const T1 x, const T2 y) noexcept
Definition: is_nan.hpp:45