Why does the compiler not show an error when we try to divide a variable by zero

前端 未结 5 637
野的像风
野的像风 2021-01-06 08:20

If we try to run this code:

int d = 10/0;

We get a compiler error. So we cannot divide by zero.

Now consider this code:

<         


        
5条回答
  •  甜味超标
    2021-01-06 08:50

    the c# compiler is only doing constants value arithmetic check, and there for can tell that you can't do 10/0. it's a lot more then you think for a compiler to do that.

    even more then that: the c# compiler allows:

    1.0 / 0 // Infinity
    

    because:

    Floating-point arithmetic overflow or division by zero never throws an exception, because floating-point types are based on IEEE 754 and so have provisions for representing infinity and NaN (Not a Number).

提交回复
热议问题