added noexcept guarantees to cumulative stats calculation

This commit is contained in:
joaquintides
2024-04-29 19:28:35 +02:00
parent 1ecb92deb5
commit 18797a3f32

View File

@ -44,8 +44,12 @@ struct cumulative_stats_data
struct welfords_algorithm /* 0-based */ struct welfords_algorithm /* 0-based */
{ {
template<typename T> template<typename T>
int operator()(T&& x,cumulative_stats_data& d)const int operator()(T&& x,cumulative_stats_data& d)const noexcept
{ {
static_assert(
noexcept(static_cast<double>(x)),
"Argument conversion to double must not throw.");
d.m_prior=d.m; d.m_prior=d.m;
d.m+=(static_cast<double>(x)-d.m)/static_cast<double>(n); d.m+=(static_cast<double>(x)-d.m)/static_cast<double>(n);
d.s+=(n!=1)* d.s+=(n!=1)*
@ -68,7 +72,7 @@ public:
void reset()noexcept{*this=cumulative_stats();} void reset()noexcept{*this=cumulative_stats();}
template<typename... Ts> template<typename... Ts>
void add(Ts&&... xs) void add(Ts&&... xs)noexcept
{ {
static_assert( static_assert(
sizeof...(Ts)==N,"A sample must be provided for each sequence."); sizeof...(Ts)==N,"A sample must be provided for each sequence.");
@ -122,7 +126,7 @@ public:
} }
template<typename... Ts> template<typename... Ts>
void add(Ts&&... xs) void add(Ts&&... xs)noexcept
{ {
lock_guard lck{mut}; lock_guard lck{mut};
super::add(std::forward<Ts>(xs)...); super::add(std::forward<Ts>(xs)...);