Friday, July 23, 2010

Numeric Limits in C++

You can use the "limits" class template to obtain machine specific numeric limits. Here's a sample code:
#include <iostream>
#include <limits>

using namespace std;
int main()
{
    //print maximum of various types
    cout << "Maximum values :\n";
    cout << "------------------\n";
    cout << "short : " << numeric_limits<short>::max() << endl;
    cout << "int : " << numeric_limits<int>::max() << endl;
    cout << "long : " << numeric_limits<long>::max() << endl;
    cout << "float : " << numeric_limits<float>::max() << endl;
    cout << "double : " << numeric_limits<double>::max() << endl;

    //print minimum of various types
    cout << "\n";
    cout << "Minimum Values: \n";
    cout << "------------------\n";
    cout << "short : " << numeric_limits<short>::min() << endl;
    cout << "int : " << numeric_limits<int>::min() << endl;
    cout << "long : " << numeric_limits<long>::min() << endl;
    cout << "float : " << numeric_limits<float>::min() << endl;
    cout << "double : " << numeric_limits<double>::min() << endl;
    //
    return 0;
}

Cite as:
Saad, T. "Numeric Limits in C++". Weblog entry from Please Make A Note. http://pleasemakeanote.blogspot.com/2010/07/numeric-limits-in-c.html

No comments:

Post a Comment