• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pls help on NaN

 
teja kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MA {
public static void main(String ags[]){
if ((0.0/0.0)==Double.NaN)
System.out.print (0.0/0.0);
System.out.print ("not equal");
}
}
can anyone tell me why output is not equal rather than NaN?
ready
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ready:
public class MA {
public static void main(String ags[]){
if ((0.0/0.0)==Double.NaN)
System.out.print (0.0/0.0);
System.out.print ("not equal");
}
}
can anyone tell me why output is not equal rather than NaN?
ready

I remember reading this in RHE. Even (Double.NaN == Double.NaN) returns false. It is just the nature of NaN - it is not a finite quality that can be used for comparison.
Savithri
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's discussed fully here http://www.javaranch.com/ubb/Forum24/HTML/002376.html
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

double d1=Double.NaN;
double d2=Double.NaN;
Double D1=new Double(Double.NaN);
Double D2=new Double(Double.NaN);
// always false
if (Double.NaN==Double.NaN) System.out.println("Never Equal");
// true
if (D1.equals(D2)) System.out.println("D1.equals(D2)");
//true
if(D1.isNaN()) System.out.println("D1 is NaN");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic