In C# the method Math.Ceiling
returns a double
value. Why does it not return int
?
The returned value is of type double. Following is the syntax of ceil() method. Since the definition of ceil() function has double datatype as argument, you can pass int, float or long as arguments; because these datatypes will implicitly promote to double.
The Math. ceil() function always rounds a number up to the next largest integer. Note: Math. ceil([ null ](/en-US/docs/Web/JavaScript/Reference/Operators/null)) returns integer 0 and does not give a NaN error.
Ceiling(Decimal) Returns the smallest integral value that is greater than or equal to the specified decimal number.
Ceiling() Method in C# The Math. Ceiling() method in C# is used to return the smallest integral value greater than or equal to the specified number.
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).
The documentation says about the return value:
The smallest whole number greater than or equal to a. If a is equal to NaN, NegativeInfinity, or PositiveInfinity, that value is returned.
Therefore the return value has to be double since NaN, NegativeInfinity and PositiveInfinity are fields of Double.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With