I have a double in Java and I want to check if it is NaN
.
What is the best way to do this?
7 Answers
Use the static Double.isNaN(double)
method, or your Double
's .isNaN()
method.
// 1. static method
if (Double.isNaN(doubleValue)) {
...
}
// 2. object's method
if (doubleObject.isNaN()) {
...
}
Simply doing:
if (var == Double.NaN) {
...
}
is not sufficient due to how the IEEE standard for NaN and floating point numbers is defined.
-
54Another way to do this would be v != v. Only NaN compares false with itself. Don't do that though, isNaN is a million times better. :) – Joren Sep 21 '09 at 20:15
-
5@Joren, better late than never: 'isNaN' is indeed better to use than v != v for readability. But the source code of the isNaN method is exactly the same as saying v != v. Source: static public boolean isNaN(double v) { return (v != v); } – Rolf ツ Dec 10 '14 at 19:50
-
1```Double.isNaN should be (true) ``` nice answer – Oliver Shaw Apr 24 '15 at 18:41
-
1@Joren isNaN just check v != v ;) but it looks better – Mr Jedi May 31 '15 at 12:50
-
Using Java 5: `value == Double.NaN` does not work, but `Double.isNaN(value)` works just fine. – secondbreakfast Dec 06 '16 at 18:08
-
@Rolf No. You would expect it, but it does not. " d == Double.NaN" will never be true, even if d is NaN. You can NOT use it as a method for checking for NaN, you *have* to use isNaN – Apr 26 '17 at 13:37
-
@Timo not sure what you want to say here. The source of `isNan` is equal to the statement `v != v` so what has `d == Double.NaN` to do with that? – Rolf ツ Apr 26 '17 at 13:41
-
@Rolf to be specific, I'm saying you're incorrect. Tell me, what is the result of v != v ? - Note that v is the same variable – Apr 26 '17 at 13:48
-
@Rolf No. You cannot use " d == Double.NaN" - it will never be true. Prove it to yourself with code, run this "System.out.println( "Value=" + (0.0/0.0) + " Comparison to Nan=" + ((0.0/0.0) == Double.NaN ) + " Comparison to Nan=" + Double.isNaN((0.0/0.0)));" Result --> "Value=NaN Comparison to Nan=false Comparison to Nan=true". – Apr 26 '17 at 13:48
-
@Rolf NaN is a valid VALUE of double (base type). IEEE insist (I imagine) that *any* comparison to the VALUE NaN must always be false, including to *itself*. What the NaN check does is compares the double to itself, and if it happens to return false then it must be a NaN because any other value would return true. compare(3,3)->true, compare(1,1)->true ... etc ... compare( Nan,Nan) --> false. It must be the only value that does that So the OP *could* look for NaN by saying "if( myValue != myValue )" ... but it looks very weird and isNan is better, but == NaN will never work. – Apr 26 '17 at 13:51
-
@Timo, First of all I never said `d == Double.NaN` will work. Secondly `v != v` works because: A. The Java Standard manual says so and B. The method `isNaN` uses this. – Rolf ツ Apr 26 '17 at 13:51
-
@Timo so I'm still uncertain what you're trying to correct here. We're basically saying the same, except for the `d == Double.NaN` part which I've never seen or said before. – Rolf ツ Apr 26 '17 at 13:54
Try Double.isNaN()
:
Returns true if this Double value is a Not-a-Number (NaN), false otherwise.
Note that [double.isNaN()
] will not work, because unboxed doubles do not have methods associated with them.

- 1
- 1

- 344,730
- 71
- 640
- 635
-
I thought you couldn't call methods on primitive types in Java. It really needs to be `Double.isNan()` and not `double.IsNan()`, right? – Joren Sep 21 '09 at 20:41
-
Joren, he's relying on autoboxing (double getting converted to Double by the compiler/runtime); new feature from 1.5 onwards. Little risk going this direction; going from Double to double creates risk of NullPointerExceptions. – M1EK Sep 21 '09 at 21:01
-
I thought autoboxing only worked on using the double as an argument, adding it to a collection, and the like. Try declaring double x and then asking x to isNaN() - gives me a compiler error. – Carl Sep 22 '09 at 02:23
-
Really, I suspect Andrew just missed the shift key typing the first "double". – Carl Sep 22 '09 at 02:25
You might want to consider also checking if a value is finite via Double.isFinite(value)
. Since Java 8 there is a new method in Double
class where you can check at once if a value is not NaN and infinity.
/**
* Returns {@code true} if the argument is a finite floating-point
* value; returns {@code false} otherwise (for NaN and infinity
* arguments).
*
* @param d the {@code double} value to be tested
* @return {@code true} if the argument is a finite
* floating-point value, {@code false} otherwise.
* @since 1.8
*/
public static boolean isFinite(double d)

- 2,253
- 19
- 26
You can check for NaN by using var != var
. NaN
does not equal NaN
.
EDIT: This is probably by far the worst method. It's confusing, terrible for readability, and overall bad practice.

- 5,272
- 2
- 29
- 50
-
3Can someone explain the downvote? I know, this way is very bad, and `isNan` is better for readability, but it works, right? And the `isNan` method uses this to check for `NaN`. – hyper-neutrino Oct 12 '15 at 19:20
-
1I'm guessing the downvote was because this way is very bad, and isNaN is better for readability. – Edward Falk Jan 22 '16 at 17:06
-
1I didn't downvote you, but I think additional comment would be useful here: if you compare wrappers like Float or Double you end up comparing references this way, not their values, which is definitely is not what you want. – Battle_Slug Mar 03 '16 at 03:30
-
3@Battle_Slug Thanks for the comment. I do know that this is a very bad idea, but I put it here for completeness. – hyper-neutrino Mar 04 '16 at 02:28
-
```isNaN``` does this under the hood, but how does it work? How does something not equal itself ?? – wilmol Jul 08 '19 at 07:23
-
-
method? You mean operator? ```!=``` operator is overloaded for ```Double``` class? – wilmol Jul 08 '19 at 10:21
If your value under test is a Double (not a primitive) and might be null
(which is obviously not a number too), then you should use the following term:
(value==null || Double.isNaN(value))
Since isNaN()
wants a primitive (rather than boxing any primitive double to a Double), passing a null
value (which can't be unboxed to a Double) will result in an exception instead of the expected false
.

- 1,011
- 9
- 25

- 11
- 1
The below code snippet will help evaluate primitive type holding NaN.
double dbl = Double.NaN;
Double.valueOf(dbl).isNaN() ? true : false;

- 153
- 1
- 3
- 9
-
1There is never a need to write `condition? true: false`, as `condition` is already `true` when `true` and `false` when `false`. Even shorter is `Double.isNaN(dbl)` which skips the unnecessary conversion from a `double` value to a `Double` object. – Holger Feb 10 '23 at 13:38
Beginners needs practical examples. so try the following code.
public class Not_a_Number {
public static void main(String[] args) {
String message = "0.0/0.0 is NaN.\nsimilarly Math.sqrt(-1) is NaN.";
String dottedLine = "------------------------------------------------";
Double numerator = -2.0;
Double denominator = -2.0;
while (denominator <= 1) {
Double x = numerator/denominator;
Double y = new Double (x);
boolean z = y.isNaN();
System.out.println("y = " + y);
System.out.println("z = " + z);
if (z == true){
System.out.println(message);
}
else {
System.out.println("Hi, everyone");
}
numerator = numerator + 1;
denominator = denominator +1;
System.out.println(dottedLine);
} // end of while
} // end of main
} // end of class

- 73,784
- 33
- 194
- 347

- 31
- 2
-
5This example does too much, and it's not clear what you were trying to show. This is just a bunch of fragmented code. – Jared Hooper Sep 07 '15 at 14:56
-
6As the OP, who was a beginner when this question was asked back in '09, I can assure you that the accepted answer was far more helpful than this "practical" example would have been. – Eric Wilson Sep 28 '15 at 18:26
-
1Worse than providing a piece of code full of unnecessary things, this is a piece of code full of *discouraged* things. – Holger Feb 10 '23 at 13:42