在 C++ 中将 int 设置为无穷大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8690567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting an int to Infinity in C++
提问by Daniel Gratzer
I have an int a
that needs to be equal to "infinity". This means that if
我有一个int a
需要等于“无穷大”的。这意味着如果
int b = anyValue;
a>b
is always true.
a>b
永远是真的。
Is there any feature of C++ that could make this possible?
C++ 有什么特性可以使这成为可能吗?
采纳答案by Daniel Gratzer
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:
整数是有限的,所以遗憾的是你不能将它设置为真正的无穷大。但是,您可以将其设置为 int 的最大值,这意味着它将大于或等于任何其他 int,即:
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
这通常等于 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.
注意:正如 WTP 所提到的,如果绝对有必要拥有一个“无限”的 int,则必须为 int 编写一个包装类并重载比较运算符,尽管这对于大多数项目来说可能不是必需的。
回答by Etienne de Martel
Integers are inherently finite. The closest you can get is by setting a
to int
's maximum value:
整数本质上是有限的。您可以获得的最接近的是通过设置a
为int
的最大值:
#include <limits>
// ...
int a = std::numeric_limits<int>::max();
Which would be 2^31 - 1
(or 2 147 483 647
) if int
is 32 bits wide on your implementation.
这将是2^31 - 1
(或2 147 483 647
)如果int
是32个位宽您的实现。
If you reallyneed infinity, use a floating point number type, like float
or double
. You can then get infinity with:
如果您确实需要无穷大,请使用浮点数类型,例如float
或double
。然后你可以得到无穷大:
double a = std::numeric_limits<double>::infinity();
回答by bdonlan
int
is inherently finite; there's no value that satisfies your requirements.
int
本质上是有限的;没有满足您要求的值。
If you're willing to change the type of b
, though, you can do this with operator overrides:
但是,如果您愿意更改 的类型b
,则可以使用运算符覆盖来执行此操作:
class infinitytype {};
template<typename T>
bool operator>(const T &, const infinitytype &) {
return false;
}
template<typename T>
bool operator<(const T &, const infinitytype &) {
return true;
}
bool operator<(const infinitytype &, const infinitytype &) {
return false;
}
bool operator>(const infinitytype &, const infinitytype &) {
return false;
}
// add operator==, operator!=, operator>=, operator<=...
int main() {
std::cout << ( INT_MAX < infinitytype() ); // true
}
回答by Wilmer E. Henao
This is a message for me in the future:
这是对我未来的信息:
Just use: (unsigned)!((int)0)
只需使用: (unsigned)!((int)0)
It creates the largest possible number in any machine by assigning all bits to 1s (ones) and then casts it to unsigned
它通过将所有位分配为 1(一个),然后将其强制转换为无符号数,从而在任何机器中创建最大可能的数字
Even better
甚至更好
#define INF (unsigned)!((int)0)
And then just use INF in your code
然后在你的代码中使用 INF
回答by nicodjimenez
You can also use INT_MAX:
您还可以使用 INT_MAX:
http://www.cplusplus.com/reference/climits/
http://www.cplusplus.com/reference/climits/
it's equivalent to using numeric_limits.
它相当于使用 numeric_limits。
回答by Shaun07776
int min and max values
int 最小值和最大值
Int -2,147,483,648 / 2,147,483,647 Int 64 -9,223,372,036,854,775,808 / 9,223,372,036,854,775,807
国际 -2,147,483,648 / 2,147,483,647 国际 64 -9,223,372,036,854,775,808 / 9,223,372,036,854,775,807
i guess you could set a to equal 9,223,372,036,854,775,807 but it would need to be an int64
我想你可以将 a 设置为等于 9,223,372,036,854,775,807 但它需要是一个 int64
if you always want a to be grater that b why do you need to check it? just set it to be true always
如果你总是希望 a 更磨碎 b 为什么你需要检查它?只需将其设置为始终为真