gpt4 book ai didi

java - 什么决定了java除法运算中的Nan和Infinity

转载 作者:搜寻专家 更新时间:2023-10-31 19:59:34 28 4
gpt4 key购买 nike

下面代码的输出让我很困惑。为什么有时是 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),这是电气和电子工程师协会 (IEEE) 于 1985 年制定的浮点计算技术标准.


目的:

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

关于java - 什么决定了java除法运算中的Nan和Infinity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51801054/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com