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

2020-07-02 09:17发布

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?

标签: c#
2条回答
欢心
2楼-- · 2020-07-02 09:40

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.

查看更多
够拽才男人
3楼-- · 2020-07-02 09:44

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.

查看更多
登录 后发表回答