Always false float comparisons

Ethan Watson via Digitalmars-d digitalmars-d at puremagic.com
Mon May 9 05:21:04 PDT 2016


On Monday, 9 May 2016 at 09:10:19 UTC, Walter Bright wrote:
> Don Clugston pointed out in his DConf 2016 talk that:
>
>     float f = 1.30;
>     assert(f == 1.30);
>
> will always be false since 1.30 is not representable as a 
> float. However,
>
>     float f = 1.30;
>     assert(f == cast(float)1.30);
>
> will be true.
>
> So, should the compiler emit a warning for the former case?

I'd assume in the first case that the float is being promoted to 
double for the comparison. Is there already a warning for loss of 
precision? We treat warnings as errors in our C++ code, so C4244 
triggers all the time in MSVC with integer operations. I just 
tested that float initialisation in MSVC, initialising a float 
with a double triggers C4305.

So my preference is "Yes please".

https://msdn.microsoft.com/en-us/library/th7a07tz.aspx
https://msdn.microsoft.com/en-us/library/0as1ke3f.aspx


More information about the Digitalmars-d mailing list