Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 621
legends2k Avatar asked Dec 13 '12 14:12

legends2k


People also ask

What does numeric_limits do in C++?

The std::numeric_limits ::digits function is used to find the number of radix digits that the data type can represent without loss of precision.

How do you convert an integer to infinity in C++?

Setting an int Infinity: The nearby value that we can get is by initializing an “int” to its extreme value. The closest we can get by setting a variable to the maximum value that is double “a = std: numeric_limits<int>:: max();”. Which would be 231-1 if it is 32 bits wide on our implementation.

What is numeric_limits int min ()?

numeric_limits::minReturns the minimum finite value representable by the numeric type T . For floating-point types with denormalization, min returns the minimum positive normalized value. Note that this behavior may be unexpected, especially when compared to the behavior of min for integral types.


1 Answers

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.

like image 172
Nawaz Avatar answered Oct 13 '22 17:10

Nawaz