Compiler allowed to "simplify" floating-point expressions?

Greg Herlihy writes:

> On Oct 8, 2:29 pm, Artie Choke wrote:
>> On Oct 7, 8:22 pm, Greg Herlihy wrote:
>> > On Oct 5, 2:24 pm, Alan McKenney wrote:
>> > > double x;
>> > > ....
>> > > if ( ( x + 1.0 ) - 1.0 )
>> > > as
>> > > double x;
>> > > ....
>> > > if ( x )
>>
>> > Yes. Adding and subtracting an exactly-represented constant value
>> > (such as 1.0) has the same net result as adding 0.0.
>>
>> No. Not in floating-point. If x is less than
>> std::numeric_limits::epsilon(), adding it to 1.0 will have no
>> effect, and the result of the subtraction will (should) be 0.0, not x.

You are forgetting rounding. epsilon() is defined as the difference
between 1 and the next representable number and most probably adding any
value between epsilon/2 and epsilon to 1 will result in the same value as
1+epsilon.

> If x is less than std::numeric_limits::epsilon(), then its value
> is already 0.0 - and the "if (x)" expression will evaluate to false. So
> adding and subtracting 1.0 to x will have no net effect on x's value, and
> the if expression will still yield "false".

Aren't you confusing between epsilon() and min()? Even then, in the presence
of denormal, there are numbers smaller than min().

Yours;

--
Jean-Marc

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Google