The Double class has several static methods. These include isNaN, isFinite and isInfinite which tell us things about the double argument we provide.
public class Program {
static void test(double v) {
// Use methods to test the double.
if (Double.isNaN(v)) {
System.out.println(
"Not a number");
}
if (Double.isFinite(v)) {
System.out.println(
"Finite");
}
if (Double.isInfinite(v)) {
System.out.println(
"Infinite");
}
}
public static void main(String[] args) {
// An array of doubles.
double[] values = { Double.NaN,
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY,
100 };
// Test all doubles in array.
for (double v : values) {
System.out.println(
":: TEST ::");
System.out.println(v);
test(v);
}
}
}
:: TEST ::
NaN
Not a number
:: TEST ::
-Infinity
Infinite
:: TEST ::
Infinity
Infinite
:: TEST ::
100.0
Finite