Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Comparing Double

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
A) True
True
B) True
False
C) False
True
D) False
False
Ans given C??
I thought it will give True for first if condn.
We are comparing Double object instance contents.So why is it giving false??
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JavaDoc:
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.
HTH!
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double a = new Double(Double.NaN)
Double b = new Double(Double.NaN)
A) True
True
B) True
False
C) False
True
D) False
False
Ans given C??
I thought it will give True for first if condn.
We are comparing Double object instance contents.So why is it giving false??

NaN can be the result of certain arithmetic operations for e.g. Dividing 0 by 0 or a square root of a negative number, etc. This result, NaN, indicates an indeterminate number. It is not an ordered number like other floating point values. So NaN is considered non-ordinal for Comparisons.
Explanation given in JLS.
Except for NaN, floating-point values are ordered; arranged from smallest to largest, they are negative infinity, negative finite nonzero values, negative zero, positive zero, positive finite nonzero values, and positive infinity.
NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN . The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN . In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN.
Consider the following e.g
public class AverageFruit
{
public static void main(String[] args)
{
double numOranges = 50.0e-1;
double numApples = 1.0e1;
double average1 = 0.0;
double average2 = 0.0;
double a;
double b;
average1 = (numOranges + numApples) / 0.0;
average2 = (numOranges + numApples) / 0.0;
System.out.println("A totally fruity program");
System.out.println("Average fruit is " + average1);
System.out.println("Average fruit is " + average2);
if(average1==average2)
System.out.println("Average1 and Average2 are equal");

a = (numOranges - 5.0)/ (numApples - 10.0);
b = (numOranges - 5.0)/ (numApples - 10.0);

System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
if(a==b)
System.out.println("a and b are equal");
else
System.out.println("a and b are not equal");

}
}

The value of average1 and average2 is Infinity. It means the result is greater than the largest number that can be represented as type double. In this case the comparison average1==average2 will result in true.
The second calculation with a and b is of type 0/0. This gives indeterminate result. Both will have value NaN. But the result of a==b will result in false.
To summarize:
For any value of x, including NaN itself, all of the following comparisons will return false.
x < Double.NaN<br /> x <= Double.NaN<br /> x == Double.NaN<br /> x > Double.NaN<br /> x >= Double.NaN
NOTE: Double.NaN != Double.NaN will return TRUE.
I hope the above explanation will help you.

 
Suma Narayan
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mani,
After I submitted my reply to your question, I realized that I have missed the main point (Why a==b is false and why not a.equals(b)???) and instead explained all general things which I am sure you already know them. Before I could give an appropriate answer, Brian had already answered. Thanks Brian.
His explanation is accurate for the question asked.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a float and if you want to check it is NaN (Not a Number), you have to use either Float.isNaN(Float f) or Double.isNaN(Double d) . This will give true if the arg is NaN. The == comparision will give always false if one of the operands is NaN.
regds
maha anna
 
Mani
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all..
 
reply
    Bookmark Topic Watch Topic
  • New Topic