| 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/if_else.hpp> | ||
| 15 | #include <polyfloat/module/core/is_gtz.hpp> | ||
| 16 | |||
| 17 | namespace plf | ||
| 18 | { | ||
| 19 | |||
| 20 | template<typename Options> | ||
| 21 | struct heaviside_t : eve::strict_elementwise_callable<heaviside_t, Options, raw_option, pedantic_option> | ||
| 22 | { | ||
| 23 | 20 | template<concepts::polyfloat_like Z> POLYFLOAT_ABI constexpr Z operator()(Z z) const noexcept | |
| 24 | { | ||
| 25 | 20 | return POLYFLOAT_CALL(z); | |
| 26 | } | ||
| 27 | template<concepts::polyfloat_like Z1, concepts::polyfloat_like Z2> | ||
| 28 | 12 | POLYFLOAT_ABI constexpr as_polyfloat_t<Z1, Z2> operator()(Z1 z1, Z2 z2) const noexcept | |
| 29 | { | ||
| 30 | 12 | return POLYFLOAT_CALL(z1, z2); | |
| 31 | } | ||
| 32 | |||
| 33 | 32 | POLYFLOAT_CALLABLE_OBJECT(heaviside_t, heaviside_); | |
| 34 | }; | ||
| 35 | //====================================================================================================================== | ||
| 36 | //! @addtogroup core | ||
| 37 | //! @{ | ||
| 38 | //! @var heaviside | ||
| 39 | //! @brief return 1 or 0 according to strict positiveness of the input value. | ||
| 40 | //! | ||
| 41 | //! @groupheader{Header file} | ||
| 42 | //! | ||
| 43 | //! @code | ||
| 44 | //! #include <polyfloat/core.hpp> | ||
| 45 | //! @endcode | ||
| 46 | //! | ||
| 47 | //! @groupheader{Callable Signatures} | ||
| 48 | //! | ||
| 49 | //! @code | ||
| 50 | //! namespace polyfloat | ||
| 51 | //! { | ||
| 52 | //! // Regular overload | ||
| 53 | //! constexpr auto heaviside(value auto x) noexcept; // 1 | ||
| 54 | //! constexpr auto heaviside(value auto x, auto s) noexcept; // 2 | ||
| 55 | //! } | ||
| 56 | //! @endcode | ||
| 57 | //! | ||
| 58 | //! **Parameters** | ||
| 59 | //! | ||
| 60 | //! * `z`: Value to process. | ||
| 61 | //! * `s`: shift. | ||
| 62 | //! | ||
| 63 | //! **Return value** | ||
| 64 | //! 1. Each [element](@ref glossary_elementwise) of the result contains: | ||
| 65 | //! * `0`, if `x` is less or equal to zero. | ||
| 66 | //! * `1` otherwise. | ||
| 67 | //! 2. Each [element](@ref glossary_elementwise) of the result contains: | ||
| 68 | //! * `0`, if `x` is less or equal to `s` (default to zero). | ||
| 69 | //! * `1` otherwise. | ||
| 70 | //! | ||
| 71 | //! @groupheader{Example} | ||
| 72 | //! | ||
| 73 | //! @godbolt{doc/core/heaviside.cpp} | ||
| 74 | //====================================================================================================================== | ||
| 75 | |||
| 76 | inline constexpr auto heaviside = eve::functor<heaviside_t>; | ||
| 77 | //====================================================================================================================== | ||
| 78 | //! @} | ||
| 79 | //====================================================================================================================== | ||
| 80 | } | ||
| 81 | |||
| 82 | namespace plf::_ | ||
| 83 | { | ||
| 84 | template<typename Z, eve::callable_options O> | ||
| 85 | 20 | constexpr auto heaviside_(POLYFLOAT_DELAY(), O const&, Z const& a) noexcept | |
| 86 | { | ||
| 87 | 20 | return Z(eve::heaviside(hi(a))); | |
| 88 | } | ||
| 89 | |||
| 90 | template<typename Z1, typename Z2, eve::callable_options O> | ||
| 91 | 12 | constexpr auto heaviside_(POLYFLOAT_DELAY(), O const&, Z1 const& a, Z2 const& s) noexcept | |
| 92 | { | ||
| 93 | using t_t = as_polyfloat_t<Z1, Z2>; | ||
| 94 | 12 | return if_else(a > s, t_t(1), eve::zero); | |
| 95 | } | ||
| 96 | } | ||
| 97 |