在java分区操作中决定Nan和Infinity的是什么

以下代码的输出让我感到困惑.为什么NaN有时和Infinity其他时间?

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

这里的决定因素是什么?

解决方法:

这是因为IEEE标准浮点运算(IEEE 754),它是1985年由电气和电子工程师协会(IEEE)建立的浮点运算的技术标准.

目的:

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
.

规则:

In IEEE 754 arithmetic, a ÷ +0 is positive infinity when a is
positive, negative infinity when a is negative, and NaN when a = ±0.

Source

上一篇:(Alpha)Let's-技术文档(技术规格说明书)


下一篇:为什么PHP浮点除法和POW会给出错误的结果和意外的结果?