• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Double.Nan question.plz help

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will print

1: Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);
3:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
8:
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");

A) True
True

B) True
False

C) False
True

D) False
False
c is the answer, plz anyone answer.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am getting prepared to write SCJP5 exam. Should I worry about Nan use for the exam or it's only for SCJP 1.4 ?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well Double.NAN is not actually defined any where as a precise value, so any test about it will never be true
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interestingly,
Double.NaN != Double.NaN
gives true!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I had in my own notes regarding NaN...

NaN is non-ordinal, so relational comparisons involving NaN always result in false. The only exception is NaN != NaN, which returns true. Wrapper methods Float.isNaN(float) and Double.isNaN(double) can be used to test for NaN. Alternatively, a float or double is NaN if it is not equal to itself (e.g., x != x).

Furthermore...
  • Math.round(Float.NaN) results in an int zero, and Math.round(Double.NaN) results in a long zero.
  • String literals "NaN" and "Infinity" are acceptable for Float and Double constructors.
  • float or double division by zero results in +/- Infinity; except dividing zero by zero, which results in NaN.
  •  
    Tilo Hemp
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Marc,

    thanks a lot for the interesting information!

    I've got one remark and one question:

    NaN is non-ordinal, so relational comparisons involving NaN always result in false. The only exception is NaN != NaN, which returns true.



    --> the exception is not limited to NaN != NaN, but can also applied to say Double.NaN != 12.34, which also gives true. I guess != simply inverts the output of ==.

    And my question is: what does "Cuius rei demonstrationem mirabilem sane detexi hanc marginis exiguitas non caperet." mean? I tried to find an automated latin-translator, but this did not work out

    Regards
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Tilo Hemp:
    ...--> the exception is not limited to NaN != NaN, but can also applied to say Double.NaN != 12.34, which also gives true. I guess != simply inverts the output of ==...


    You're correct! I guess it would be more accurate to say something like, "the exception is in applying the != comparison to NaN, which returns true -- even when comparing two NaNs."

    As for the quote, see Fermat's Last Theorem (and note the enigmatic quality that made Fermat's words so intriguing in the first place ).
     
    author
    Posts: 9050
    21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And to answer an earlier question, this topic is on the 1.4 exam, but NOT the 1.5 exam.
     
    Tilo Hemp
    Ranch Hand
    Posts: 91
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks a lot marc, this is quite funny!
    especially, putting this quotation in a small margin under short and to-the-point statements
     
    bnkiran kumar
    Ranch Hand
    Posts: 176
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    marc weber ,thank you for your valuable material.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic