Solved

Double.NaN != Double.NaN

Posted on 2007-11-28
5
1,498 Views
Last Modified: 2008-09-16
The code below does not enter the 'then'...why?

double d = Double.NaN;
if (d == Double.NaN) {
  System.out.println ("Does not get here");
}
0
Comment
Question by:allelopath
  • 2
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20369844
should use:

if (Double.isNaN(d)) {
0
 
LVL 92

Expert Comment

by:objects
ID: 20369872
Equality comparison between two NaN numbers is always false.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20369900
When comparing two Double.NaN values, == operator always gives false.

You can use equals function which Double class inherited from Object class:

public boolean equals(Object obj)

    Compares this object against the specified object. The result is true if and only if the argument is not null and is a Double object that represents a double that has the identical bit pattern to the bit pattern of the double represented by this object. For this purpose, two double values are considered to be the same if and only if the method doubleToLongBits(double) returns the same long value when applied to each.

    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.

    Overrides:
        equals in class Object

    Parameters:
        obj - the object to compare with.
    Returns:
        true if the objects are the same; false otherwis
0
 
LVL 1

Author Comment

by:allelopath
ID: 20369918
UrosVidojevic:
Thanks for all the info. I guess I accepted objects answer while you were typing. Sorry.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20369976
It's OK. You are given the right answer, that is important. :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now