Always false float comparisons

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon May 9 11:48:08 PDT 2016


On Monday, May 09, 2016 02:10:19 Walter Bright via Digitalmars-d 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?

It does seem like having implicit conversions with floating point numbers is
problematic in general, though warning about it or making it illegal could
very well be too annoying to be worth it. But at bare minimum, warning about
literals not matching the type that they're being compared against when
there _is_ a literal that would be of the same type is probably worth
warning about - and that could apply to more than just floating point
values. But figuring out when implicit conversions are genuinely useful and
should be allowed and when they're more trouble than they're worth is
surprisingly hard to get right. :(

- Jonathan M Davis



More information about the Digitalmars-d mailing list