为什么是numeric_limits< int> :: max()> numeric_limits< int> :: infinity()? [英] Why is numeric_limits<int>::max() > numeric_limits<int>::infinity()?

查看:345
本文介绍了为什么是numeric_limits< int> :: max()> numeric_limits< int> :: infinity()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读在C ++中设置int为Infinity 。我理解,当一个需要真正的无穷大,一个应该使用 numeric_limits< float> :: infinity();我想其背后的理由是,通常整数类型没有指定用于表示像IEEE 754浮点数的特殊状态(例如, Inf t任务既不 - int & float used都留给实现)但仍然误导 max>无穷大。我试图理解标准中这个调用背后的理由。如果 infinity 对一个类型没有意义,那么它不应该被禁止,而不是有一个标志被检查其有效性?

解决方案

函数 numeric_limits< T> :: infinity() $ c> T 为 numeric_limits< T> :: has_infinity 返回 true / p>

如果 T = int ,则返回 false 。所以比较没有意义,因为 numeric_limits< int> :: infinity()不会返回任何有意义的值。 / p>

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?

解决方案

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.

这篇关于为什么是numeric_limits&lt; int&gt; :: max()&gt; numeric_limits&lt; int&gt; :: infinity()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆