• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on NaN Number

 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JLS it is mentioned that :
" x!=x is true if and only if x is NaN,"
and "The equality operator == returns false if either operand is NaN"
But while compiling the below code,it returns just opposite

class val{
int i=7;

public static void main (String args[]){


Float f = 0.0f/0.0f;
System.out.println(f);
System.out.println(f == f);//output true
System.out.println(f != f);//output false
}
}

Please explain ,
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think NaN is covered under SCJP. You wont find such questions in SCJP anyway
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ramya ramya:
In JLS it is mentioned that :
" x!=x is true if and only if x is NaN,"
and "The equality operator == returns false if either operand is NaN"
But while compiling the below code,it returns just opposite

class val{
int i=7;

public static void main (String args[]){


Float f = 0.0f/0.0f;
System.out.println(f);
System.out.println(f == f);//output true
System.out.println(f != f);//output false
}
}

Please explain ,


Note that you're boxing the output of 0.0f/0.0f into a Float instead of storing it in a float.

If you change the type of f to float or compare f against Float.NaN, you will see the output that you are expecting to see from the conditional operators.
[ September 01, 2006: Message edited by: Keith Lynn ]
 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for reply ,You are right ,If i replaced it to float f ,It works fine but why it is not working for Float f ,means making an object of Float.
Is there any object related concept in this .

Is it not covered in SCJP as it is floating point types,Please suggest.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

x!=x is true if and only if x is NaN

Float.NaN != Float.NaN



The equality operator == returns false if either operand is NaN

f == Float.NaN (f is Float object with value NaN)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic