Using DataTable.Compute
, and have built some cases to test:
dt.Compute("0/0", null); //returns NaN when converted to double
dt.Compute("0/0.00", null); //results in DivideByZero exception
I have changed my code to handle both. But curious to know what's going on here?
Although without precision we can't make the assertion of whether or not 1/0.0 is infinity, undefined, or just a very large inaccurate number. Hence returns NaN instead. Because anything divded by 0 is actually infinity. Hence it is wrong to really say its undefined or an error.
Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such as infinities. In mathematics, zero divided by zero is undefined and is therefore represented by NaN in computing systems.
I guess, that it happens because literals with decimal points are threated as System.Decimal
and cause the DivideByZeroException
According to DataColumn.Expression
Integer literals [+-]?[0-9]+ are treated as
System.Int32
,System.Int64
orSystem.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 bySystem.Decimal
, then it is parsed as aSystem.Double
.
Accroding to DivideByZeroException
The exception that is thrown when there is an attempt to divide an integral or
Decimal
value by zero.
For System.Double
it returns Nan, because if the operation is a division and the constants are integers it changes to a double result type, according to reference source (Thanks @steve16351 for nice found)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With