0

I am studying numerical analysis and I don't understand this: when adding lots of floating point numbers, the best way is to add up numbers from lowest to highest. Why is this more precise than adding up numbers from higher to lower?

melpomene
  • 84,125
  • 8
  • 85
  • 148
codingnight
  • 231
  • 1
  • 7
  • Possible duplicate of [Why adding from biggest to smallest floating-point numbers is less accurate than adding from smallest to biggest?](https://stackoverflow.com/questions/48124692/why-adding-from-biggest-to-smallest-floating-point-numbers-is-less-accurate-than) – Simon Byrne Feb 26 '18 at 19:54

2 Answers2

1

Say that you are using a decimal floating-point system with 3 digits of precision. If you add the positive numbers 3+3+4+40+70+300+800+1000 in this order, you get the partial sums: 6 10 50 120 420 1220 2220 The final result is the exact sum: there was never any approximation at any step.

If you add them the other way round, you get the partial sums: 1800 2100 2170 2210 2210 2210 2210

Floating-point means that there is more precision around 0. In general, the goal is to make each partial sum's result be as close to zero as possible, so that each partial sum is as accurate as possible. When summing numbers of the same sign, this means adding the smaller numbers first.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
0

When you add a small number and a big number you may loose a lot of precision. As a simple example try adding up 10 million 1's and 20000000, using (32 bit) floats. If you add up all the ones first (getting 10 million) and then add the 20 million, you get the right answer, 30 milion. But if you start with the 20 million and add 10 million ones you end up with 20 million. This is because, in 32 bit floating point, 20 million + 1 is 20 million.

dmuir
  • 4,211
  • 2
  • 14
  • 12