std::numeric_limits is a C++ standard library class that contains traits and information about the built-in numeric types, such as the maximum and minimum representable values, number of digits, and whether it is a signed type. Access to this class is provided via the header
Questions tagged [numeric-limits]
117 questions
255
votes
6 answers
Why is 0 < -0x80000000?
I have below a simple program:
#include
#define INT32_MIN (-0x80000000)
int main(void)
{
long long bal = 0;
if(bal < INT32_MIN )
{
printf("Failed!!!");
}
else
{
printf("Success!!!");
…

Jayesh Bhoi
- 24,694
- 15
- 58
- 73
217
votes
7 answers
maximum value of int
Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue function in java?

d3vdpro
- 2,887
- 4
- 25
- 29
60
votes
6 answers
warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits::max();
This line works correctly in a small test program, but in the program for which I want it, I get the following compiler complaints:
#include
x = std::numeric_limits::max();
c:\...\x.cpp(192) : warning C4003: not enough actual…

Harvey
- 2,062
- 2
- 21
- 38
54
votes
2 answers
Syntax error with std::numeric_limits::max
I have class struct definition as follows:
#include
struct heapStatsFilters
{
heapStatsFilters(size_t minValue_ = 0, size_t maxValue_ = std::numeric_limits::max())
{
minMax[0] = minValue_; minMax[1] = maxValue_;
…

mmostajab
- 1,937
- 1
- 16
- 37
50
votes
3 answers
Negative infinity
I'm trying to figure out how to assign the value of negative infinity to a float or double variable. It seems that including the standard library limits, I can get the infinity representation, and I know (quite surely) that adding a minus in front…

Setzer22
- 1,589
- 2
- 13
- 29
49
votes
6 answers
Is it possible to read infinity or NaN values using input streams?
I have some input to be read by a input filestream (for example):
-365.269511 -0.356123 -Inf 0.000000
When I use std::ifstream mystream; to read from the file to some
double d1 = -1, d2 = -1, d3 = -1, d4 = -1;
(assume mystream has already been…

Drise
- 4,310
- 5
- 41
- 66
42
votes
2 answers
‘numeric_limits’ was not declared in this scope, no matching function for call to ‘max()’
I compiled this code at home on my mac w/ xcode and there was no provblem. I compile it at school with g++ on linux and I get these errors:
numeric_limits’ is not a member of std
expected primary-expression before ‘>’ token
no matching function for…

Matt Munson
- 2,903
- 5
- 33
- 52
42
votes
2 answers
Why does numeric_limits::min return a negative value for int but positive values for float/double?
Why does numeric_limits::min return a negative value for int, but positive values for e.g. float and double?
#include
#include
using namespace std;
int main() {
cout << "int: " << numeric_limits::min() << " "
<<…

gnzlbg
- 7,135
- 5
- 53
- 106
37
votes
12 answers
Why is the maximum value of an unsigned n-bit integer 2ⁿ-1 and not 2ⁿ?
The maximum value of an n-bit integer is 2n-1. Why do we have the "minus 1"? Why isn't the maximum just 2n?

Ugdu Shan
- 395
- 1
- 3
- 3
31
votes
1 answer
Why is std::numeric_limits::max() a function?
In the C++ Standard Library the value std::numeric_limits::max() is specified as a function. Further properties of a specific type are given as constants (likestd::numeric_limits::is_signed). All constants that are of type T are given as…

ablaeul
- 2,750
- 20
- 22
31
votes
4 answers
Why is FLT_MIN equal to zero?
limits.h specifies limits for non-floating point math types, e.g. INT_MIN and INT_MAX. These values are the most negative and most positive values that you can represent using an int.
In float.h, there are definitions for FLT_MIN and FLT_MAX. If you…

Nick Forge
- 21,344
- 7
- 55
- 78
30
votes
5 answers
Using numeric_limits::max() in constant expressions
I would like to define inside a class a constant which value is the maximum possible int. Something like this:
class A
{
...
static const int ERROR_VALUE = std::numeric_limits::max();
...
}
This declaration fails to compile with…

FireAphis
- 6,650
- 8
- 42
- 63
20
votes
2 answers
When a float variable goes out of the float limits, what happens?
I remarked two things:
std::numeric_limits::max()+(a small number) gives: std::numeric_limits::max().
std::numeric_limits::max()+(a large number like: std::numeric_limits::max()/3) gives inf.
Why this difference? Does 1…

WildThing
- 665
- 2
- 9
- 17
19
votes
1 answer
C++: Why does numeric_limits work on types it does not know?
I have created my own type, without any comparator, and without a specialization of std::numeric_limits. Despite that, for some reason, std::numeric_limits compiles fine. Why did the c++ standards committee define the numeric_limits template…

tohava
- 5,344
- 1
- 25
- 47
18
votes
2 answers
Where are the limits for Qt types?
Regularly, I could reference limits.h to see what the max is for a certain type, like an int or long.
In Qt, there are types like qlonglong. Is there a header file and/or documentation that can be used in a similar way to manually or…

Cory Klein
- 51,188
- 43
- 183
- 243