E.V.E
v2022.03.00

◆ average

eve::average = {}
inlineconstexpr

Computes the arithmetic mean of its arguments.

Defined in Header

#include <eve/module/core.hpp>

Callable Signatures

namespace eve
{
template< eve::value T, eve::value U >
eve::common_compatible_t<T, U> average(T x, U y) noexcept;
template< eve::floating_value Ts ... >
eve::common_compatible_t<Ts ...> average(Ts ... xs) noexcept;
}
Definition: value.hpp:83
constexpr callable_average_ average
Computes the arithmetic mean of its arguments.
Definition: average.hpp:89
Definition: all_of.hpp:22

Parameters

Return value

The value of the arithmetic mean of the arguments is returned.

Note
  • For two parameters half the sum of x and y. No overflow occurs.
  • For more than two parameters only floating or complex entries are allowed. No overflow occurs.
  • If x and y are integral values and the sum is odd, the result is a rounded value at a distance guaranteed to be less than or equal to 0.5 of the average floating value, but may differ by unity from the truncation given by (x+y)/2. Moreover, as some architectures provide simd intrinsics to perform the operation, the scalar results may differ by one unit from simd ones which are system dependent.

Example

#include <eve/module/core.hpp>
#include <eve/wide.hpp>
#include <vector>
#include <iostream>
float mean(std::vector<float> ary) {
float avg = 0;
int t = 1;
for (float x : ary) {
avg += (x - avg) / t;
++t;
}
return avg;
}
int main()
{
w_t pi = {3, 2, 3, 3}, qi = {4, 1, 1, ~0};
std::cout << "---- simd" << '\n'
<< " <- pi = " << pi << '\n'
<< " <- qi = " << qi << '\n'
<< " -> average(pi, qi) = " << eve::average(pi, qi) << '\n';
std::uint32_t xi = 3, yi = 4;
std::cout << "---- scalar" << '\n'
<< " xi = " << xi << '\n'
<< " yi = " << yi << '\n'
<< " -> average(xi, yi) = " << eve::average(xi, yi) << '\n';
w_ft pf = {3, 4, 3, 10}, qf = {4, 1, 1, 15};;
std::cout << "---- multi" << '\n'
<< " <- pf = " << pf << '\n'
<< " <- qf = " << qf << '\n'
<< " -> average(pf, 0.0f, qf, pf, 11.0f) = " << eve::average(pf, 0.0f, qf, pf, 11.0f) << '\n';
std::cout << "---- multi parameters" << '\n';
return 0;
}
constexpr callable_pi_ pi
Callable object computing the constant .
Definition: pi.hpp:49
Wrapper for SIMD registers.
Definition: wide.hpp:65

Semantic Modifiers

  • Masked Call

    The call eve::average[mask](x, ...) provides a masked version of average which is equivalent to if_else(mask, average(x, ...), x)

    Example

    #include <eve/module/core.hpp>
    #include <eve/wide.hpp>
    #include <vector>
    #include <iostream>
    int main()
    {
    w_t pi = {3, 2, 3, 3}, qi = {4, 1, 1, ~0};
    std::cout << "---- simd" << '\n'
    << " <- pi = " << pi << '\n'
    << " <- qi = " << qi << '\n'
    << " -> average[pi > qi](pi, qi) = " << eve::average[pi > qi](pi, qi) << '\n';
    return 0;
    }
  • eve::raw

    when raw(average)(x, args, ...) is used, no provision is made to avoid overflows for more than 2 parameters.

    Example

    #include <eve/module/core.hpp>
    #include <eve/wide.hpp>
    #include <vector>
    #include <iostream>
    int main()
    {
    w_t pi = {3, 2, 3, 3},
    qi = {4, 1, 1, eve::valmax(eve::as<float>())},
    ri = {4, 1, 1, eve::valmax(eve::as<float>())};
    std::cout << "---- simd" << '\n'
    << " <- pi = " << pi << '\n'
    << " <- qi = " << qi << '\n'
    << " <- ri = " << ri << '\n'
    << " -> raw(average)(pi, qi, ri) = " << eve::raw(eve::average)(pi, qi, ri) << '\n'
    << " -> average(pi, qi, ri) = " << eve::average(pi, qi, ri) << '\n';
    return 0;
    }
    constexpr callable_valmax_ valmax
    Computes the the greatest representable value.
    Definition: valmax.hpp:55
    constexpr raw_type const raw
    Higher-order Callable Object imbuing quick and dirty behaviour onto other Callable Objects.
    Definition: raw.hpp:43
    Lightweight type-wrapper.
    Definition: as.hpp:29