Compilers

Why does a compiler with strict settings not reorder the floating-point sum (a + b) + c into a + (b + c) on its own?

  1. ARounding breaks associativity, so results can differ
  2. BReordering would increase the number of additions
  3. 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.
Practice this topic Back to topic

More Compilers questions

  1. Which kind of instruction does a compiler's automatic vectorization use?
  2. What basic condition must compiler optimizations respect?
  3. Which describes instruction selection, one stage of code generation?
  4. Which describes a cross compiler?
  5. When debugging a program compiled with optimization, some variables may show as "unavailable." What is the main reason?