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

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

问题描述

以下语句抛出 java.lang.ArithmeticException:/ by zero ,显而易见。

The following statement throws java.lang.ArithmeticException: / by zero as obvious.

System.out.println(0/0);

因为文字 0 被认为是在整数算术中不允许 int literal并除以零。

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

以下情况不't 抛出任何异常,例如 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中抛出java.lang.ArithmeticException:/ by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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