18

Possible Duplicate:
Why does Math.Floor(Double) return a value of type Double?

Why does C# Math.Floor() return double instead of int

From the MSDN Docs:

Returns the largest integer less than or equal to the specified double-precision floating-point number

it says it returns an integer. Its ok to return a double, I can always cast it to an int but its just quite strange, isn't it?

Community
  • 1
  • 1
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • 1
    It is poorly worded. Arguably it should say "whole number" (or something else, even in the notes) to avoid ambiguity with with the *int*eger data-type(s). However, [integer](http://en.wikipedia.org/wiki/Integer) is a mathematical term, so, while confusing here, is technically correct. –  Nov 09 '10 at 06:05

2 Answers2

41

Not really, considering that a double can be a much higher magnitude than an int. You wouldn't want to overflow an int with the large value that a double could be.

Just to show you what I mean:

Double.MaxValue = 1.7976931348623157E+308

Integer.MaxValue = 2,147,483,647

So you could have a double that is 3,000,000,000.50 and floor it, which would overflow the max value of an int.

userx
  • 3,769
  • 1
  • 23
  • 33
  • 3
    That's a good explanation for why it makes sense... the explanation why it is that way is much simpler though -- the FPU result is a `double`. Since many times you want to use it as a `double` (maybe subtract it from the argument, to get the fractional part), it wouldn't make sense to convert it and then have to convert it back. – Ben Voigt Nov 09 '10 at 05:16
  • 1
    @Ben Voigt - Also a good point, but doesn't explain why the returned value could NOT be of Integer type as the documentation verges on suggesting. Just because the FPU outputs something doesn't mean that a method needs to return that result, esp in a non-native runtime environment. The MSDN documentation probably should have used the phrase "integral part" or something like that instead to avoid confusion with the type name. – userx Nov 09 '10 at 05:26
  • 2
    if it was a size thing, surely they could just return a long / Int64? – mike Nov 09 '10 at 05:50
  • I agree with @userx and we could also think about the cost of conversion, a integer exist in the double domain and the opposite is not true, so is better to represent a integer as a double then not. – Oakcool Nov 09 '10 at 06:16
  • 2
    @mynye - Even an Long / Int64 isn't big enough. MaxValue of an Int64 is 9,223,372,036,854,775,807, which is 9E+18. A double can be as large as 1E+308 which is a LOT bigger. The reason for this is that part structure of a double (and singles / decimals) denotes the exponent for a fraction. Doubles and Ints are very different and serve very different purposes. I wouldn't say that it is any better to represent an int as a double as there are may be cases where a double may be less precise (I'm not sure, a double has 53-bits for its fraction, definitely true for a single though). – userx Nov 09 '10 at 07:35
3

Because the INPUT is a double, the OUTPUT must also be a double, or a lot of potential output would not fit into the output variable.

In mathematical terms, the domain and the range of the function must have the same size.

abelenky
  • 63,815
  • 23
  • 109
  • 159