在许多编程语言中,你可以使用特定的函数来判断一个值是否为 "NaN"(Not a Number)。
在JavaScript中,你可以使用 isNaN()
函数:
javascriptlet value = "NaN";
if (isNaN(value)) {
console.log("The value is NaN.");
} else {
console.log("The value is not NaN.");
}
在Python中,你可以使用 math.isnan()
函数:
pythonimport math
value = "NaN"
if math.isnan(value):
print("The value is NaN.")
else:
print("The value is not NaN.")
在Python中,还可以使用 numpy.isnan()
函数:
pythonimport numpy as np
value = "NaN"
if np.isnan(value):
print("The value is NaN.")
else:
print("The value is not NaN.")
在Java中,你可以使用 Double.isNaN()
方法:
javapublic class Main {
public static void main(String[] args) {
double value = Double.parseDouble("NaN");
if (Double.isNaN(value)) {
System.out.println("The value is NaN.");
} else {
System.out.println("The value is not NaN.");
}
}
}