exactly so My example actually shows two problems with summing a huge number of small values (random numbers are between 0 and 1). The first, obvious is roundoff error and the second is the finite density of real values in the computer representation. In 4-bytes reals, above the magic value of 16777216, is it impossible to add a number less than one and get the result any different. Using higher precision accumulator helps with that. But, I must say, @ivanpribec’s trick with sum(real(t,kind(1d0)))
is very smart. I would not guess that there would be no temporary array built (which would be a killer for GB-sized array)
Edit: still, smart as it is, the trick depends fully on the smartness of compiler creators. It is by no means guaranteed. An optional kind
parameter of sum
could guarantee that.