开发者

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

This question already has answers here: 开发者_如何学运维 Closed 10 years ago.

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜