Why is numeric_limits<int>::max() > numeric_limits<int>::infinity()?
Asked Answered
C

4

30

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?

Coyle answered 13/12, 2012 at 14:11 Comment(9)
Are you sure that numeric_limits<int>::has_infinity is true? If it's not, then the standard doesn't require infinity() to be meaningful.Premaxilla
en.cppreference.com/w/cpp/types/numeric_limits/has_infinityCostmary
Yep, like I mentioned in the question, one cannot represent infinity using an integer value because of its inherent nature, but still a Joe coder not knowing has_infinity() trying out the above line will shoot his foot nice and square; it seems unintuitive.Coyle
Most answers say the same thing mentioned in the question, but not the actual rationale; say why did the standards committee, instead of giving a has_infinity(), didn't opt for not defining infinity() for such types?Coyle
@legends2k: It is not has_infinity(), it is just has_infinity. It is not a function.Russi
@legends2k: I agree with you. If this function doesn't return any meaningful value, then it should not exist, or at least require the implementation to reject the code on using this function. That is not that difficult. With the help of some metaprograming, the implementation could detect meaningless usage. But then I think such techniques was discovered later.Russi
@Nawaz Oh yes, just noticed that has_infinity isn't a function, thanks! And yes again, one could've avoided it with SFINAE.Coyle
“Integral types are finite”, as are floating-point types—floats trade precision for range, and merely have additional special representations for positive and negative infinity, signed zero, and NaN.Wrench
@JonPurdy Agreed, fixed it in the question, thanks!Coyle
R
37

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.

Russi answered 13/12, 2012 at 14:16 Comment(2)
In fact, the standard requires non-meaningful values to be 0 or false.Nephritis
Why are we even allowed to make comparisons that don't make sense? Why not fail at compile time if someone tries to use numeric_limits<int>::infinity()?Quenelle
L
6

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.

Leaf answered 13/12, 2012 at 14:17 Comment(0)
C
5

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.

Courbet answered 13/12, 2012 at 14:41 Comment(3)
There is a third option: require the implementation to reject the code which uses numeric_limits<int>::infinity() - which is possible with a bit of metaprogramming.Russi
@Courbet This seems to be right; although I'd say, like Nawaz suggested, this could've been avoided for types which don't have an infinity representation using SFINAE, but I guess these functions were defined earlier than meta-programming's advent.Coyle
Also this shows that the language projects what the machine does as is, which, in my opinion, isn't great because later suppose some novel machine's architecture does support infinity for integral types, then its has_infinity would be true, and hence it's the programmer who has to add another exception to his mental model. This is exactly what makes C++ expert-friendly and complex for the Joe coder.Coyle
T
0

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
Thoron answered 13/12, 2012 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.