nan question

Regan Heath regan at netmail.co.nz
Tue Sep 25 08:17:22 PDT 2007


bobef wrote:
> Hello,
> 
> I have a little question.
> This program outputs gggg on dmd 1.21. Yes, I suck at math, but is this right (i.e. buggy) or nans must be compared another way?
> 
> 
> ===========================
> 
> import tango.io.Stdout;
> 
> void main()
> {
> 	double a=double.nan;
> 	if(a==double.nan) Stdout("1\n");
> 	else Stdout("g\n");
> 	if(a is double.nan) Stdout("2\n");
> 	else Stdout("g\n");
> 	if(double.nan==double.nan) Stdout("3\n");
> 	else Stdout("g\n");
> 	if(double.nan is double.nan) Stdout("4\n");
> 	else Stdout("g\n");
> }

See:
http://www.digitalmars.com/d/expression.html#floating_point_comparisons

In particular notice that the == row contains F in the unordered column.

This means (if I'm reading it correctly) that if either operand is nan 
== will always give false.

I think you want to have a look at isnan from std.math (not sure what 
Tango has), eg

//example phobos-ified
import std.math;

void main()
{
   double a = double.nan;
   if (isnan(a)) writefln("1");
   else writefln("g");
}

Regan



More information about the Digitalmars-d mailing list