• Post Reply Bookmark Topic Watch Topic
  • New Topic

Float.NaN question  RSS feed

 
Abhimanyu Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

public static void main(String[] args) throws Exception {
if (Float.NaN > Float.POSITIVE_INFINITY) {
System.out.println("Float.NaN is greater");
} else {
System.out.println("Float.POSITIVE_INFINITY is greater");
}
if (Float.POSITIVE_INFINITY > Float.NaN) {
System.out.println("Float.POSITIVE_INFINITY is greater");
} else {
System.out.println("Float.NaN is greater");
}
if(Float.POSITIVE_INFINITY == Float.NaN) {
System.out.println("both are equal");
} else {
System.out.println("both are NOT equal");
}
System.out.println(Float.NaN == Float.NaN);
}


The above code prints,
Float.POSITIVE_INFINITY is greater
Float.NaN is greater
both are NOT equal
false

Can someone pls explain this o/p to me ? Also, Float.NaN is a static field of Float wrapper class, then why is Float.NaN == Float.NaN returning false ?

TIA,
Abhimanyu

[ November 16, 2004: Message edited by: Abhimanyu Kumar ]
[ November 16, 2004: Message edited by: Abhimanyu Kumar ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any comparison involving Float.NaN returns false. To check for NaN, use a Float.isNaN() method.
[ November 16, 2004: Message edited by: Mike Gershman ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!