Can we drop casts from float to bool?
Daniel Keep
daniel.keep.lists at gmail.com
Mon Mar 22 13:05:52 PDT 2010
Don wrote:
> This issue is inspired by bug 3918.
>
> float f;
> if (f) ...
>
> This currently compiles, but if the condition is true, what can you
> conclude about f?
> Obviously it's not zero, but can it be NaN?
> If it's just translated to: if (f!=0) ..., then f could be NaN.
> Likewise, if (!f) ... is NOT triggered if f is NaN.
>
> I find that rather unintuitive, and I can't easily invent a case where
> it is useful.
>
> As bug 3918 shows, this conversion has never worked properly. Most
> existing code that makes use of it is probably broken.
>
> `cast(bool)f` has the same problem. I don't think it should compile.
> And compared to `f != 0`, it's very unclear.
>
> This conversion seems to be confusing, bug prone, and not useful. Can we
> just get rid if it?
I dealt with this in my first ever blog post... largely because I named
the blog `while(nan)`.
That said, it is pretty strange behaviour.
More information about the Digitalmars-d
mailing list