Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java(为什么用浮点数(或双精度)数字除以零不会抛出 java.lang.ArithmeticException:/Java 中的零)
问题描述
以下语句显然会抛出 java.lang.ArithmeticException:/by zero
.
The following statement throws java.lang.ArithmeticException: / by zero
as obvious.
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 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 中的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么用浮点数(或双精度)数字除以零不会抛出
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01