在 C++ 中将 int 设置为 Infinity

发布于 12-23 17:59 字数 159 浏览 4 评论 0原文

我有一个 int a 需要等于“无穷大”。这意味着如果

int b = anyValue;

a>b 始终为 true。

C++ 是否有任何功能可以使这成为可能?

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?

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

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

发布评论

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

评论(6

咽泪装欢2024-12-30 17:59:32

整数本质上是有限的。您可以获得的最接近的结果是将 a 设置为 int 的最大值:

#include <limits>

// ...

int a = std::numeric_limits<int>::max();

这将是 2^31 - 1 (或 2 147 483 647) 如果 int 在您的实现中是 32 位宽。

如果您确实需要无穷大,请使用浮点数类型,例如floatdouble。然后你可以通过以下方式获得无穷大:

double a = std::numeric_limits<double>::infinity();

Integers are inherently finite. The closest you can get is by setting a to int's maximum value:

#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.

If you really need infinity, use a floating point number type, like float or double. You can then get infinity with:

double a = std::numeric_limits<double>::infinity();
肥爪爪2024-12-30 17:59:32

整数是有限的,所以遗憾的是你不能将它设置为真正的无穷大。
但是,您可以将其设置为 int 的最大值,这意味着它将大于或等于任何其他 int,即:

a>=b

始终为 true。

您可以通过以下方式执行此操作

#include <limits>

//your code here

int a = std::numeric_limits<int>::max();

//go off and lead a happy and productive life

:This will通常等于2,147,483,647

如果您确实需要一个真正的“无限”值,则必须使用双精度或浮点数。然后您可以简单地执行此操作。

float a = std::numeric_limits<float>::infinity();

可以在此处找到数字限制的其他说明,

祝您编码愉快!

注意:正如 WTP 提到的,如果绝对需要有一个“无限”的 int,则必须为 int 编写一个包装类并重载比较运算符,尽管这对于大多数项目来说可能不是必需的。

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.

£噩梦荏苒2024-12-30 17:59:32

这是给我的未来消息:

只需使用:(unsigned)!((int)0)

它通过将所有位分配给 1(个)来创建任何机器中最大可能的数字,然后将其转换为 unsigned

更好

#define INF (unsigned)!((int)0)

然后在代码中使用 INF

This is a message for me in the future:

Just use: (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

Even better

#define INF (unsigned)!((int)0)

And then just use INF in your code

意中人2024-12-30 17:59:32

int 本质上是有限的;没有任何值可以满足您的要求。

不过,如果您愿意更改 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
}

int is inherently finite; there's no value that satisfies your requirements.

If you're willing to change the type of b, though, you can do this with operator overrides:

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
}
请持续率性2024-12-30 17:59:32

您还可以使用 INT_MAX:

http://www.cplusplus.com/reference/climits/

它相当于使用 numeric_limits。

You can also use INT_MAX:

http://www.cplusplus.com/reference/climits/

it's equivalent to using numeric_limits.

心头的小情儿2024-12-30 17:59:32

我认为宏 INFINITY 是在标头“"。

它的定义如下,

#define INFINITY ((float)(1e+300 * 1e+300))

这是一个如此大的数字,没有其他数字(至少在 C++ 中)可以大于它。但显然,由于我们要转换为 (int) 类型,该类型最多可以容纳值 2147483647。那么它就会隐式转换为该值。

I think that a macro INFINITY is defined in header "<cmath>".

It is defined as follows,

#define INFINITY ((float)(1e+300 * 1e+300))

This is such a large number that no other number (at least in c++) can be greater than it. But obviously since we are converting to a type (int), which at max can hold a value 2147483647. So then it would implicitly convert to that value.

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