为什么 numeric_limits::max() > numeric_limits::无穷大()?

发布于 2025-01-11 19:33:11 字数 436 浏览 2 评论 0原文

我正在阅读在C++中将int设置为无穷大。我明白,当需要真正的无穷大时,应该使用 numeric_limits::infinity();我猜其背后的基本原理是,通常整型没有指定用于表示特殊状态的值,例如 NaNInf 等,就像 IEEE 754 浮点数那样(同样,C++ 也没有) t 两者都不要求 - 使用的 intfloat 留给实现);但 max > 仍然具有误导性。对于给定类型,无穷大。我试图理解标准中此调用背后的基本原理。如果无限对于一个类型来说没有意义,那么是否应该禁止它而不是使用一个标志来检查其有效性?

I was reading Setting an int to Infinity in C++. I understand that when one needs true infinity, one is supposed to use numeric_limits<float>::infinity(); I guess the rationale behind it is that usually integral types have no values designated for representing special states like NaN, Inf, etc. like IEEE 754 floats do (again C++ doesn't mandate neither - int & float used are left to the implementation); but still it's misleading that max > infinity for a given type. I'm trying to understand the rationale behind this call in the standard. If having infinity doesn't make sense for a type, then shouldn't it be disallowed instead of having a flag to be checked for its validity?

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

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

发布评论

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

评论(4

温柔一刀 2025-01-18 19:33:11

函数 numeric_limits::infinity() 对于 numeric_limits::has_infinity 返回 T 有意义正确。

如果是 T=int,则返回 false。因此这种比较没有意义,因为 numeric_limits::infinity() 不会返回任何有意义的值进行比较。

The function numeric_limits<T>::infinity() makes sense for those T for which numeric_limits<T>::has_infinity returns true.

In case of T=int, it returns false. So that comparison doesn't make sense, because numeric_limits<int>::infinity() does not return any meaningful value to compare with.

长安忆 2025-01-18 19:33:11

如果您阅读此参考,您将看到一个表格,显示整数类型的无穷大为零。这是因为根据定义,C++ 中的整数类型不可能是无限的。

If you read e.g. this reference you will see a table showing infinity to be zero for integer types. That's because integer types in C++ can't, by definition, be infinite.

请帮我爱他 2025-01-18 19:33:11

相反,假设标准确实保留了一些值来表示无穷大,并且numeric_limits::infinity() > numeric_limits::max()。这意味着 int 的某个值会大于 max(),即 int 的某个可表示值大于int 的最大可表示值。

显然,无论标准指定哪种方式,都会违反一些自然理解。要么 inifinity() <= max(),或者存在 x 使得 int(x) > >最大()。标准必须选择要违反哪条自然规则。

我相信他们的选择是明智的。

Suppose, conversely, the standard did reserve some value to represent inifity, and that numeric_limits<int>::infinity() > numeric_limits<int>::max(). That means that there would be some value of int which is greater than max(), that is, some representable value of int is greater than the greatest representable value of int.

Clearly, whichever way the Standard specifies, some natural understanding is violated. Either inifinity() <= max(), or there exists x such that int(x) > max(). The Standard must choose which rule of nature to violate.

I believe they chose wisely.

不即不离 2025-01-18 19:33:11

numeric_limits::infinity() 返回正无穷大的表示形式(如果可用)。

对于整数,不存在正无穷大:

cout << "int has infinity: " << numeric_limits<int>::has_infinity << endl;

打印

int has infinity: false

numeric_limits<int>::infinity() returns the representation of positive infinity, if available.

In case of integers, positive infinity does not exists:

cout << "int has infinity: " << numeric_limits<int>::has_infinity << endl;

prints

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