Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# Math.Floor() return Double instead of Int [duplicate]

Tags:

c#

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?

like image 723
Jiew Meng Avatar asked Nov 09 '10 05:11

Jiew Meng


2 Answers

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.

like image 183
userx Avatar answered Oct 13 '22 06:10

userx


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.

like image 38
abelenky Avatar answered Oct 13 '22 05:10

abelenky