Why Math.Ceiling returns double?

后端 未结 5 1745
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 15:52

In C# the method Math.Ceiling returns a double value. Why does it not return int?

5条回答
  •  生来不讨喜
    2021-01-07 16:38

    double has a greater value range than int:

    The Double value type represents a double-precision 64-bit number with values ranging from negative 1.79769313486232e308 to positive 1.79769313486232e308, as well as positive or negative zero, PositiveInfinity, NegativeInfinity, and Not-a-Number (NaN).

    Double complies with the IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic.

    That standard says that double has a 52-bit mantissa, which means it can represent any integer up to 52 bits long without loss of precision.

    Therefore if the input is large enough, the output doesn't fit inside an int (which only has 32 bits).

提交回复
热议问题