Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

By design, why does the C# compiler allows any float or double values to be divided by zero?

By design, why does the C# compiler allows any float or double values to be divided by zero?

class Program
{
    static void Main(string[] args)
    {
        double x = 0.0 / 0;

        float y = 1f / 0;


    }
}
like image 556
xport Avatar asked Dec 23 '22 00:12

xport


2 Answers

Because IEEE 754 floating-point values have special non-numeric values to deal with this:

PS Home:\> 1.0/0
Infinity
PS Home:\> 0.0/0
NaN

whereas dividing an integer by zero is always an exception (in the C# sense1), so you could just throw the exception directly.


1 Dividing a floating-point number by zero is also an exception but at a completely different level and many programming languages abstract this away.

like image 161
Joey Avatar answered Dec 26 '22 08:12

Joey


Because floating point values have a valid (and genuinely useful) representation of infinity, whereas integers of any type do not.

like image 22
Drew Hall Avatar answered Dec 26 '22 08:12

Drew Hall