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#ifndef TETL_NUMERIC_ABS_HPP
4#define TETL_NUMERIC_ABS_HPP
5
6#include <etl/_limits/numeric_limits.hpp>
7
8namespace etl {
9
10/// \brief Returns the absolute value.
11/// \ingroup numeric
12template <typename Type>
13[[nodiscard]] constexpr auto abs(Type input) noexcept -> Type
14{
15 using limits = etl::numeric_limits<Type>;
16 if constexpr (limits::is_signed or not limits::is_specialized) {
17 if (input < 0) {
18 return static_cast<Type>(-input);
19 }
20 return input;
21 } else {
22 return input;
23 }
24}
25
26} // namespace etl
27
28#endif // TETL_NUMERIC_ABS_HPP
constexpr auto abs(Type input) noexcept -> Type
Returns the absolute value.
Definition abs.hpp:13
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18