The value being returned from the std::accumulate function is an integer, not a double because of this:
double sum = accumulate(V.begin(), V.end(), 0);
// ^-- Integer!
The template parameter inference done by the C++ compiler makes the return type of the accumulate
function the same as the init
parameter, which in your case is an integer. Therefore the return value is being rounded to the nearest whole number. The return value is then being implicitly cast back into a double when it's assigned into sum
.
To fix this, you can simply pass the zero as a double (i.e. 0.0
) instead.