If I run the code:

arr = Table[0.979, 249];
Last@Accumulate[arr] - Total[arr]

on Mathematica 11.0 (Windows), I get the result 1.53477*10^-12. I should instead be getting a result of 0, since Accumulate's last element should be the total of the array.

Also, this only happens when the array's length length is less than 250. The difference between the Total and Acumulate also seems to vary depending on the value of the elements inside the array, and for some values, Total and Accumulate will give the same result.

Something such as ListPlot@Table[(Accumulate[Table[a, 249]] - Total[Table[a, 249]])[[-1]], {a, 0, 1, 0.1}] shows an intresting graph of the difference between the two for various values:

enter image description here

My questions are:

  • Why is this happening? Is this a bug?
  • Why doesn't this happen for arrays larger than 250?
  • I need Accumulate to give me the same result as Total, so I'm currently zero-padding my arrays to 250 as a workaround. Is there a better soultion instead?

Edit: Even though Last@Accumulate[arr] - Total[arr] is 1.53477*10^-12, Table[Last@Accumulate[arr] - Total[arr], 250] gives an array of 0s (Table[1.53477*10^-12, 250] does not).

  • 2
    $\begingroup$ The 250 limit has to do with whether Table returns a packed array. Try arr = Developer`ToPackedArray[arr]. I would have guessed that the different result is simply due to the summation being done in a different order, but I don't understand how this relates to the array being packed. $\endgroup$ – Szabolcs Sep 21 '16 at 4:44
  • 4
    $\begingroup$ ... and use Method->"CompensatedSummation" for both and compare... $\endgroup$ – ciao Sep 21 '16 at 4:47
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/103771/… $\endgroup$ – Michael E2 Jul 8 at 3:38
  • $\begingroup$ I get a similar plot with tables over 250 in length in V12. $\endgroup$ – Michael E2 Jul 8 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.