C++ std::accumulate doesn’t give the expected sum

You should write the following:

std::cout << 
 std::accumulate ( doublenumbers.begin( ) , doublenumbers.end( ) , 0.0 ) ;

Because the type of 0 is int.

When std::accumulate is instantiated with the type of the third argument is int, then it would convert the right hand side of the sum. e.g.:

   result += *iter;
// int    += double

This would force a conversion of double to int, instead of what you were thinking of which is the opposite.

Leave a Comment