I have the following code which returns me the sum of the last count elements in a vector of doubles foo:
return std::accumulate(foo.rbegin().base() - std::min(count, foo.size()), foo.rbegin().base(), 0);
But it is ignoring any decimal part. Why?
It's surprisingly simple.
The type of the final parameter sets the type of the return of std::accumulate.
The simplest thing to do is use 0.0 in place of your final 0:
return std::accumulate(foo.rbegin().base() - std::min(count, foo.size()), foo.rbegin().base(), 0.0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With