24#ifndef MYSQL_MATH_SUMMATION_H
25#define MYSQL_MATH_SUMMATION_H
45template <
class Value_tp =
double>
87template <
class Value_t =
double, std::input_iterator Iterator_t>
88[[nodiscard]] Value_t
kahan_sum(
const Iterator_t &first,
89 const std::sentinel_for<Iterator_t>
auto &last,
91 return (Value_t)std::accumulate(first, last,
Kahan_sum(
init));
120template <
class Result_t =
long double, std::input_iterator Iterator1_t,
121 std::input_iterator Iterator2_t>
122 requires std::is_arithmetic_v<Result_t> &&
123 std::unsigned_integral<decltype(*std::declval<Iterator1_t>())> &&
124 std::unsigned_integral<
decltype(*std::declval<Iterator2_t>())>
126 const Iterator1_t &begin1,
const std::sentinel_for<Iterator1_t>
auto &end1,
127 const Iterator2_t &begin2,
const std::sentinel_for<Iterator2_t>
auto &end2,
133 auto step = [&](
auto &it) {
134 uint64_t
value = *it;
146 auto sum_tail = [&](
auto &it,
auto last) {
147 return kahan_sum(it, last, Result_t(sum));
160 return sum_tail(it1, end1);
167 auto ret = sum_tail(it2, end2);
169 if (ret > 0) ret = -ret;
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:566
Tracks the state of the Kahan summation algorithm, which produces a sum over a sequence of floating p...
Definition: summation.h:46
Kahan_sum(Value_t value=0)
Definition: summation.h:50
Kahan_sum operator-(const Value_t &value) const
Return a new object holding this object minus the given value.
Definition: summation.h:75
Value_t m_sum
Definition: summation.h:82
Kahan_sum & operator-=(const Value_t &value)
In-place subtract the given value from this object.
Definition: summation.h:65
Value_tp Value_t
Definition: summation.h:48
Value_t m_compensation
Definition: summation.h:83
Kahan_sum operator+(const Value_t &value) const
Return a new object holding the sum of this object and the given value.
Definition: summation.h:68
Kahan_sum & operator+=(const Value_t &value)
In-place add the given value to this object.
Definition: summation.h:56
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: bounded_arithmetic.h:34
Result_t sequence_sum_difference(const Iterator1_t &begin1, const std::sentinel_for< Iterator1_t > auto &end1, const Iterator2_t &begin2, const std::sentinel_for< Iterator2_t > auto &end2, uint64_t init=0)
Compute the sum of values in the first sequence, minus the sum of values in the second sequence.
Definition: summation.h:125
Value_t kahan_sum(const Iterator_t &first, const std::sentinel_for< Iterator_t > auto &last, Value_t init=0)
Compute the sum of values in the given range, with very low numeric error.
Definition: summation.h:88