[R] Bug or feature? sum(c(a, b, c)) != (a + b + c)

Karl Ove Hufthammer karl at huftis.org
Wed Aug 24 11:26:55 CEST 2011


Thomas Lumley wrote:

>> So it looks as though sum(c(...)) is somwhow independent of
>> the order of its arguments, which implies that the summation
>> it does is not quite the addition corresponding to "+".
>>
>> I now feel out of my depth, so hope someone else knows/can
>> find the truth about this!
> 
> sum() uses a long double accumulator.

BTW, summing numbers is (surprisingly?) actually a quite interesting problem.
See, for example, the paper ‘A Comparison Of Methods For Accurate Summation’
by John Michael McNamee for some nice example. The paper is available online at
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.103.344&rep=rep1&type=pdf

-- 
Karl Ove Hufthammer



More information about the R-help mailing list