Why (int)double.NaN and (int)double.PositiveInfinity are 0?

前端 未结 2 2126
栀梦
栀梦 2021-01-03 22:22

in C#, if you 0/0 you get an exception.

But if you 0.0/0 or 0.0/0.0 you get double.NaN and

2条回答
  •  执笔经年
    2021-01-03 22:55

    This is mainly due to the fact that double.NAN and double.PositiveInfity ( or double.Negative for the matter) are not numbers but rather values to identify a concept.

    A method or operator returns NaN when the result of an operation is undefined. For example, the result of dividing zero by zero is NaN, as the following example shows.

    try this to illustrate that concept.

    if ((0 / zero) == Double.NaN) 
         Console.WriteLine("0 / 0 can be tested with Double.NaN.");
      else 
         Console.WriteLine("0 / 0 cannot be tested with Double.NaN; use Double.IsNan() instead.");
    

提交回复
热议问题