tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
abs.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_MATH_ABS_HPP
5#define TETL_MATH_ABS_HPP
6
7namespace etl {
8namespace detail {
9
10template <typename T>
11[[nodiscard]] constexpr auto abs_impl(T n) noexcept -> T
12{
13 // constexpr auto isInt = is_same_v<T, int>;
14 // constexpr auto isLong = is_same_v<T, long>;
15 // constexpr auto isLongLong = is_same_v<T, long long>;
16 // static_assert(isInt || isLong || isLongLong);
17
18 if (n >= T(0)) {
19 return n;
20 }
21 return n * T(-1);
22}
23
24} // namespace detail
25
26/// \brief Computes the absolute value of an integer number. The behavior is
27/// undefined if the result cannot be represented by the return type. If abs
28/// is called with an unsigned integral argument that cannot be converted to int
29/// by integral promotion, the program is ill-formed.
30[[nodiscard]] constexpr auto abs(int n) noexcept -> int
31{
32 return detail::abs_impl<int>(n);
33}
34
35[[nodiscard]] constexpr auto abs(long n) noexcept -> long
36{
37 return detail::abs_impl<long>(n);
38}
39
40[[nodiscard]] constexpr auto abs(long long n) noexcept -> long long
41{
42 return detail::abs_impl<long long>(n);
43}
44
45[[nodiscard]] constexpr auto abs(float n) noexcept -> float
46{
47 return detail::abs_impl<float>(n);
48}
49
50[[nodiscard]] constexpr auto abs(double n) noexcept -> double
51{
52 return detail::abs_impl<double>(n);
53}
54
55[[nodiscard]] constexpr auto abs(long double n) noexcept -> long double
56{
57 return detail::abs_impl<long double>(n);
58}
59
60[[nodiscard]] constexpr auto fabs(float n) noexcept -> float
61{
62 return detail::abs_impl<float>(n);
63}
64
65[[nodiscard]] constexpr auto fabsf(float n) noexcept -> float
66{
67 return detail::abs_impl<float>(n);
68}
69
70[[nodiscard]] constexpr auto fabs(double n) noexcept -> double
71{
72 return detail::abs_impl<double>(n);
73}
74
75[[nodiscard]] constexpr auto fabs(long double n) noexcept -> long double
76{
77 return detail::abs_impl<long double>(n);
78}
79
80[[nodiscard]] constexpr auto fabsl(long double n) noexcept -> long double
81{
82 return detail::abs_impl<long double>(n);
83}
84
85} // namespace etl
86
87#endif // TETL_MATH_ABS_HPP
Definition adjacent_find.hpp:9
constexpr auto fabsl(long double n) noexcept -> long double
Definition abs.hpp:80
constexpr auto abs(long long n) noexcept -> long long
Definition abs.hpp:40
constexpr auto abs(long n) noexcept -> long
Definition abs.hpp:35
constexpr auto abs(double n) noexcept -> double
Definition abs.hpp:50
constexpr auto abs(int n) noexcept -> int
Computes the absolute value of an integer number. The behavior is undefined if the result cannot be r...
Definition abs.hpp:30
constexpr auto abs(long double n) noexcept -> long double
Definition abs.hpp:55
constexpr auto fabsf(float n) noexcept -> float
Definition abs.hpp:65
constexpr auto fabs(float n) noexcept -> float
Definition abs.hpp:60
constexpr auto fabs(double n) noexcept -> double
Definition abs.hpp:70
constexpr auto abs(float n) noexcept -> float
Definition abs.hpp:45
constexpr auto fabs(long double n) noexcept -> long double
Definition abs.hpp:75