I was always assuming that the following test will always succeed for finite values (no INF, no NAN) of somefloat
:
assert(somefloat*0.0==0.0);
In Multiply by 0 optimization it was stated that double a=0.0
and double a=-0.0
are not strictly speaking the same thing.
So I was wondering whether this can lead to problems on some platforms e.g. can the result of the above test depend on a
beeing positive or negative.
If your implementation uses IEEE 754 arithmetic (which most do), then positive and negative zero will compare equal. Since the left-hand side of your expression can only be either positive or negative zero for finite a
, the assertion will always be true.
If it uses some other kind of arithmetic, then only the implementor, and hopefully the implementation-specific documentation, can tell you. Arguably (see comments) the wording of the standard can be taken to imply that they must compare equal in any case, and certainly no sane implementation would do otherwise.
-0.0 == 0.0 according to double comparison rules.
For non-finite values (+-Inf, Nan) somefloat*0.0 != 0.0.
Your assert
can never fail, as long as somefloat
is not
infinity or NaN. On systems which don't support infinity or
NaN, the compiler can simply optimize it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With