std::numeric_limits::max() and std::numeric_limits::min() in C++
Last Updated :
01 Feb, 2021
std::numeric_limits::max():
The std::numeric_limits<T>::max() function is used to get the maximum finite value representable by the numeric type T. All arithmetic types are valid for type T.
Header File:
#include<limits>
Template:
static T max() throw();
static constexpr T max() noexcept;
Syntax:
std::numeric_limits<T>::max
Parameter: It receives any one type of data type i.e., T.
Return Type: It returns predefined macros, true or default T() depending on type T. Some standard return values are:
Type T |
std::numeric_limits<T>::max() |
/* non-specialized */ |
T() |
bool |
TRUE |
char |
CHAR_MAX |
signed char |
SCHAR_MAX |
unsigned char |
UCHAR_MAX |
wchar_t |
WCHAR_MAX |
short |
SHRT_MAX |
unsigned short |
USHRT_MAX |
int |
INT_MAX |
unsigned int |
UINT_MAX |
long |
LONG_MAX |
unsigned long |
ULONG_MAX |
long long |
LLONG_MAX |
unsigned long long |
ULLONG_MAX |
float |
FLT_MAX |
double |
DBL_MAX |
long double |
LDBL_MAX |
Program 1:
Below is the program to illustrate the function std::numeric_limits<T>::max(). The output of the program is system-specific.
C++
#include <iostream>
#include <limits>
using namespace std;
int main()
{
cout << "bool: "
<< numeric_limits< bool >::max()
<< '\n' ;
cout << "char: "
<< int (numeric_limits< char >::max())
<< '\n' ;
cout << "unsigned char: "
<< int (numeric_limits<unsigned char >::max())
<< '\n' ;
cout << "short: "
<< numeric_limits< short >::max()
<< '\n' ;
cout << "int: " << numeric_limits< int >::max()
<< '\n' ;
cout << "unsigned int: "
<< numeric_limits<unsigned int >::max()
<< '\n' ;
cout << "long long: "
<< numeric_limits< long long >::max()
<< '\n' ;
cout << "float: "
<< numeric_limits< float >::max()
<< '\n' ;
cout << "double: "
<< numeric_limits< double >::max()
<< '\n' ;
cout << "size_t: "
<< numeric_limits< size_t >::max()
<< '\n' ;
}
|
Output:
bool: 1
char: 127
unsigned char: 255
short: 32767
int: 2147483647
unsigned int: 4294967295
long long: 9223372036854775807
float: 3.40282e+38
double: 1.79769e+308
size_t: 18446744073709551615
std::numeric_limits::min():
The std::numeric_limits<T>::min() function is used to get the minimum finite value representable by the numeric type T. All bounded arithmetic types are valid for type T.
Header File:
#include<limits>
Template:
static T min() throw();
static constexpr T min() noexcept;
Syntax:
std::numeric_limits<T>::min
Parameter: It receives any one type of data type i.e., T.
Return Type: It returns predefined macros, true or default T() depending on type T. For floating-point types with denormalization, min returns the minimum positive normalized value. To find the value that has no values less than it for floating data type, use numeric_limits::lowest(). Some standard return values are:
Type T |
std::numeric_limits::min() |
/* non-specialized */ |
T() |
bool |
FALSE |
char |
CHAR_MIN |
signed char |
SCHAR_MIN |
unsigned char |
​0​ |
wchar_t |
WCHAR_MIN |
short |
SHRT_MIN |
unsigned short |
0 |
int |
INT_MIN |
unsigned int |
0 |
long |
LONG_MIN |
unsigned long |
0 |
long long |
LLONG_MIN |
unsigned long long |
​0​ |
float |
FLT_MIN |
double |
DBL_MIN |
long double |
LDBL_MIN |
Program 2:
Below is the program to illustrate the function std::numeric_limits<T>::min(). The output of the program is system-specific.
C++
#include <iostream>
#include <limits>
using namespace std;
int main()
{
cout << "bool: "
<< numeric_limits< bool >::min()
<< '\n' ;
cout << "char: "
<< int (numeric_limits< char >::min())
<< '\n' ;
cout << "unsigned char: "
<< int (numeric_limits<unsigned char >::min())
<< '\n' ;
cout << "short: "
<< numeric_limits< short >::min() << '\n' ;
cout << "int: " << std::numeric_limits< int >::min()
<< '\n' ;
cout << "unsigned int: "
<< numeric_limits<unsigned int >::min()
<< '\n' ;
cout << "long long: "
<< numeric_limits< long long >::min()
<< '\n' ;
cout << "float: "
<< numeric_limits< float >::min()
<< '\n' ;
cout << "double: "
<< numeric_limits< double >::min()
<< '\n' ;
cout << "size_t: "
<< numeric_limits< size_t >::min()
<< '\n' ;
}
|
Output:
bool: 0
char: -128
unsigned char: 0
short: -32768
int: -2147483648
unsigned int: 0
long long: -9223372036854775808
float: 1.17549e-38
double: 2.22507e-308
size_t: 0
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...