Possible Duplicate:
C++ float precision question
I've got a problem of determining the most precise method of the three to calculate the sum of vector elements, which can be only positive numbers, using std::accumulate.
1)
double sum(vector<float> &v)
{
return accumulate(v.begin(), v.end(), 0.0);
}
2)
double sum(vector<float> &v)
{
sort(v.begin(), v.end());
return accumulate(v.begin(), v.end(), 0.0);
}
3)
double sum(vector<float> &v)
{
sort(v.begin(), v.end(), greater<float>());
return accumulate(v.begin(), v.end(), 0.0);
}
This is a kind of job interview question, that's why I got these particular three ways to calculate the sum. I've done a lot of searching the web, but couldn't figure out the difference. Could you please help me guys understand it?