Pitch: Method to sum numeric arrays

Perhaps the expectations on a sum() method provided by the Standard Library would be higher than just doing the obvious/naive thing?

Should it do something to reduce the numerical error when Element is floating point for example?

Seeing this code
let v = myLargeFloatArray.reduce(0, +)
it is at least clear what it does,
but seeing this:
let v = myLargeFloatArray.sum()
might suggest that some more precise summing algorithm is being used.

Should the following trap (because the particular order causes an overflow) or do the best that it can and print 7:

let a: [Int8] = [127, 8, -128]
print(a.sum())

?

13 Likes