Why does this method return double.PositiveInfinity not DivideByZeroException?

后端 未结 4 682
醉话见心
醉话见心 2020-12-07 00:59

I ran the following snippet in the VS2015 C# interactive and got some very weird behavior.

> double divide(double a, double b)
. {
.     try
.     {
.             


        
4条回答
  •  自闭症患者
    2020-12-07 01:16

    Why did the method return infinity while 3 / 0 threw an error and 3 / b threw a formated error?

    Because in the first case the 0 is not an integer, is a double. While in the second is an integer. That you should realize here is that a double is a floating point number. So it doesn't have an exact value like an integer. On the other hand an integer can be represented by a computer with 100% accuracy.

    Here you could find a very nice article regarding floating point numbers.

提交回复
热议问题