用于检查浮点数是否为无限数的 Java 程序(NAN)
要检查Float是否为isInfinite,请使用该isInfinite()方法并检查NAN,请使用该isNaN()方法。
示例
public class Demo { public static void main(String[] args) { float value1 = (float) 1 / 0; boolean res1 = Float.isInfinite(value1); System.out.println("Checking for isInfinite? = "+res1); float value2 = (float) Math.sqrt(9); boolean res2 = Float.isNaN(value2); System.out.println("Checking for isNan? = "+res2); } }输出结果
Checking for isInfinite? = true Checking for isNan? = false