I was trying to use the accumulate function for vectors
vector <double> A;
double B = 0;
A.reserve(100);
for(itr = 0; itr < 210; itr++)
{
term1 = pow(r[itr], 12);
term1 = 1/term1;
term2 = pow(r[itr], 6);
term2 = 2/term2;
A.push_back(term1 - term2);
}
B = accumulate(A.begin(), A.end(), 0);
however, I always got B = 0, while A had nonzero values
std::accumulateis a bit sneaky in the sense that the type of the result is the type of the initial value, and not the type of the container elements! So your accumulator producesints.To fix this, accumulate into a
double: