I have a double in Java and I want to check if it is NaN
.
What is the best way to do this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can check for NaN by using
var != var
.NaN
does not equalNaN
.EDIT: This is probably by far the worst method. It's confusing, terrible for readability, and overall bad practice.
Try
Double.isNaN()
:Note that [
double.isNaN()
] will not work, because unboxed doubles do not have methods associated with them.Beginners needs practical examples. so try the following code.
Use the static
Double.isNaN(double)
method, or yourDouble
's.isNaN()
method.Simply doing:
is not sufficient due to how the IEEE standard for NaN and floating point numbers is defined.
You might want to consider also checking if a value is finite via
Double.isFinite(value)
. Since Java 8 there is a new method inDouble
class where you can check at once if a value is not NaN and infinity.