www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - nan question

reply bobef <bobef abv_nospam.bg> writes:
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");
}
Sep 25 2007
next sibling parent Regan Heath <regan netmail.co.nz> writes:
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
Sep 25 2007
prev sibling next sibling parent reply BCS <ao pathlink.com> writes:
Reply to bobef,

 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");
 }

if you want to check for nan without a function call uses: fpVar !<>= 0
Sep 25 2007
parent reply bobef <bobef abv_nospam.bg> writes:
BCS Wrote:


 
 if you want to check for nan without a function call uses: fpVar !<>= 0
 
 

HAHAHAHAHAHAHAHA. I am not laughing at you, thanks for the help, but something is totally wrong. Just look at this. "!<>=" it looks almost like bytecode or 1337 or something :)
Sep 25 2007
parent Nathan Reed <nathaniel.reed gmail.com> writes:
bobef wrote:
 BCS Wrote:
 
 
 if you want to check for nan without a function call uses: fpVar !<>= 0

HAHAHAHAHAHAHAHA. I am not laughing at you, thanks for the help, but something is totally wrong. Just look at this. "!<>=" it looks almost like bytecode or 1337 or something :)

Read it as "fpVar is not less than, greater than, or equal to zero." The only 'number' that satisfies all these conditions is NaN. Thanks, Nathan Reed
Sep 25 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 9/25/07, bobef <bobef abv_nospam.bg> wrote:
 something is totally wrong. Just look at this. "!<>="

Well consider, the imaginary number i is not less than one. It is also not greater than one. It is also not equal to one. It makes perfect sense.
Sep 25 2007