• 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

Query on NaN Number

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ,
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think NaN is covered under SCJP. You wont find such questions in SCJP anyway
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic