Why [float.MaxValue == float.MaxValue + 1] does return true?

后端 未结 5 1297
太阳男子
太阳男子 2020-12-06 09:07

I wonder if you could explain the Overflow in floating-point types.

float.MaxValue == float.MaxValue + 1 // returns true
5条回答
  •  不思量自难忘°
    2020-12-06 09:45

    Because the 1 is way too small to make a dent in the float.MaxValue value.

    Anything less than 1e32 will fall below the precision of the float, so it's in effect the same as adding a zero.

    Edit:

    ulrichb showed that a value of 1e23 does actually affect float.MaxValue, which has to mean that you are not comparing floats at all, but doubles. The compiler converts all values to doubles before adding and comparing.

提交回复
热议问题