aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wrapper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wrapper class" Watch "wrapper class" New topic
Author

wrapper class

ameen ahamed
Greenhorn

Joined: Jan 02, 2001
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");
}
};
the answer is
false
true
why the first if statement gives false?
can anybody help me out
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
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).]


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
sankar sivassoupramanime
Greenhorn

Joined: Jan 03, 2001
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

Joined: Jan 03, 2001
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!
 
 
subject: wrapper class