In this guide, we will explore the functions std::numericlimits::max and std::numericlimits::min, including their syntax and usage examples.
What is std::numeric_limits::max?
The std::numeric_limits<T>:: max function provides the highest finite value that can be stored using the numeric data type T. This method is compatible with all numerical data types for T.
Header files:
#include<limits>
Template:
static T max() throw();
static constexpr T max() noexcept;
Syntax:
It has the following syntax:
std::numeric_limits<T>::max
Argument: It has the ability to accept a variety of data types, like T.
Depending on the data type T, the function will return either predefined macros, the boolean value true, or the default value of T.
Program 1:
The subsequent program showcases the function std::numeric_limits<T>::max.
Filename: MaxLimits.cpp
// program to implement the numeric std--numeric_limits--max() function
#include <limits>
#include <iostream>
using namespace std;
int main()
{
cout << "bool limit: "
<< numeric_limits<bool>::max()
<< '\n';
//It prints the integer value of type cast
cout << "char limit: "
<< int(numeric_limits<char>::max())
<< '\n';
cout << "unsigned char limit: "
<< int(numeric_limits<unsigned char>::max())
<< '\n';
cout << "short limit: "
<< numeric_limits<short>::max()
<< '\n';
cout << "int: " << numeric_limits<int>::max()
<< '\n';
cout << "unsigned int limit: "
<< numeric_limits<unsigned int>::max()
<< '\n';
cout << "long long limit: "
<< numeric_limits<long long>::max()
<< '\n';
cout << "float limit: "
<< numeric_limits<float>::max()
<< '\n';
cout << "double limit: "
<< numeric_limits<double>::max()
<< '\n';
cout << "size_t limit: "
<< numeric_limits<size_t>::max()
<< '\n';
}
Output:
bool limit: 1
char limit: 127
unsigned char limit: 255
short limit: 32767
int: 2147483647
unsigned int limit: 4294967295
long long limit: 9223372036854775807
float limit: 3.40282e+38
double limit: 1.79769e+308size_t limit: 18446744073709551615
What is std::numeric_limits::min?
The std::numeric_limits<T>::min function provides the minimum finite value that can be represented by the numeric data type T. Any bounded arithmetic type is acceptable for type T.
Header File:
#include<limits>
Syntax:
It has the following syntax:
std::numeric_limits<T>::min
Argument: It has the capability to accept various forms of data, like T.
Depending on the type T, this function returns predefined macros, true, or the default value T. The minimum result is the smallest positive normalized value for floating-point number types with denormalization. When working with floating-point data types, you can utilize numeric_limits::lowest to determine the value that does not have any smaller values than it.
Example:
// Program to implement the std--numeric_limits--min() in C++
#include <iostream>
#include <limits>
using namespace std;
int main()
{
cout << "bool min value: "
<< numeric_limits<bool>::min()
<< '\n';
// numeric_limits<char>:: min() // results in 127 in ASCII in an integer that may be typecast to int()
cout << "char min value: "
<< int(numeric_limits<char>::min())
<< '\n';
cout << "unsigned char min value: "
<< int(numeric_limits<unsigned char>::min())
<< '\n';
cout << "short min value: "
<< numeric_limits<short>::min() << '\n';
cout << "int min value: " << std::numeric_limits<int>::min()
<< '\n';
cout << "unsigned int min value: "
<< numeric_limits<unsigned int>::min()
<< '\n';
cout << "long long min value: "
<< numeric_limits<long long>::min()
<< '\n';
cout << "float: "
<< numeric_limits<float>::min()
<< '\n';
cout << "double min value: "
<< numeric_limits<double>::min()
<< '\n';
cout << "size_t min value: "
<< numeric_limits<size_t>::min()
<< '\n';
}
Output:
bool min value: 0
char min value: -128
unsigned char min value: 0
short min value: -32768
int min value: -2147483648
unsigned int min value: 0
long long min value: -9223372036854775808
float: 1.17549e-38
double min value: 2.22507e-308
size_t min value: 0