Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 459
Yahya Avatar asked Aug 15 '19 08:08

Yahya


People also ask

Why 0 divided by 0 is NaN?

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.

Is NaN equal to zero?

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.


1 Answers

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 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.

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)

like image 116
Pavel Anikhouski Avatar answered Oct 09 '22 09:10

Pavel Anikhouski