为什么除以 0.0 时 Java 不抛出异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2381544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't Java throw an Exception when dividing by 0.0?
提问by froadie
I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100;
- where both of the numbers are double
s. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when I did a test run with values of 0.0 for both oldNum and newNum, execution continued as if nothing had happened and no error was thrown. Running this code with int
s would definitely cause an arithmetic division-by-zero exception. Why does Java ignore it when it comes to double
s?
我有代码来计算两个数字之间的百分比差异 - (oldNum - newNum) / oldNum * 100;
- 其中两个数字都是double
s。我希望在 oldNum 为 0 的情况下必须添加某种检查/异常处理。但是,当我对 oldNum 和 newNum 的值都为 0.0 进行测试运行时,执行继续,好像什么也没发生并且没有抛出错误。使用int
s运行此代码肯定会导致算术被零除异常。为什么Java在谈到double
s时会忽略它?
采纳答案by Kris
The result of division by zero is, mathematically speaking, undefined, which can be expressed with a float/double (as NaN
- not a number), it isn't, however, wrong in any fundamental sense.
除以零的结果从数学上讲是undefined,可以用浮点数/双精度数表示(如NaN
- 不是数字),但是从任何基本意义上讲,它都没有错。
As an integer must hold a specific numerical value, an error must be thrown on division by zero when dealing with them.
由于整数必须包含特定的数值,因此在处理它们时必须在被零除时抛出错误。
回答by Mike
The way a double is stored is quite different to an int. See http://firstclassthoughts.co.uk/java/traps/java_double_traps.htmlfor a more detailed explanation on how Java handles double calculations. You should also read up on Floating Point numbers, in particular the concept of Not a Number (NaN).
double 的存储方式与 int 完全不同。有关Java 如何处理双重计算的更详细说明,请参见http://firstclassthoughts.co.uk/java/traps/java_double_traps.html。您还应该阅读浮点数,特别是Not a Number (NaN)的概念。
If you're interested in learning more about floating point representation, I'd advise reading this document(Word format, sorry). It delves into the binary representation of numbers, which may be helpful to your understanding.
如果您有兴趣了解有关浮点表示的更多信息,我建议您阅读此文档(Word 格式,抱歉)。它深入研究了数字的二进制表示,这可能对您的理解有所帮助。
回答by Michael Borgwardt
Java's float
and double
types, like pretty much any other language out there (and pretty much any hardware FP unit), implement the IEEE 754standard for floating point math, which mandates division by zero to return a special "infinity" value. Throwing an exception would actually violate that standard.
Javafloat
和double
类型,就像几乎所有其他语言(以及几乎所有硬件 FP 单元)一样,实现了浮点数学的IEEE 754标准,该标准要求除以零以返回一个特殊的“无穷大”值。抛出异常实际上会违反该标准。
Integer arithmetic (implemented as two's complementrepresentation by Java and most other languages and hardware) is different and has no special infinity or NaN values, thus throwing exceptions is a useful behaviour there.
整数算术(由 Java 和大多数其他语言和硬件实现为二进制补码表示)是不同的,并且没有特殊的无穷大或 NaN 值,因此在那里抛出异常是一种有用的行为。
回答by ziyapathan
Though Java developers know about the double primitive type and Double
class, while doing floating point arithmetic they don't pay enough attention to Double.INFINITY
, NaN
, -0.0
and other rules that govern the arithmetic calculations involving them.
虽然Java开发人员了解双基本类型和Double
类,同时做浮点运算他们没有给予足够的重视Double.INFINITY
,NaN
,-0.0
和支配它们涉及算术计算的其他规则。
The simple answer to this question is that it will not throw ArithmeticException
and return Double.INFINITY
. Also, note that the comparison x == Double.NaN
always evaluates to false
, even if x
itself is a NaN
.
这个问题的简单答案是它不会 throwArithmeticException
和 return Double.INFINITY
。另外,请注意,比较x == Double.NaN
总是计算为false
,即使它x
本身是NaN
。
To test if x
is a NaN
, one should use the method call Double.isNaN(x)
to check if given number is NaN or not. This is very close to NULL
in SQL
.
要测试是否x
是 a NaN
,应该使用方法调用Double.isNaN(x)
来检查给定的数字是否为 NaN。这与NULL
in非常接近SQL
。
It may helpful for you.
它可能对你有帮助。
回答by Raman Gupta
When divided by zero ( 0 or 0.00 )
除以零时(0 或 0.00)
If you divide double by 0, JVM will show Infinity.
public static void main(String [] args){ double a=10.00; System.out.println(a/0); }
Console:
Infinity
If you divide int by 0, then JVM will throw Arithmetic Exception.
public static void main(String [] args){ int a=10; System.out.println(a/0); }
Console:
Exception in thread "main" java.lang.ArithmeticException: / by zero
But if we divide int by 0.0, then JVM will show Infinity:
public static void main(String [] args){ int a=10; System.out.println(a/0.0); }
Console:
Infinity
如果将 double 除以 0,JVM 将显示Infinity。
public static void main(String [] args){ double a=10.00; System.out.println(a/0); }
安慰:
Infinity
如果将 int 除以 0,则 JVM 将抛出算术异常。
public static void main(String [] args){ int a=10; System.out.println(a/0); }
安慰:
Exception in thread "main" java.lang.ArithmeticException: / by zero
但是如果我们将 int 除以 0.0,那么 JVM 将显示 Infinity:
public static void main(String [] args){ int a=10; System.out.println(a/0.0); }
安慰:
Infinity
This is because JVM will automatically type cast int to double, so we get infinity instead of ArithmeticException.
这是因为 JVM 会自动将 int 类型转换为 double,所以我们得到的是无穷大而不是 ArithmeticException。