E.V.E  0.1-beta

◆ clamp

eve::clamp = {}
inlineconstexpr

Callable object clamping a value between two others.

Required header: #include <eve/function/clamp.hpp>

Members Functions

Member Effect
operator() the clamping operation
operator[] Construct a conditional version of current function object

template< real_value T, real_value U, real_value V > auto operator()( T x, U lo, V hi ) const noexcept
requires compatible< T, U > && compatible< T, V >;
constexpr callable_hi_ hi
Callable object computing the higher part of the values.
Definition: hi.hpp:59
constexpr callable_lo_ lo
Callable object computing the lower part of the values.
Definition: lo.hpp:59

Parameters

x: value to clamp.

lo, hi: the boundary values to clamp x to.

Return value

Each element of the result contains:

  • lo, if x is less than lo.
  • hi, if hi is less than x.
  • otherwise x.

The result type is the common compatible type of the three parameters.

Warning
Contrary to the standard implementation clamp does not return a reference.

auto operator[]( conditional_expression auto cond ) const noexcept;

Higher-order function generating a masked version of eve::clamp

Parameters

cond : conditional expression

Return value

A Callable object so that the expression clamp[cond](x, ...) is equivalent to if_else(cond,clamp(x, ...),x)


Supported decorators

  • eve::diff, eve::diff_1st, eve::diff_2nd, eve::diff_3rd, eve::diff_nth

    Required header: #include <eve/function/diff/clamp.hpp>

    The expression diff_1st(clamp)(x,y,z), diff_2nd(clam)(x,y,z) and diff_3rd(clam)(x,y,z) computes the partial derivatives of \(f\), where \(f\) is the function \((x,y,z) \rightarrow \ \mbox{clamp}(x,y,z)\).

Example

See it live on Compiler Explorer

#include <eve/function/clamp.hpp>
#include <eve/wide.hpp>
#include <iostream>
int main()
{
wide_t xi = {2, -3, 0, 4};
wide_t lo = {3, -2, -10, 0};
wide_t hi = {4, -1, 0, 5};
std::cout << "---- ximd" << '\n'
<< " <- xi = " << xi << '\n'
<< " <- lo = " << lo << '\n'
<< " <- hi = " << hi << '\n'
<< " -> clamp(xi, lo, hi) = " << eve::clamp(xi, lo, hi) << '\n';
float sxi = 3, slo = 3, shi = 4;
std::cout << "---- scalar" << '\n'
<< " sxi = " << sxi << '\n'
<< " slo = " << slo << '\n'
<< " shi = " << shi << '\n'
<< " -> clamp(sxii, slo, shi) = " << eve::clamp(sxi, slo, shi) << '\n';
return 0;
}
constexpr callable_clamp_ clamp
Callable object clamping a value between two others.
Definition: clamp.hpp:104
Wrapper for SIMD registers.
Definition: wide.hpp:65