为什么 C# 允许浮点类型中的非零数除以零?
为什么 C# 允许:
1.0 / 0 // Infinity
不允许:
1 / 0 // Division by constant zero [Compile time error]
从数学上讲,整数和浮点数除以零有什么区别吗?
Why C# allows:
1.0 / 0 // Infinity
And doesn't allow:
1 / 0 // Division by constant zero [Compile time error]
Mathematically, is there any differences between integral and floating-point numbers in dividing by zero?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 Microsoft 的说法,“浮点算术溢出或除以零永远不会引发异常,因为浮点类型基于 IEEE 754,因此有表示无穷大和 NaN(非数字)的规定。”
有关此内容的更多信息,请此处。
According to Microsoft, "Floating-point arithmetic overflow or division by zero never throws an exception, because floating-point types are based on IEEE 754 and so have provisions for representing infinity and NaN (Not a Number)."
More on this here.
从数学上来说,没有区别。然而,对于计算机,只有标准 IEEE-754 浮点规范具有特殊值代表±∞。整数只能保存...整数:-)
Mathematically, there is no difference. With computers, however, only the standard IEEE-754 floating-point specification has special values for representing ±∞. Integers can only hold... integers :-)
IEEE 浮点运算标准 (IEEE 754) 是使用最广泛的浮点计算的标准,许多硬件和软件实现都遵循该标准,包括 C# 编译器。
这意味着 C# 中的浮点变量可以包含表示奇怪生物的位模式,例如 PositiveInfinity、NegativeInfinity 和 Not-a-Number(缩写为 NaN)。根据 IEEE 754 算术规则,任何这些非有限浮点值都可以通过某些运算生成。例如,无效的浮点运算(例如将零除以零)会导致 NaN。
在您的具体示例中,您可以看到 C#(与 VB 不同)重载 / 运算符以表示整数或浮点除法,具体取决于所涉及数字的数字类型。
在第一个示例中,编译器看到1.0,因此使用浮点除法并将结果放入浮点变量中。该变量包含无穷大的表示,编译器对此很满意。
在第二个示例中,编译器看到 1,因此使用整数除法并将结果放入整数变量中。由于 C# 中的整型使用二进制补码系统进行表示,并且不使用任何特殊的位模式来表示无穷大(或 NaN),因此编译器会给出错误。
还有其他有趣的浮点微妙之处。值得一读 Eric Lippert 的博客文章主题。
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is the most widely-used standard for floating-point computation, and is followed by many hardware and software implementations, including the C# compiler.
This means that a floating-point variable in C# can contain a bit pattern that represents strange creatures such as PositiveInfinity, NegativeInfinity, and Not-a-Number (abbreviated as NaN). Under the IEEE 754 arithmetic rules, any of these non-finite floating-point values can be generated by certain operations. For example, an invalid floating-point operation such as dividing zero by zero results in NaN.
In your specific examples, you can see that C# (unlike VB) overloads the / operator to mean either integer or floating-point division, depending on the numeric types of the numbers involved.
In your first example the compiler sees 1.0, and therefore uses floating-point division and puts the result into a floating-point variable. That variable contains a representation of infinity and the compiler is fine with that.
In your second example the compiler sees 1, and therefore uses integer division and puts the result into an integer variable. Because integral types in C# use two's complement system for representation, and don't use any special bit patterns to represent infinity (or NaN), the compiler gives an error.
There are also other interesting floating-point subtleties. And it's worth reading Eric Lippert's blog entry on the subject.
浮点除法受 IEEE754 管辖,它规定除以零应该是无穷大。整数除法没有这样的标准,所以他们只是遵循数学的标准规则。
Floating point division is govered by IEEE754, which specifies that divide by zero should be infinity. There is no such standard for integer division, so they simply went with the standard rules of math.