在我们惯性思维中,编写java代码时,数值类型做除法运算,除数不能是0,否则程序会抛出异常。那么,有没有列外呢?
下面用代码验证一下:
- 整数除以0测试:
public static void main(String[] args) {
System.out.println("1 / 0=" + 1 / 0);
}
运行结果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at learnbymaven.division.DivisionTest.main(DivisionTest.java:10)
- 单精度浮点数除以0测试:
public static void main(String[] args) {
System.out.println("1.0f / 0 = " + 1.0f / 0);
}
运行结果:
1.0f / 0 = Infinity
- 双精度浮点数除以0测试:
public static void main(String[] args) {
System.out.println("1.0d / 0 = " + 1.0d / 0);
}
运行结果:
1.0d / 0 = Infinity
可以看到,整数除以0时程序确实抛出了异常,可是浮点数除以0时,程序却没有报错,而是得出运算结果:Infinity。
Infinity是什么呢?
Infinity是无穷大的意思,为什么浮点数除以0会得到这个结果呢,从源码中,我们可以找到答案。
- java.lang.Float类:
2. java.lang.Double类:
可以看到Float类和Double类中,都定义了除数为0时的对应的浮点数值情况,基于此,验证一下以下代码:
public static void main(String[] args) {
// Float
System.out.println("(Float.NEGATIVE_INFINITY == Float.POSITIVE_INFINITY):"
+ (Float.NEGATIVE_INFINITY == Float.POSITIVE_INFINITY));
System.out.println("(1.0f / 0.0f) = " + (1.0f / 0.0f));
System.out.println("(1.0f / 0.0f == 10.0f / 0.0f):" + (1.0f / 0.0f == 10.0f / 0.0f));
System.out.println("(-1.0f / 0.0f) = " + (-1.0f / 0.0f));
System.out.println("(-1.0f / 0.0f == -10.0f / 0.0f):" + (-1.0f / 0.0f == -10.0f / 0.0f));
System.out.println("(0.0f / 0.0f) = " + (0.0f / 0.0f));
// NaN与任何值比较都是false
System.out.println("(Float.NaN==Float.NaN):" + (Float.NaN == Float.NaN));
System.out.println("(0.0f / 0.0f == Float.NaN) = " + (0.0f / 0.0f == Float.NaN));
System.out.println("(-0.0f / 0.0f) = " + (-0.0f / 0.0f));
System.out.println("(-0.0f / 0.0f == Float.NaN) = " + (-0.0f / 0.0f == Float.NaN));
System.out.println("(0.0 / 0.0 == -0.0 / 0.0) = " + (0.0 / 0.0 == -0.0 / 0.0));
System.out.println("===================================================================");
// Double
System.out.println("(Double.NEGATIVE_INFINITY == Double.POSITIVE_INFINITY):"
+ (Double.NEGATIVE_INFINITY == Double.POSITIVE_INFINITY));
System.out.println("(1.0 / 0.0) = " + (1.0 / 0.0));
System.out.println("(1.0 / 0.0 == 10.0 / 0.0):" + (1.0 / 0.0 == 10.0 / 0.0));
System.out.println("(-1.0 / 0.0) = " + (-1.0 / 0.0));
System.out.println("(-1.0 / 0.0 == -10.0 / 0.0):" + (-1.0 / 0.0 == -10.0 / 0.0));
System.out.println("(0.0 / 0.0) = " + (0.0 / 0.0));
System.out.println("(-0.0 / 0.0) = " + (-0.0 / 0.0));
System.out.println("(0.0 / 0.0 == -0.0 / 0.0) = " + (0.0 / 0.0 == -0.0 / 0.0));
// NaN与任何值比较都是false
System.out.println("(Double.NaN==Double.NaN):" + (Double.NaN == Double.NaN));
System.out.println("===================================================================");
// Float Double
System.out.println("(Float.NEGATIVE_INFINITY == Double.NEGATIVE_INFINITY):"
+ (Float.NEGATIVE_INFINITY == Double.NEGATIVE_INFINITY));
System.out.println("(Float.POSITIVE_INFINITY == Double.POSITIVE_INFINITY):"
+ (Float.POSITIVE_INFINITY == Double.POSITIVE_INFINITY));
System.out.println("(Float.NaN == Double.NaN):" + (Float.NaN == Double.NaN));
}
运行结果:
(Float.NEGATIVE_INFINITY == Float.POSITIVE_INFINITY):false
(1.0f / 0.0f) = Infinity
(1.0f / 0.0f == 10.0f / 0.0f):true
(-1.0f / 0.0f) = -Infinity
(-1.0f / 0.0f == -10.0f / 0.0f):true
(0.0f / 0.0f) = NaN
(Float.NaN==Float.NaN):false
(0.0f / 0.0f == Float.NaN) = false
(-0.0f / 0.0f) = NaN
(-0.0f / 0.0f == Float.NaN) = false
(0.0 / 0.0 == -0.0 / 0.0) = false
===================================================================
(Double.NEGATIVE_INFINITY == Double.POSITIVE_INFINITY):false
(1.0 / 0.0) = Infinity
(1.0 / 0.0 == 10.0 / 0.0):true
(-1.0 / 0.0) = -Infinity
(-1.0 / 0.0 == -10.0 / 0.0):true
(0.0 / 0.0) = NaN
(-0.0 / 0.0) = NaN
(0.0 / 0.0 == -0.0 / 0.0) = false
(Double.NaN==Double.NaN):false
===================================================================
(Float.NEGATIVE_INFINITY == Double.NEGATIVE_INFINITY):true
(Float.POSITIVE_INFINITY == Double.POSITIVE_INFINITY):true
(Float.NaN == Double.NaN):false
上面的代码运行结果没有过多解释的地方,只是注意NaN与任何值比较都是false,包括自身比较都是false。
java中,为什么浮点数除以0没有报错呢?因为java的浮点数运算,是遵循IEEE 754规范的。关于IEEE 754规范,想详细了解,可以看一下关于IEEE 754的论文原文,如果只想粗略了解一下可以看一下下面的相关资料:
文档中有些内容截图如下:
- 关于NaN:
2. 关于Infinity,-Infinity(正负无穷):
基于上述描述,多测试一次NaN参与运算结果:
public static void main(String[] args) {
System.out.println("(Double.NaN):" + Double.NaN);
System.out.println("(Double.NaN + 1.0):" + (Double.NaN + 1.0));
System.out.println("(Double.NaN - 1.0):" + (Double.NaN - 1.0));
System.out.println("(Double.NaN * 1.0):" + (Double.NaN * 1.0));
System.out.println("(Double.NaN / 1.0):" + (Double.NaN / 1.0));
System.out.println("(Double.NaN / 0.0):" + (Double.NaN / 0.0));
System.out.println("(0.0 / Double.NaN):" + (0.0 / Double.NaN));
}
运行结果:
(Double.NaN):NaN
(Double.NaN + 1.0):NaN
(Double.NaN - 1.0):NaN
(Double.NaN * 1.0):NaN
(Double.NaN / 1.0):NaN
(Double.NaN / 0.0):NaN
(0.0 / Double.NaN):NaN
由此可见,NaN参与的任何运算结果都是NaN。