File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wrapper class

 
ameen ahamed
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic