double.init is real.nan, is it a bug?

JS js.mdnq at gmail.com
Wed Aug 14 22:10:15 PDT 2013


On Thursday, 15 August 2013 at 04:01:28 UTC, bsd wrote:
> Hi all,
>
>
> I think this is a bug, but if not I'm very confused and would 
> like some clarification.
>
> When checking double values that could be nan or unassigned I 
> have to check both double.init and double.nan. But double.init 
> IS double.nan.
>
> ---
> void main() {
>     // passes but I believe it is checked during compile
>     assert(double.init is double.nan);
>
>     double val;
>     std.stdio.writeln( to!string(val) ); // writes "nan" to 
> console
>     assert(val is double.init); // passes
>     assert(val is double.nan); // << FAILS (unexpectedly for me 
> anyway)
> }
> ---
>
> Why does assert(val is double.nan) fail? Given:
>
> a) double.init is double.nan
> b) val is double.init
> c) to!string(val) sees val as a nan
>
> I also found this confusing:
> ---
> void main() {
>
>     assert(double.init is double.nan); // passes expected
>
>     assert(double.init is float.init); // passes, unexpected
>     assert(double.init is real.init); // passes, unexpected
>
>     assert(double.init is real.nan); // passes, unexpected
>     assert(double.init is float.nan); // passes, unexpected
>
> }
> ---
> I don't think these should be passing...should they??
>
> I'm on fedora 19, dmd 2.063.2 extracted from the ZIP
>
> Thanks

There is the issue with order on is. Try reversing them and see 
if you get the same results. I imagine for floats and doubles are 
interchangeable for nans(both are nan, regardless of the encoding 
used).


More information about the Digitalmars-d-learn mailing list