双 d=1/0.0 与 双 d=1/0
double d=1/0.0;
System.out.println(d);
它打印 Infinity
,但如果我们编写 double d=1/0;
并打印它,我们会得到这个异常: Exception 在线程“main”中 java.lang.ArithmeticException: / 为零 at D.main(D.java:3)
为什么 Java 在一种情况下知道除零是无穷大,但对于 int 0 却没有定义? 在这两种情况下,d 都是双倍的,并且在这两种情况下,结果都是无穷大。
double d=1/0.0;
System.out.println(d);
It prints Infinity
, but if we will write double d=1/0;
and print it we'll get this exception: Exception
Why does Java know in one case that diving by zero is infinity but for the int 0 it is not defined?
in thread "main" java.lang.ArithmeticException: / by zero
at D.main(D.java:3)
In both cases d is double and in both cases the result is infinity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
浮点数据类型有一个保留的特殊值来表示无穷大,而整数值则没有。
在您的代码中
1/0
是一个整数除法,当然会失败。但是,1/0.0
是浮点除法,因此会产生无穷大
。Floating point data types have a special value reserved to represent infinity, integer values do not.
In your code
1/0
is an integer division that, of course, fails. However,1/0.0
is a floating point division and so results inInfinity
.严格来说,
1.0/0.0
根本不是无穷大,它是未定义的。正如 David 在他的回答中所说,浮点数有一种表达不在它可以表示的最高数字和最低数字范围内的数字的方式。这些值统称为“非数字”或简称为 NaN。 NaN 也可能来自真正无限的计算(例如
limx -> 0 ln2 x
),这些值是有限的,但溢出浮点数可以表示的范围(例如 10100100),以及未定义的值(例如 1/0)。浮点数不能清楚地区分未定义值、溢出和无穷大;该计算结果取决于什么位组合。由于仅打印“NaN”或“不是数字”对于那些不知道如何表示浮点值的人来说有点难以理解,因此该格式化程序仅打印“Infinity”或有时“-Infinity”,因为它提供了相同的当您确实知道 FP NaN 的含义时,这就是信息级别;当您不知道时,它也具有一定的意义。
整数没有任何可与浮点 NaN 相媲美的东西。由于执行 1/0 时整数没有任何合理的值,因此剩下的唯一选择就是引发异常。
用机器语言编写的相同代码可以调用中断(相当于 Java 异常),也可以设置条件寄存器(该寄存器是一个全局值,指示最后一次计算是除以零)。其中可用的内容因平台而异。
strictly speaking,
1.0/0.0
isn't infinity at all, it's undefined.As David says in his answer, Floats have a way of expressing a number that is not in the range of the highest number it can represent and the lowest. These values are collectively known as "Not a number" or just NaNs. NaNs can also occur from calculations that really are infinite (such as
limx -> 0 ln2 x
), values that are finite but overflow the range floats can represent (like 10100100), as well as undefined values like 1/0.Floating point numbers don't quite clearly distinguish among undefined values, overflow and infinity; what combination of bits results from that calculation depends. Since just printing "NaN" or "Not a Number" is a bit harder to understand for folks that don't know how floating point values are represented, that formatter just prints "Infinity" or sometimes "-Infinity" Since it provides the same level of information when you do know what FP NaN's are all about, and has some meaning when you don't.
Integers don't have anything comparable to floating point NaN's. Since there's no sensible value for an integer to take when you do 1/0, the only option left is to raise an exception.
The same code written in machine language can either invoke an interrupt, which is comparable to a Java exception, or set a condition register, which would be a global value to indicate that the last calculation was a divide by zero. which of those are available varies a bit by platform.