xpo*_*ort 4 c# compiler-construction
根据设计,为什么C#编译器允许将任何float或double值除以零?
class Program
{
static void Main(string[] args)
{
double x = 0.0 / 0;
float y = 1f / 0;
}
}
Run Code Online (Sandbox Code Playgroud)
Joe*_*oey 10
因为IEEE 754浮点值具有特殊的非数字值来处理这个问题:
PS Home:\> 1.0/0
Infinity
PS Home:\> 0.0/0
NaN
Run Code Online (Sandbox Code Playgroud)
将整数除以零总是一个例外(在C#意义上1),所以你可以直接抛出异常.
1将浮点数除以零也是一个例外,但处于完全不同的水平,许多编程语言将其抽象出来.