4

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?

Community
  • 1
  • 1
rightaway717
  • 2,631
  • 3
  • 29
  • 43
  • 1
    What do you mean by, "a kind of job interview question"? Are you asking for help to pass a job interview? – Marcelo Cantos Jul 23 '11 at 13:53
  • Sort by absolute value and start at the small end. You can think about why that is. – Kerrek SB Jul 23 '11 at 13:54
  • @Marcelo Cantos No, I'm not asking to pass a job interview. This task was once at a job interview, so I'm just preparing for a job interview, doing different tasks – rightaway717 Jul 23 '11 at 13:59

1 Answers1

4

The difference should be really small, but starting with the smaller numbers will be slightly more accurate. Consider for exposition purposes that your floating point number contained only 4 significant digits and an exponent, and that it was decimal rather than binary. Using the numbers:

a = 5000
b = 5000
c = 1000e4 (10000000)

If we add c first, then either a or b, the smaller of the two falls off the representation and is rounded. The end result of c + b + a will yield 1000e4. If on the other hand, we add a and b first we get 1e4 as the first intermediate value, and adding that to c will yield 1001e4 which is a more precise result for the operation.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489