tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
nextafter.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_NEXTAFTER_HPP
5#define TETL_CMATH_NEXTAFTER_HPP
6
7#include <etl/_bit/bit_cast.hpp>
8#include <etl/_cstdint/uint_t.hpp>
9#include <etl/_type_traits/conditional.hpp>
10#include <etl/_type_traits/is_same.hpp>
11
12namespace etl {
13
14namespace detail {
15
16template <typename T>
17[[nodiscard]] constexpr auto nextafter(T from, T to) -> T
18{
19 using U = etl::conditional_t<sizeof(T) == 4U, etl::uint32_t, etl::uint64_t>;
20 auto const fromBits = etl::bit_cast<U>(from);
21 auto const toBits = etl::bit_cast<U>(to);
22 if (toBits == fromBits) {
23 return to;
24 }
25 if (toBits > fromBits) {
26 return etl::bit_cast<T>(fromBits + 1);
27 }
28 return etl::bit_cast<T>(fromBits - 1);
29}
30} // namespace detail
31
32/// \ingroup cmath
33/// @{
34
35/// Returns the next representable value of from in the direction of to.
36/// If from equals to, to is returned.
37/// \details ttps://en.cppreference.com/w/cpp/numeric/math/nextafter
38[[nodiscard]] constexpr auto nextafter(float from, float to) noexcept -> float
39{
40 return detail::nextafter(from, to);
41}
42[[nodiscard]] constexpr auto nextafterf(float from, float to) noexcept -> float
43{
44 return detail::nextafter(from, to);
45}
46[[nodiscard]] constexpr auto nextafter(double from, double to) noexcept -> double
47{
48 return detail::nextafter(from, to);
49}
50
51/// @}
52
53} // namespace etl
54
55#endif // TETL_CMATH_NEXTAFTER_HPP
constexpr auto nextafter(double from, double to) noexcept -> double
Definition nextafter.hpp:46
constexpr auto nextafterf(float from, float to) noexcept -> float
Definition nextafter.hpp:42
constexpr auto nextafter(float from, float to) noexcept -> float
Definition nextafter.hpp:38
Definition adjacent_find.hpp:9