tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
hypot.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CMATH_HYPOT_HPP
5#define TETL_CMATH_HYPOT_HPP
6
7#include <etl/_config/all.hpp>
8
9#include <etl/_cmath/isinf.hpp>
10#include <etl/_cmath/isnan.hpp>
11#include <etl/_cmath/sqrt.hpp>
12#include <etl/_limits/numeric_limits.hpp>
13
14namespace etl {
15
16namespace detail {
17
18inline constexpr struct hypot {
19 template <typename Float>
20 [[nodiscard]] constexpr auto operator()(Float x, Float y) const noexcept -> Float
21 {
22 if (etl::isinf(x) or etl::isinf(y)) {
23 return etl::numeric_limits<Float>::infinity();
24 }
25 if (etl::isnan(x) or etl::isnan(y)) {
26 return etl::numeric_limits<Float>::quiet_NaN();
27 }
28 return etl::sqrt(x * x + y * y);
29 }
30
31 template <typename Float>
32 [[nodiscard]] constexpr auto operator()(Float x, Float y, Float z) const noexcept -> Float
33 {
34 if (etl::isinf(x) or etl::isinf(y) or etl::isinf(z)) {
35 return etl::numeric_limits<Float>::infinity();
36 }
37 if (etl::isnan(x) or etl::isnan(y) or etl::isnan(z)) {
38 return etl::numeric_limits<Float>::quiet_NaN();
39 }
40 return etl::sqrt(x * x + y * y + z * z);
41 }
42
43} hypot;
44
45} // namespace detail
46
47/// \ingroup cmath
48/// @{
49
50/// Computes the square root of the sum of the squares of x and y,
51/// without undue overflow or underflow at intermediate stages of the
52/// computation.
53///
54/// - hypot(x,y) is INF if x or y is +INF or -INF; else
55/// - hypot(x,y) is NAN if x or y is NAN.
56///
57/// https://en.cppreference.com/w/cpp/numeric/math/hypot
58[[nodiscard]] constexpr auto hypot(float x, float y) noexcept -> float
59{
60 return etl::detail::hypot(x, y);
61}
62
63[[nodiscard]] constexpr auto hypotf(float x, float y) noexcept -> float
64{
65 return etl::detail::hypot(x, y);
66}
67
68[[nodiscard]] constexpr auto hypot(double x, double y) noexcept -> double
69{
70 return etl::detail::hypot(x, y);
71}
72
73[[nodiscard]] constexpr auto hypot(long double x, long double y) noexcept -> long double
74{
75 return etl::detail::hypot(x, y);
76}
77
78[[nodiscard]] constexpr auto hypotl(long double x, long double y) noexcept -> long double
79{
80 return etl::detail::hypot(x, y);
81}
82
83[[nodiscard]] constexpr auto hypot(float x, float y, float z) noexcept -> float
84{
85 return etl::detail::hypot(x, y, z);
86}
87
88[[nodiscard]] constexpr auto hypot(double x, double y, double z) noexcept -> double
89{
90 return etl::detail::hypot(x, y, z);
91}
92
93[[nodiscard]] constexpr auto hypot(long double x, long double y, long double z) noexcept -> long double
94{
95 return etl::detail::hypot(x, y, z);
96}
97
98/// @}
99
100} // namespace etl
101
102#endif // TETL_CMATH_HYPOT_HPP
constexpr auto hypot(long double x, long double y, long double z) noexcept -> long double
Definition hypot.hpp:93
constexpr auto hypot(double x, double y, double z) noexcept -> double
Definition hypot.hpp:88
constexpr auto hypotf(float x, float y) noexcept -> float
Definition hypot.hpp:63
constexpr auto hypot(long double x, long double y) noexcept -> long double
Definition hypot.hpp:73
constexpr auto hypot(double x, double y) noexcept -> double
Definition hypot.hpp:68
constexpr auto hypotl(long double x, long double y) noexcept -> long double
Definition hypot.hpp:78
constexpr auto hypot(float x, float y, float z) noexcept -> float
Definition hypot.hpp:83
constexpr auto hypot(float x, float y) noexcept -> float
Definition hypot.hpp:58
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18