为什么用浮点(或双精度)数除以零不会抛出 java.lang.ArithmeticException:/在 Java 中被零除 [英] Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java

查看:27
本文介绍了为什么用浮点(或双精度)数除以零不会抛出 java.lang.ArithmeticException:/在 Java 中被零除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下语句明显抛出 java.lang.ArithmeticException:/by zero.

System.out.println(0/0);

因为文字 0 被认为是 int 文字,整数运算中不允许除以零.

because the literal 0 is considered to be an int literal and divide by zero is not allowed in integer arithmetic.

但是,以下情况不会抛出任何异常,例如java.lang.ArithmeticException:/by zero.

The following case however doesn't throw any exception like java.lang.ArithmeticException: / by zero.

int a = 0;
double b = 6.199;
System.out.println((b/a));

显示Infinity.

以下语句毫无例外地产生 NaN(非数字).

The following statement produces NaN (Not a Number) with no exception.

System.out.println(0D/0); //or 0.0/0, or 0.0/0.0 or 0/0.0 - floating point arithmetic.

在这种情况下,两个操作数都被认为是双精度的.

In this case, both of the operands are considered to be double.

同样,以下语句不会抛出任何异常.

Similarly, the following statements don't throw any exception.

double div1 = 0D/0; //or 0D/0D
double div2 = 0/0D; //or 0D/0D

System.out.printf("div1 = %s : div2 = %s%n", div1, div2);
System.out.printf("div1 == div2 : %b%n", div1 == div2);
System.out.printf("div1 == div1 : %b%n", div1 == div1);
System.out.printf("div2 == div2 : %b%n", div2 == div2);
System.out.printf("Double.NaN == Double.NaN : %b%n", Double.NaN == Double.NaN);
System.out.printf("Float.NaN == Float.NaN : %b%n", Float.NaN == Float.NaN);

它们产生以下输出.

div1 = NaN : div2 = NaN
div1 == div2 : false
div1 == div1 : false
div2 == div2 : false
Double.NaN == Double.NaN : false
Float.NaN == Float.NaN : false

它们都返回 false. 为什么这个操作(被零除)允许用于浮点数或双精度数?

They all return false. Why is this operation (division by zero) allowed with floating point or double precision numbers?

顺便说一下,我可以理解浮点数(双精度数)的值表示正无穷负无穷不是数字 (NaN)...

By the way, I can understand that floating point numbers (double precision numbers) have their values that represent positive infinity, negative infinity, not a number (NaN)...

推荐答案

简而言之,IEEE-754 标准就是这样规定的,也就是 Java 的 浮点运算基于.

In short, that's the way it's specified in the IEEE-754 standard, which is what Java's Floating-Point Operations are based on.

为什么除以零(或上溢或下溢)不会停止程序或触发错误?为什么数字标准包括非数字"?(NaN)?

754 模型鼓励强大的程序.它不仅适用于数值分析师,也适用于电子表格用户、数据库系统,甚至咖啡壶.NaN 和无穷大的传播规则允许无关紧要的异常消失.类似地,逐渐下溢在精度范围内保持误差属性.

The 754 model encourages robust programs. It is intended not only for numerical analysts but also for spreadsheet users, database systems, or even coffee pots. The propagation rules for NaNs and infinities allow inconsequential exceptions to vanish. Similarly, gradual underflow maintains error properties over a precision's range.

当异常情况需要注意时,可以通过陷阱立即检查它们或在方便的时候通过状态标志检查它们.陷阱可用于停止程序,但不可恢复的情况极为罕见.简单地停止程序不是嵌入式系统或网络代理的选项.更常见的是,陷阱会记录诊断信息或替换有效结果.

When exceptional situations need attention, they can be examined immediately via traps or at a convenient time via status flags. Traps can be used to stop a program, but unrecoverable situations are extremely rare. Simply stopping a program is not an option for embedded systems or network agents. More often, traps log diagnostic information or substitute valid results.

Flags 提供可预测的控制流和速度.它们的使用要求程序员注意异常情况,但标志粘性允许程序员延迟处理异常情况,直到必要.

Flags offer both predictable control flow and speed. Their use requires the programmer be aware of exceptional conditions, but flag stickiness allows programmers to delay handling exceptional conditions until necessary.

这篇关于为什么用浮点(或双精度)数除以零不会抛出 java.lang.ArithmeticException:/在 Java 中被零除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆