52

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

MaciejLisCK
  • 3,706
  • 5
  • 32
  • 39

5 Answers5

33

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

Jon
  • 428,835
  • 81
  • 738
  • 806
31

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • 7
    Except `Math.Ceiling` can return a `decimal` also, which doesn't have `NaN`, `NegativeInfinity` or `PositiveInfinity` fields. – MusiGenesis Apr 17 '11 at 12:30
  • 11
    You're confusing the cause and the consequence. The documentation says so because it was implemented that way. It the CLR team implement return value as `int`, then there would be no single word about `NaN` e.t.c. So -1, sorry. – Dmitrii Lobanov Apr 17 '11 at 12:32
  • 3
    @Dmitry: But then the function would be undefined when passed values of NaN or infinity. (This is why it can return decimal when given a decimal, because those can't be NaN or infinity.) –  Apr 17 '11 at 15:03
  • @Joe: I assumed that if the return value is `Int` then parameter is also `Int`, because it is natural that you expect the return value of the same type (in the mentioned context, of course, because if method name is something like `ToInt()` then the return value expected to be `int`). – Dmitrii Lobanov Apr 17 '11 at 16:20
  • 2
    @Dmitry: I'm not sure you've got a grasp of the point here. Ceiling(Int) would be useless. –  Apr 17 '11 at 18:58
  • 2
    @Joe: oh, I don't expect that there should be parameter of `int` type ever, it definitely would be useless. I just tried to show that IF there would be an `int` parameter, then the return value expected to be an `int`, i.e. you expect to get out what you have passed in if you are using some simple calculation over numbers. – Dmitrii Lobanov Apr 18 '11 at 00:59
6

Math.Ceiling can return either a double or a decimal, depending on the type passed in. In other words, the output type of the method matches the input type (quite sensibly).

They could have added a third overload that takes an int and returns an int, but there wouldn't have been much point to this - the function would always just return its input.

You seem to be assuming that the purpose of Math.Ceiling is to cast a floating-point value to an integer, but that's usually not how it's used.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
3

It has to return double in order to be complete. Any math operation involving a NaN always returns NaN. Thus if you pass a NaN to ceiling() function one would not be able to return NaN, as there is no equivalent in Int. Also given that Double has a wider range what would one return for those out of range integer values ? What does one return for +/- inf ?

mP.
  • 18,002
  • 10
  • 71
  • 105
0

Because double can contain larger numbers than int or long. Same reason there's no implicit cast from double to int.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • 1
    Actually a `long` can contain larger values than a `double` because a long is 64-bits. – Kevin Apr 17 '11 at 14:22
  • 1
    @Kevin: A `long` can *not* contain larger values than a `double`, unless you also require *no loss of precision*. If loss of precision is acceptable, a `double` can contain far larger values. – Jon Apr 17 '11 at 20:28
  • @Kevin - I'm assuming the down-vote is yours? If so that's a bit harsh. There are other answers here that mention the larger numbers possible with doubles, and you didn't down-vote them. – Andrew Cooper Apr 17 '11 at 23:28
  • @Jon: Could you expand on this? A double doesn't have the full 64-bits to hold a number so how can it contain a larger value? – Kevin Apr 18 '11 at 09:01
  • 1
    @Kevin: See http://en.wikipedia.org/wiki/Binary64 -- the maximum value a `double` can hold is about 1.8 * 10^308. Of course at that point there is not much precision (the "previous", smaller value is about 10^297 smaller). – Jon Apr 19 '11 at 08:33