#232 Overflow in sum() function and alike

release_4.3.x
open
nobody
1
2013-12-09
2013-10-28
J B
No

Hello,
the sum(const Vec< T > &v) function and most of the similar summation function have an overflow problem, as they use the same datatype for summing as the elements of the vector, see matfunc.h:

 --> 59 T sum(const Vec<T> &v)
     60 {
 --> 61   T M = 0;
     62 
     63   for (int i = 0;i < v.length();i++)
 --> 64     M += v[i];
     65 
     66   return M;
     67 }

So, an example in pseudocode:

    svec test = "10000 10000 10000 10000"; // valid svec
    cout << mean(test) << endl;  // fails, outputs -6384

Although all elements of the vector test are easily inside the valid range, the mean function fails, because it calls sum() internally. Sum uses type short for M, which will not suffice to hold the sum of all elements!

As a workaround, one needs to convert the whole vector to a larger datatype, just to make the summation succeed...

Discussion

  • Bogdan Cristea
    Bogdan Cristea
    2013-11-01

    I would say that rather the mean function needs fixing. For the sum function, it might be more problematic as choosing a larger numerical format for the accumulator could have an impact on performance. Anyway, patches and suggestions are always welcome.