• Home
  • 1.20 Numeric Limits

1.20 Numeric Limits

View Categories

1.20 Numeric Limits

1 min read

Table of Contents

Each fundamental data type can store values only within a specific range. The <limits> header provides information about these ranges through the std::numeric_limits class template.

This example demonstrates how to determine the minimum and maximum values that can be stored by commonly used numeric data types.

Source Code #

#include <iostream>
#include <limits>

int main()
{
    std::cout << "int\n";
    std::cout << "Minimum: " << std::numeric_limits<int>::min() << '\n';
    std::cout << "Maximum: " << std::numeric_limits<int>::max() << "\n\n";

    std::cout << "float\n";
    std::cout << "Minimum: " << std::numeric_limits<float>::lowest() << '\n';
    std::cout << "Maximum: " << std::numeric_limits<float>::max() << '\n';

    return 0;
}

Output #

int
Minimum: -2147483648
Maximum: 2147483647

float
Minimum: -3.40282e+38
Maximum: 3.40282e+38

The reported values are implementation-dependent and may vary depending on the compiler and target platform.

Explanation #

  • #include <limits> includes the Standard Library header that defines the std::numeric_limits class template.
  • std::numeric_limits<int>::min() returns the smallest value that can be represented by the int type.
  • std::numeric_limits<int>::max() returns the largest value that can be represented by the int type.
  • std::numeric_limits<float>::lowest() returns the lowest finite value that can be represented by the float type.
  • std::numeric_limits<float>::max() returns the largest finite value that can be represented by the float type.
  • std::numeric_limits provides similar information for most fundamental arithmetic types, including char, short, long, double, and bool.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *