Why Math.Ceiling returns double?

MaciejLisCK picture MaciejLisCK · Apr 17, 2011 · Viewed 9.2k times · Source

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

Answer

Jon picture Jon · Apr 17, 2011

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).