为什么 Math.Ceiling 返回 double?

发布于 2024-11-02 01:50:45 字数 93 浏览 0 评论 0原文

在 C# 中,Math.Ceiling 方法返回一个 double 值。为什么它不返回int

In C# the method Math.Ceiling returns a double value. Why does it not return int?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北笙凉宸 2024-11-09 01:50:45

double 的取值范围比 int 更大:

Double 值类型表示
双精度 64 位数字
值范围从负
1.79769313486232e308 到正 1.79769313486232e308,以及正零或负零,
正无穷大、负无穷大、
和非数字 (NaN)。

双重符合 IEC 标准
60559:1989 (IEEE 754) 标准
二进制浮点运算。

该标准规定,double 具有 52 位尾数,这意味着它可以表示最长 52 位的任何整数,而不会损失精度。

因此,如果输入足够大,则输出无法容纳在 int(只有 32 位)中。

double has a greater value range than int:

The Double value type represents a
double-precision 64-bit number with
values ranging from negative
1.79769313486232e308 to positive 1.79769313486232e308, as well as positive or negative zero,
PositiveInfinity, NegativeInfinity,
and Not-a-Number (NaN).

Double complies with the IEC
60559:1989 (IEEE 754) standard for
binary floating-point arithmetic.

That standard says that double has a 52-bit mantissa, which means it can represent any integer up to 52 bits long without loss of precision.

Therefore if the input is large enough, the output doesn't fit inside an int (which only has 32 bits).

郁金香雨 2024-11-09 01:50:45

文档中关于返回值的说明是:

大于或等于a的最小整数。如果 a 等于 NaN、NegativeInfinity 或 PositiveInfinity,则返回该值。

因此,返回值必须是 double,因为 NaN、NegativeInfinity 和 PositiveInfinity 是 Double 的字段。

The documentation says about the return value:

The smallest whole number greater than or equal to a. If a is equal to NaN, NegativeInfinity, or PositiveInfinity, that value is returned.

Therefore the return value has to be double since NaN, NegativeInfinity and PositiveInfinity are fields of Double.

祁梦 2024-11-09 01:50:45

Math.Ceiling 可以返回 doubledecimal,具体取决于传入的类型。换句话说,该方法的输出类型匹配输入类型(非常明智)。

他们本可以添加第三个重载,该重载接受一个 int 并返回一个 int ,但这没有多大意义 - 该函数总是只返回其输入。

您似乎假设 Math.Ceiling 的目的是将浮点值转换为整数,但这通常不是它的使用方式。

Math.Ceiling can return either a double or a decimal, depending on the type passed in. In other words, the output type of the method matches the input type (quite sensibly).

They could have added a third overload that takes an int and returns an int, but there wouldn't have been much point to this - the function would always just return its input.

You seem to be assuming that the purpose of Math.Ceiling is to cast a floating-point value to an integer, but that's usually not how it's used.

何时共饮酒 2024-11-09 01:50:45

它必须返回 double 才能完整。任何涉及 NaN 的数学运算始终返回 NaN。因此,如果将 NaN 传递给 Ceiling() 函数,则将无法返回 NaN,因为 Int 中没有等效项。另外,考虑到 Double 的范围更广,对于那些超出范围的整数值会返回什么? +/- inf 返回什么?

It has to return double in order to be complete. Any math operation involving a NaN always returns NaN. Thus if you pass a NaN to ceiling() function one would not be able to return NaN, as there is no equivalent in Int. Also given that Double has a wider range what would one return for those out of range integer values ? What does one return for +/- inf ?

暗恋未遂 2024-11-09 01:50:45

因为 double 可以包含比 intlong 更大的数字。同样的原因,没有从 doubleint 的隐式转换。

Because double can contain larger numbers than int or long. Same reason there's no implicit cast from double to int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文