gpt4 book ai didi

java - 双除以零 : Why is the result inconsistent?

转载 作者:行者123 更新时间:2023-11-29 06:52:19 25 4
gpt4 key购买 nike

为什么结果是:

double a = 0.0/0.0;
double b = 0/0.0;

= NaN

但是结果例如:

double e = 0.1/0.0;
double e = 12.0/0.0;
double f = 1.0/0.0;

= 无限

我知道 doublefloat 划分在某种程度上有点不同。我对生成的 NaN 非常满意,因为当某个值除以零时,结果未定义。但为什么他们定义大于零除以零的结果是 Infitity?这与他们用来执行浮点除法的方法有关吗?

最佳答案

JLS 15.17.2明确指出

Division of a zero by a zero results in NaN; division of zero by any other finite value results in a signed zero. The sign is determined by the rule stated above.

Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.

JLS 也说了这些常量在操作上的区别。

If either operand is NaN, the result is NaN.

Division of an infinity by an infinity results in NaN.

Division of an infinity by a finite value results in a signed infinity. The sign is determined by the rule stated above.

一点也不矛盾。

关于java - 双除以零 : Why is the result inconsistent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983692/

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