Sum of all elements of a vector

Aug 17, 2015 at 4:26pm
Hello all,

I have a vector with float values and doubles, I am trying to sum all the elements of the vector. The problem is: it is only considering the number before decimal point.

 
  sum_of_elems = std::accumulate(MyVector.begin(), MyVector.end(), 0);  //this is what i m doing 


This is the data in the vector.
 
0,0,0.00737798,0.00737798,0.00737798,0.00737798,0.00737798,0.00737798,0.00737798,0,0


I need exact sum to calculate exact mean.
Last edited on Aug 17, 2015 at 4:27pm
Aug 17, 2015 at 4:30pm
Hi,

this should work
sum_of_elems = std::accumulate(MyVector.begin(), MyVector.end(), 0.0);
http://ideone.com/cD0mz5

Hope this helps
Last edited on Aug 17, 2015 at 4:32pm
Aug 21, 2015 at 10:18am
thank you!
Topic archived. No new replies allowed.