问题描述
我有一个 int a
,需要等于infinity。这意味着如果
int b = anyValue;
a> b
/ p>
有没有C ++的特性可以使这成为可能?
整数是有限的,所以可悲的是,你不能把它设置为一个真正的无穷。
然而,你可以将它设置为int的最大值,这意味着它将大于或等于任何其他int,例如:
a> = b
始终为真。
#include< limits>
//你的代码在这里
int a = std :: numeric_limits< int> :: max();
//走出去,过着幸福生活的生活
通常等于2,147,483,647
如果你真的需要一个真正的无限值,你必须使用双精度浮点数。
float a = std :: numeric_limits< float> :: infinity();
有关数字限制的其他说明,请参阅
快乐编码!
:正如WTP所提到的,如果绝对需要有一个无限的int,你必须为int编写一个包装类,并重载比较运算符,尽管这对大多数项目可能不是必需的。
I have an int a
that needs to be equal to "infinity". This means that if
int b = anyValue;
a>b
is always true.
Is there any feature of C++ that could make this possible?
Integers are finite, so sadly you can't have set it to a true infinity.However you can set it to the max value of an int, this would mean that it would be greater or equal to any other int, ie:
a>=b
is always true.
You would do this by
#include <limits>
//your code here
int a = std::numeric_limits<int>::max();
//go off and lead a happy and productive life
This will normally be equal to 2,147,483,647
If you really need a true "infinite" value, you would have to use a double or a float. Then you can simply do this
float a = std::numeric_limits<float>::infinity();
Additional explanations of numeric limits can be found here
Happy Coding!
Note: As WTP mentioned, if it is absolutely necessary to have an int that is "infinite" you would have to write a wrapper class for an int and overload the comparison operators, though this is probably not necessary for most projects.
这篇关于在C ++中将int设置为无穷大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!