W
William Hughes
I've noted that compilers (usually in some strict mode) even emit
warnings for comparing with 0.0. I have enough faith in implementors
that that seems too much of a stretch. Not to mention that there is no
other way to check for division-by-zero.
Well, there is no easy way to tell if a comparison with zero
is to avoid an exception (e.g. divide by 0) or
an implicit equality comparison (f1-f2)==0.
Still, a warning that warns agains a needed construct
with no good altenate form is (to say the least)
questionable.
I also think it will be
generally safe to compare with 1.0 (based on typical floating point
implementations).
It is always safe. It may even make sense at times. Although
not guaranteed, you can expect integers to be exaclty represented
and exactly converted. So
if( (float)1 == 1.0 )
will probably do what you expect. It is still however stupid.
The more likely case is
a: if ( f1 == 1.0 )
This is (probably) equivalent to
b: if( (floor(f1) == 1.0) && (ceil(f1) == 1.0))
If you know enough about f1 to know that b will work then
you can use a. If you do not know that b will work then you do not
know that
a will work.
- William Hughes