为什么 0/0 是 NaN 而 0/0.00 不是

Why 0/0 is NaN but 0/0.00 isn't

使用DataTable.Compute,并构建了一些案例进行测试:

dt.Compute("0/0", null); //returns NaN when converted to double

dt.Compute("0/0.00", null); //results in DivideByZero exception

我已经更改了我的代码来处理这两个问题。但是很想知道这里发生了什么?

我猜,这是因为带小数点的文字被威胁为 System.Decimal 并导致 DivideByZeroException

根据DataColumn.Expression

Integer literals [+-]?[0-9]+ are treated as System.Int32, System.Int64 or System.Double

Real literals without scientific notation, but with a decimal point, are treated as System.Decimal. If the number exceeds the maximum or minimum values supported by System.Decimal, then it is parsed as a System.Double.

根据 DivideByZeroException

The exception that is thrown when there is an attempt to divide an integral or Decimal value by zero.

对于 System.Double 它 returns Nan,因为如果操作是除法并且常量是整数,它会更改为双结果类型,根据 reference source (感谢@steve16351很高兴找到)