summing up a large single precision array

4 views (last 30 days)
Seongsu Jeong on 20 Nov 2014
Answered: Roger Stafford on 20 Nov 2014
Hi all,
I was trying to sum up the array of large single format array, I got significantly different results to what I got using double precision.
LCOI, which is the vector to be summed up, is a 25,233,291-by-1 single-precision array. Here are the results about summing it up in the different settings of precision.
---------------------------------------------------
>> sum(LCOI)
ans =
5.7487e+09
>> sum(double(LCOI))
ans =
4.9164e+09
>>
---------------------------------------------------
For more information, here is the histogram for the values of LCOI, plots of mean values calculated from the two data types. I suspected the overflow at the first time because the array is kinda large, but I found the maximum number that the single precision can express is far larger than that.
Could it be the lower "resolution" of single precision data than that of double precision? And is it possible that it can cause that 17% of difference?

Roger Stafford on 20 Nov 2014
When the cumulative sum of your array has reached, say, the halfway point around 2.5e9, in each further addition the size of the least significant bit of the 24-bit significand (mantissa) that single precision possesses, must necessarily have a value of 2^32/2^24 = 2^8. That is because this is a floating point format. This means the round-off errors are ranging from a whopping big -128 to +128 with each further addition, which is a large fraction of the average value of each addend, and you still have another twelve million additions to make. Work it out for yourself. Twelve million additions yet to perform with up to plus or minus 128 error at each step. The difference between your two results is certainly due to round-off error in the single precision format, and you are lucky to have only the amount of error you quote.
It is wildly inappropriate to attempt to perform twenty-five million additions with only single precision floating point numbers.