Compilers
Why does a compiler with strict settings not reorder the floating-point sum (a + b) + c into a + (b + c) on its own?
- ARounding breaks associativity, so results can differ
- BReordering would increase the number of additions
- CHardware automatically decides the order of floating-point additions
Show answer and explanation
Answer: A. Rounding breaks associativity, so results can differ
Each floating-point addition rounds the result to representable digits, so changing the order can change the result slightly. Adding a tiny number to a huge one first may round the tiny number away, while adding the tiny numbers together first may preserve them. Compilers with strict settings therefore preserve the order; choosing settings that tolerate differences (such as fast-math options) may allow reordering for vectorization. The number of additions does not change.
- A. Correct. Rounding breaks associativity, so results can change.
- B. Reordering keeps the same number of additions.
- C. The order is set by the instruction sequence, not chosen by hardware.