• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Double.Nan problem

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test2 {
public static void main( String[] args ) {
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
if (Double.NaN == Double.NaN)
System.out.println("True");
else
System.out.println("false");
if (a.equals(b) )
System.out.println("True");
else
System.out.println("false");


}
}
I was expecting the result as
false
false
but correct result is
false
true
can some one explain it to me.
Thanks in advance
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---From Java Specs---
<pre>
public boolean equals(Object obj)
Compares this object against the specified
object. The result is true if and only if
the argument is not null and is a Double
object that represents a double that has the
identical bit pattern to the bit pattern of
the double represented by this object. For
this purpose, two double values are
considered to be the same if and only if the
method doubleToLongBits(double) returns the
same long value when applied to each.
Note that in most cases, for two instances
of class Double, d1 and d2, the value of
d1.equals(d2) is true if and only if
d1.doubleValue() == d2.doubleValue()

also has the value true. However, there are
two exceptions:
If d1 and d2 both represent Double.NaN, then
the equals method returns true, even though
Double.NaN==Double.NaN has the value false.
If d1 represents +0.0 while d2 represents -
0.0, or vice versa, the equal test has the
value false, even though +0.0==-0.0 has the
value true. This allows hashtables to
operate properly.
Overrides:
equals in class Object
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false
otherwise. </pre>
I missed it on the first look thru too... I
love being able to delete my messages
here

------------------
\\ //
~\// irucidal~
[This message has been edited by Khalid Bou-Rabee (edited July 13, 2000).]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pk:
public class Test2 {
public static void main( String[] args ) {
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
if (Double.NaN == Double.NaN)
System.out.println("True");
else
System.out.println("false");
if (a.equals(b) )
System.out.println("True");
else
System.out.println("false");


}
}
I was expecting the result as
false
false
but correct result is
false
true
can some one explain it to me.
Thanks in advance


the only reliable way I've found to deal with NaN is to use the methods isNaN the test would be
if(Double.isNaN(a) && Double.isNaN(b))
{
Sysytm.out.println("True");
}
...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic