| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | POLYFLOAT - Extended precision floating points | ||
| 4 | Copyright : POLYFLOAT Contributors & Maintainers | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <polyfloat/details/callable.hpp> | ||
| 11 | #include <polyfloat/types/concepts.hpp> | ||
| 12 | #include <polyfloat/types/traits.hpp> | ||
| 13 | #include <type_traits> | ||
| 14 | #include <polyfloat/module/core/trunc.hpp> | ||
| 15 | #include <polyfloat/module/core/nearest.hpp> | ||
| 16 | #include <polyfloat/module/core/ceil.hpp> | ||
| 17 | #include <polyfloat/module/core/floor.hpp> | ||
| 18 | |||
| 19 | namespace plf | ||
| 20 | { | ||
| 21 | |||
| 22 | template<typename Options> | ||
| 23 | struct round_t : eve::callable<round_t, | ||
| 24 | Options, | ||
| 25 | raw_option, | ||
| 26 | pedantic_option, | ||
| 27 | to_nearest_option, | ||
| 28 | toward_zero_option, | ||
| 29 | downward_option, | ||
| 30 | upward_option> | ||
| 31 | { | ||
| 32 | 32 | template<concepts::polyfloat_like Z> POLYFLOAT_ABI constexpr as_polyfloat_like_t<Z> operator()(Z z) const noexcept | |
| 33 | { | ||
| 34 | 32 | return POLYFLOAT_CALL(z); | |
| 35 | } | ||
| 36 | |||
| 37 | 32 | POLYFLOAT_CALLABLE_OBJECT(round_t, round_); | |
| 38 | }; | ||
| 39 | //====================================================================================================================== | ||
| 40 | //! @addtogroup core | ||
| 41 | //! @{ | ||
| 42 | //! @var round | ||
| 43 | //! @brief rounds a value, to nearest unless an option says otherwise. | ||
| 44 | //! | ||
| 45 | //! @groupheader{Header file} | ||
| 46 | //! | ||
| 47 | //! @code | ||
| 48 | //! #include <polyfloat/core.hpp> | ||
| 49 | //! @endcode | ||
| 50 | //! | ||
| 51 | //! @groupheader{Callable Signatures} | ||
| 52 | //! | ||
| 53 | //! @code | ||
| 54 | //! namespace polyfloat | ||
| 55 | //! { | ||
| 56 | //! template<polyfloat::concepts::polyfloat_like T> constexpr auto round(T z) noexcept; //1 | ||
| 57 | //! | ||
| 58 | //! // Semantic exclusive options | ||
| 59 | //! constexpr auto round[upward](/*any of the above overloads*/) noexcept; // 2 | ||
| 60 | //! constexpr auto round[downward](/*any of the above overloads*/) noexcept; // 2 | ||
| 61 | //! constexpr auto round[toward_zero](/*any of the above overloads*/) noexcept; // 2 | ||
| 62 | //! constexpr auto round[to_nearest](/*any of the above overloads*/) noexcept; // 2 | ||
| 63 | //! } | ||
| 64 | //! @endcode | ||
| 65 | //! | ||
| 66 | //! **Parameters** | ||
| 67 | //! | ||
| 68 | //! * `z`: Values to process. | ||
| 69 | //! | ||
| 70 | //! **Return value** | ||
| 71 | //! | ||
| 72 | //! 1. Returns the `plf::nearest(z)`. | ||
| 73 | //! 2. P roduces: | ||
| 74 | //! * `plf::trunc(z)`, if `d` is `toward_zero`. | ||
| 75 | //! * `plf::floor(z)`, if `d` is `downward`. | ||
| 76 | //! * `plf::ceil(z)`, if `d` is `upward`. | ||
| 77 | //! * `plf::nearest(z)`, if `d` is `to_nearest`. | ||
| 78 | //! | ||
| 79 | //! @groupheader{Example} | ||
| 80 | //! | ||
| 81 | //! @godbolt_todo{doc/core/round.cpp} | ||
| 82 | //====================================================================================================================== | ||
| 83 | |||
| 84 | inline constexpr auto round = eve::functor<round_t>; | ||
| 85 | //====================================================================================================================== | ||
| 86 | //! @} | ||
| 87 | //====================================================================================================================== | ||
| 88 | } | ||
| 89 | |||
| 90 | namespace plf::_ | ||
| 91 | { | ||
| 92 | 32 | template<typename T, eve::callable_options O> constexpr auto round_(POLYFLOAT_DELAY(), O const&, T a) noexcept | |
| 93 | { | ||
| 94 | 8 | if constexpr (O::contains(toward_zero)) return trunc(a); | |
| 95 | 8 | else if constexpr (O::contains(downward)) return floor(a); | |
| 96 | 8 | else if constexpr (O::contains(upward)) return ceil(a); | |
| 97 | 8 | else return nearest(a); | |
| 98 | } | ||
| 99 | } | ||
| 100 |