Author Topic:   Comparing Double
Mani
ranch hand
posted April 27, 2000 01:32 AM             
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??

Brian, Nice
greenhorn
posted April 27, 2000 05:47 AM             
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!

Suma Narayan
ranch hand
posted April 27, 2000 06:21 AM         
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) 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
x <= Double.NaN
x == Double.NaN
x > Double.NaN
x >= Double.NaN

NOTE: Double.NaN != Double.NaN will return TRUE.

I hope the above explanation will help you.


Suma Narayan
ranch hand
posted April 27, 2000 06:38 AM         
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.

maha anna
bartender
posted April 27, 2000 08:06 AM             
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
posted April 27, 2000 08:43 AM             
Thanks to all..

|