Infinity、-Infinity和NaN
首先看看这三个代表什么:
- Infinity:正无穷大
- -Infinity:负无穷大
- NaN:Not a Number
当float或double类型的数除零时,
- 当被除数为非零值时,结果为无穷大
- 当被除数也为零值时,则结果为
NaN
.
public class NaN {
public static void main(String[] args) {
System.out.println(1.0/0);
System.out.println(-1.0/0.0);
System.out.println(0.0/0.0);
System.out.println("************************");
double a = 1.0/0.0;//Infinity
double b = 1.0/0.0;//Infinity
double c = -0.0/0.0;//-Infinity
double d = 0.0/0.0;//NaN
double e = 0.0/0.0;//NaN
System.out.println(a+10.0);//Infinity
System.out.println(a == b);//true
System.out.println(a == c);//false
System.out.println(a == d);//false
System.out.println(d == e);//false
}
}
输出
Infinity
-Infinity
NaN
************************
Infinity
true
false
false
false
注意:浮点数做除法时除数可以为零值,但整数类型会抛java.lang.ArithmeticException: / by zero
异常。