Can we drop casts from float to bool?
Paul D. Anderson
paul.d.removethis.anderson at comcast.andthis.net
Wed Mar 24 12:28:08 PDT 2010
bearophile Wrote:
> Don:
> >Sometimes NaNs are true, sometimes not!<
>
> Urg.
>
>
> > That won't work in this case, unless you make nan a keyword. There are
> > 2^^62 different NaNs, so a bitwise comparison will not work.
>
> You are right, I did forget that here.
> So I have modified a little my request:
> http://d.puremagic.com/issues/show_bug.cgi?id=3981
>
> "x is double.init" can be used to detect uninitialized variables, because in this proposal "is" performs a bitwise comparison (this makes "is" not that useful with structs).
>
> The syntax "x == nan" can be used to perform the smart comparison with all possible nans. But this is an uncommon operation, so isnan(x) is good enough.
>
> Bye and thank you for your insights,
> bearophile
There are a finite number of NaNs for a fixed-length floating point number, but there are a (conceptually) infinite number for an arbitrary precision float. The sign, coefficient and exponent are undefined for a NaN, although the coefficient and sign fields can be used as a "payload" for the NaN, containing diagnostic information, etc.
And, according to the General Decimal Arithmetic Specification (http://speleotrove.com/decimal) comparing NaN with any number, including another NaN, will never return true. The operation raises an "Invalid Operation" exception.
Paul
More information about the Digitalmars-d
mailing list