Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# wrapper class

ameen ahamed
Greenhorn
Posts: 25
class doublenan
{
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");
}
};
false
true
why the first if statement gives false?
can anybody help me out

Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
In the first if statement, you're testing for equality between values of type double. Floating-point behavior in Java follows a specification known as IEEE 754. In short, IEEE 754 allows for quite a bit of imprecision, or "drift" in the value. This drift is tolerated to the degree that precision in *not* determined by the number being stored. That is, if you use a float to hold the number 5, the assumption is you can gain or lose a teeny bit of "fiveness" among the insignificant digits of the value, and you'd be (programmatically) ok.
It's computationally cheap to maintain float values this way; whoever uses this standard for numerics presumably accepts the drift as a trade-off. Java was not perceived as a useful language for intensive (and very precise) number-crunching at first, so perhaps it made sense to go this way and conserve performance.
Ok, that wasn't such a short answer. Anyway, the real short answer is, testing floating-point values for equality is far more likely to be false than true. Use relational operators instead. If you really need an equality test, you should use integrals.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited January 10, 2001).]

sankar sivassoupramanime
Greenhorn
Posts: 12
Ameen,
You can't compare NaN value direclty in java and if you do it ALWAYS evaluates to false. If you would like to compare using NaN then try this out,
class doublenan
{
public static void main(String args[]){
Double a=new Double(Double.NaN);
Double b=new Double(Double.NaN);
double d =a.floatValue();
if (Double.isNaN(d))
{
System.out.println("true");
}
else
System.out.println("false");
if (a.equals(b))
{
System.out.println("true");
}
else
System.out.println("false");
}
};
You will get as true,true
Hope are clear now!

sankar sivassoupramanime
Greenhorn
Posts: 12
Ameen,
You can't compare NaN value direclty in java and if you do it ALWAYS evaluates to false. If you would like to compare using NaN then try this out,
class doublenan
{
public static void main(String args[]){
Double a=new Double(Double.NaN);
Double b=new Double(Double.NaN);
double d =a.floatValue();
if (Double.isNaN(d))
{
System.out.println("true");
}
else
System.out.println("false");
if (a.equals(b))
{
System.out.println("true");
}
else
System.out.println("false");
}
}
You will get as true,true
Hope are clear now!