开发者

why math.Ceiling (double a) not return int directly? [duplicate]

开发者 https://www.devze.com 2023-04-06 10:47 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Why doesn't Math.Round/Floor/Ceiling return long or int?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why doesn't Math.Round/Floor/Ceiling return long or int?

msdn defined this method:Returns the smallest integer greater than or equal to the specified double-precision floating-point number.

b开发者_运维知识库ut in fact,it is

public static double Ceiling (
    double a
)

why not return int directly? what does microsoft think of ?


It's because the range of a double (±5.0 × 10−324 to ±1.7 × 10308) is much greater than the range of an int (-2,147,483,648 to 2,147,483,647). If the return type were int many possible inputs would fail. For example Math.Ceiling might be forced to throw an OverflowException in a checked context, or it might even return an incorrect result in an unchecked context. This is undesirable behaviour.

Also some special values such as NaN and PositiveInfinity can be returned by this method. This is only possible if the return type is double.

If you believe that the result will fit into an int, you can add an explicit cast:

int result = (int)Math.Ceiling(a);
0

精彩评论

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