• 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

Question for Dan Wrapper Class

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static boolean m(double v) {
return(v != v == Double.isNaN(v));
// when d1 = Double.Nan
// When the method invokes
// (Nan != Nan == Nan)
// How is this true?
// when d2 = Double.POSITIVE_INFINITY
// (Infinity != Infinity == Infinity)
// How is this true? am I missing any evaluation
// order? Is there any chart for false == false = true
// something like that?

}
public static void main (String args[]) {
double d1 = Double.NaN;
System.out.println(d1); // Printing Nan
double d2 = Double.POSITIVE_INFINITY;
System.out.println(d2); // Printing Infinity
double d3 = Double.MAX_VALUE;
System.out.println(d3); // Printing 1.767E3
System.out.print(m(d1) + "," + m(d2) + "," + m(d3));

}
Output is true,true,true. How ?
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
subject of this thread is wrong.
Question from Dan Wrapper class, so anyone can answer not only Dan.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, since anyone can answer, I guess I'll give it a shot.
Adding some parenthesis might help clear up the confusion a bit. Look at that line like this:

Basically, what will happen is that the value that is passed in will be compared to itself. The return value of that will then be compared to the return value of Double.isNaN.
Let's look at the examples given here:
NaN
NaN != NaN: true
Double.isNaN(NaN): true
true == true, so the method returns true
Infinity
Infinity != Infinity: false
Double.isNaN(Infinity): false
false == false, so the method returns true
Double.MAX_VALUE
1.7976931348623157E308 != 1.7976931348623157E308: false
Double.isNaN(1.7976931348623157E308): false
false == false, so the method returns true
You can rewrite the method m like this to get the information that I have shown here:

I hope that helps,
Corey
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by suresh kamsa:
static boolean m(double v) {
return(v != v == Double.isNaN(v));
}
public static void main (String args[]) {
double d1 = Double.NaN;
System.out.println(d1); // Printing Nan
double d2 = Double.POSITIVE_INFINITY;
System.out.println(d2); // Printing Infinity
double d3 = Double.MAX_VALUE;
System.out.println(d3); // Printing 1.767E3
System.out.print(m(d1) + "," + m(d2) + "," + m(d3));

}
Output is true,true,true. How ?


The following is the declaration of the method java.lang.Double.isNaN.

As a result of the above declaration, the following method from my mock exam will always return a value of true.
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How come Nan != Nan. Is it because you compare anything with Nan is equal to Nan?
Thanks to both of you.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API for Double (in the description of the method equals):


If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.


The point is that Double.NaN doesn't equal anything (using the == operator, that is), even itself.
Personally, I'm not so sure if I agree that infinity should be equal (using ==) to infinity, but that's not really my call. :roll: Just remember, computer mathemtaics isn't always exactly the same as theoretical mathematics.
Hope that helps,
Corey
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic