• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pls help on NaN

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's discussed fully here http://www.javaranch.com/ubb/Forum24/HTML/002376.html
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic