Solved

Double.NaN != Double.NaN

Posted on 2007-11-28
5
1,533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 70
Desingning Refactoring existing code 2 31
restrict decimal places for double datatype 10 31
IBM TS2900 (3572) Tape Autoloader Java? 12 59
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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