Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Double Trouble!!!

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
Please help me figure out why the foll.codes print the given results?
1.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");
Prints "False"
"True"
2.The following code will give
1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");
Prints "False".
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first one, try to remember this:
if you compare to NaN values with == it will return false, if you compare them with equals it will return true.
if you compare -0.0 to 0.0 with == it will return true, if you compare them with equals it will return false.
The way I have remembered this, is 0.0 is a number and if you were to type in the numbers with == then it would work and return true. NaN is not a number, so I treat it like an object, so == will return false. Then I just remember that the opposite is true for the other scenerios involving equals() method. Tricky, but one of those things you just have to memorize.
Bill
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
regd the first prob, i agree with bill. i have also roted this mantra!
regd the second, i think that b1.toString() == b1.toString() is making to objects( remember the return type of toString() is String) and since the objects on both sides of == are not the same, the obvios answers. haven't tried but i think that true would be returne with equals method coz that compares the contents....
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Curious,
I just read in Bill Brogden's Exam Prep (p.53) that the only way to detect NaN is with the Float.isNaN or Double.isNaN method.
I also read in The JavaRanch SCJP FAQ that:
>x!=x is true if and only if x is NaN
(Don't kill me Bill, but) couldn't one conclude that actually you could, however silly it would be, detect NaN by doing all of the following:
if (Float.isNaN(x))
if (x!=x)
and even if ((""+x).equals("NaN"))
- Dave
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bill bozeman:
For the first one, try to remember this:
if you compare to NaN values with == it will return false, if you compare them with equals it will return true.
if you compare -0.0 to 0.0 with == it will return true, if you compare them with equals it will return false.
The way I have remembered this, is 0.0 is a number and if you were to type in the numbers with == then it would work and return true. NaN is not a number, so I treat it like an object, so == will return false. Then I just remember that the opposite is true for the other scenerios involving equals() method. Tricky, but one of those things you just have to memorize.
Bill


The reply to question seems to be a good way to remember the answer.But to understand it why it really happens is to just understand what NAN is actually.It is pure mathematics(with a little difference).NAN in JAVA is introduced to take care of things like 0.0/0.0,infinity*0.0.Now suppose that NAN==NAN
then
1.0/0.0*0.0==2.0/0.0*0.0
i:e POSITIVE_INFINITY*0.0(NAN)==POSITIVE_INFINITY*0.0(NAN)
BUT that means 1.0=2.0
which is ridiculos.
Now where the difference lies
1.0/0.0*0.0==1.0/0.0*0.0
where NAN doesnt work.JAVA takes result as not equal.
There is some logic behind everything and where logic can not be applied it is then yr wish
I hope that helps
sandeep


[This message has been edited by sandeep bagati (edited April 21, 2001).]
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my explanation for why the following condition is true.

Originally posted by Priya Kannan:


1.The following code will print
1: Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);

9: if( a.equals(b) )
10: System.out.println("True");


As you know Double.NaN == Double.Nan will allways return false. Because of this you might assume that the equals method should return false also. This is not true because the equals method is overriden and it doesn't use the == operator on the two values(Double.Nan == Double.NaN). It uses a native method which takes a double and returns a long value that represents that double. And then it does an == comparison with the long value that's returned. In the case of the double value "Double.NaN", the method would return the same value. Therefore a.equals(b) will return true.

------------------
Ronnie Phelps
Sun Certified Programmer for the Java� 2 Platform
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic