zr. - 7 months ago 49

C Question

Addition mathematically holds the associative property:

`(a + b) + c = a + (b + c)`

In the general case, this property does not hold for floating-point numbers because they represent values in a finite precision.

Is a compiler allowed to make the above substitution when generating machine code from a C program as part of an optimization? Where does it exactly say in the C standard?

Answer

The compiler is not allowed to perform "optimizations", which would result in a different value computed, than the one computed according to abstract machine semantics.

5.1.2.3 Program execution

[#1] The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant.

[#3] In the abstract machine, all expressions are evaluated as specified by the semantics.

[#13] EXAMPLE 5 Rearrangement for floating-point expressions is often restricted because of limitations in precision as well as range. The implementation cannot generally apply the mathematical associative rules for addition or multiplication, nor the distributive rule, because of roundoff error, even in the absence of overflow and underflow.

In your example:

```
(a + b) + c
```

or even without the parentheses:

```
a + b + c
```

we have

```
+
/ \
+ c
/ \
a b
```

and the compiler is required to generate code as if `a`

is summed with `b`

and the result is summed with `c`

.