On Apr 11, 3:37*pm, Kenneth Brody <kenbr...@spamcop.netwrote:

I see that my compiler will optimize it away for int, but not for

float. *Where would one find this prohibition in the standard?

Mathematically, "(a+b)-b" is the same as "a". *Of course, in this

case, there may be a loss of precision, but does the standard say

that you must preserve loss of precision? *Why doesn't the "as if"

rule apply to floating point?

The C Standard says that (a + b) - b means: Add a and b, subtract b

from the result. Same for integer and floating point. The C Standard

also says that the compiler is allowed to do absolutely anything it

likes, but only as long as the result is exactly what the C Standard

says. So the C Standard doesn't have to say "it's allowed for integer

and not allowed for floating-point". Instead, it is up to the compiler

writer to _prove_ that they produce exactly the same result.

And: "Mathematically" doesn't count. Actually, when you say

"mathematically" you mean "real numbers". Since the C Standard uses

floating-point numbers and not real numbers, it is irrelevant how real

numbers work.

Now an example: How could the compiler optimise this?

double f (int x) { double a = (double) x; double b = 1e300; return

(a + b) - b; }

(And no, it can't replace the return statement with "return a;" but

with something very different!)