是什么决定了Java除法运算中的Nan和Infinity [英] What decides Nan and Infinity in java division operations
问题描述
以下代码的输出使我感到困惑。为什么有时 NaN
有时是
Output of the below code confusing me. Why NaN
sometimes and Infinity other times ?
public static void main (String[] args) {
double a = 0.0;
double b = 1.0;
int c = 0;
System.out.println(a/0.0);
System.out.println(a/0);
System.out.println(b/0.0);
System.out.println(b/0);
System.out.println(c/0.0);
System.out.println(c/0);
}
输出为:
NaN
NaN
Infinity
Infinity
NaN
Exception in thread "main" java.lang.ArithmeticException: / by zero
这里的决定因素是什么?
What is the deciding factor here ?
推荐答案
这是因为 IEEE浮点算法标准(IEEE 754),它是1985年由浮点运算所建立的技术标准。电气和电子工程师协会(IEEE)。
This is because of The IEEE Standard for Floating-Point Arithmetic (IEEE 754) which is a technical standard for floating-point computation established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE).
IEEE浮点标准..指定每个浮点
点算术运算(包括除以零的 都具有
井)定义的结果 。该标准支持带符号的零以及
infinity 和 NaN (不是数字)。有两个零:+0(正零)和-0(负零),并且此 除以除 时的
模糊度。
The IEEE floating-point standard,.. specifies that every floating point arithmetic operation, including division by zero, has a well-defined result. The standard supports signed zero, as well as infinity and NaN (not a number). There are two zeroes: +0 (positive zero) and −0 (negative zero) and this removes any ambiguity when dividing.
规则:
The Rule:
在IEEE 754算法中,当
a
时,a÷+0
是正无穷大。是 b 正,负无穷大(当a
是负和<当a =±0时,strong> NaN 。
In IEEE 754 arithmetic,
a ÷ +0
is positive infinity whena
is positive, negative infinity whena
is negative, and NaN whena = ±0.
这篇关于是什么决定了Java除法运算中的Nan和Infinity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!