Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.4k points)

I have a double in Java and I want to check if it is NaN. What is the best way to do this?

1 Answer

0 votes
by (46k points)

Try the static Double.isNaN(double) way, or your Double's .isNaN() method.

// 1. static method

if (Double.isNaN(doubleValue)) {

...

}

// 2. object's method

if (doubleObject.isNaN()) {

...

}

By Easily trying:

if (var == Double.NaN) {

...

}

is not sufficient due to how the IEEE standard for NaN and floating-point numbers are defined.

Related questions

0 votes
1 answer
+13 votes
2 answers
asked May 26, 2019 in Java by tommas (1k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...