Why does changing the sum order returns a different result?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In exact arithmetic, addition is associative and commutative, so reordering a sum does not change the answer. In floating-point arithmetic, the hardware is working with rounded approximations, so intermediate rounding can make different summation orders produce different results.
Why Exact Math and Computer Math Diverge
Real-number arithmetic assumes infinite precision. Computers do not have that luxury. A double stores a finite number of bits, so most decimal fractions are approximated.
When you add two floating-point values, the processor aligns their exponents and rounds the result back into the available precision. If one number is much larger than the other, the smaller number can partly or completely disappear during that rounding step.
That means:
- the mathematical rule still exists
- the machine approximation no longer behaves exactly like that rule
So the question is not “why did algebra stop working” but “where did the rounding happen.”
A Small Example
Python makes the issue easy to see:
One grouping prints 0.0, while the other prints 0.0 or 1.0 depending on the exact evaluation path and platform behavior. The core point is that the tiny 1.0 can be lost when combined with 1e16 too early.
A clearer demonstration is to compare naive summation with a more careful routine:
sum depends on order because it adds sequentially. math.fsum uses a more stable algorithm and is much closer to the mathematically expected result.
What Causes the Difference
Two effects matter most.
First, rounding error accumulates. Each intermediate result is rounded, so a long chain of additions is really a long chain of approximate additions.
Second, cancellation makes things worse. If two nearly equal large values with opposite signs are added, the remaining digits may contain mostly rounding noise. After that, adding small values can produce noticeably different answers depending on when the cancellation occurred.
This is why summing from smallest magnitude to largest often behaves better than summing in arbitrary order. Small values get combined with each other before they are swamped by very large ones.
Better Ways to Sum Floating-Point Data
The simplest improvement is to use a numerically stable summation routine when your language provides one. In Python, that is math.fsum. In numeric libraries, pairwise or tree-based summation is common because it reduces error growth compared with a strict left-to-right loop.
If you need decimal-style exactness for financial quantities, a binary floating-point type may be the wrong tool altogether. Python's Decimal type is slower but can represent decimal arithmetic more predictably.
That example is not a universal replacement for floating point, but it shows that the data type matters as much as the loop order.
Common Pitfalls
A common mistake is treating floating-point differences as proof of a compiler bug. Most of the time the compiler and CPU are behaving as specified.
Another mistake is comparing floating-point results with exact equality after a long computation. Use tolerances such as abs(a - b) < epsilon when appropriate.
People also assume parallel reduction is harmless. It is faster, but it changes the grouping of additions, so the final low-order bits can differ from a single-threaded sum.
Summary
- In exact math, sum order does not matter; in floating-point arithmetic, rounding makes order matter.
- Large and small magnitudes mixed together are especially sensitive to summation order.
- Cancellation can amplify small rounding differences into visible result changes.
- Stable routines such as
math.fsumreduce the problem significantly. - If exact decimal behavior matters, choose a more appropriate numeric type instead of relying on naive floating-point addition.
Related reading
- Why does Math.round(0.49999999999999994) return 1?
- Why does predict_proba function print the probabilities in reverse order?
- Why does Python's itertools.permutations contain duplicates? When the original list has duplicates
- Why does TensorFlow return nan nan instead of probabilities from a CSV file?
- why is e used so much in the NN?
- Why is squaring a number faster than multiplying two random numbers?
- Why is the Mean Average Percentage Errormape extremely high?
- Why k-d trees is not used for high dimensional data?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.