std::accumulate
CXX-W2005std::accumulate
folds the elements to the initial value type (provided as the last argument to std::accumulate
). When the initial value type is narrower than that of the elements to sum, there is a loss in precision.
// Loss due to truncation
a = {0.1f, 0.2f, 0.3f}
std::accumulate(begin(a), end(a), 0);
// Loss due to overflow
a = {65536LL * 65536 * 65536};
std::accumulate(begin(a), end(a), 0);
a = {0.1f, 0.2f, 0.3f}
accumulate(begin(a), end(a), 0.0);
OR
a = {0.1f, 0.2f, 0.3f}
accumulate<vector<float>::iterator, float>(begin(a), end(a), 0);