Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Math.Ceiling returns double?

Tags:

c#

.net

ceil

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

like image 619
MaciejLisCK Avatar asked Apr 17 '11 12:04

MaciejLisCK


People also ask

Why does Java ceil return double?

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.

Does Math ceil return an integer?

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.

What is the ceiling of a decimal?

Ceiling(Decimal) Returns the smallest integral value that is greater than or equal to the specified decimal number.

What does Math ceiling to in C#?

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.


2 Answers

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

like image 146
Jon Avatar answered Oct 17 '22 00:10

Jon


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.

like image 28
RoflcoptrException Avatar answered Oct 16 '22 22:10

RoflcoptrException