CS253: Software Development with C++

Fall 2018

Limits

See this page as a slide show

CS253 Limits

C integral types

<limits.h>, alias <climits>
SymbolMeaning
CHAR_BITNumber of bits in a char
SCHAR_MINSCHAR_MAXRange of signed char
UCHAR_MAXMax value of unsigned char
CHAR_MINCHAR_MAXRange of char
SHRT_MINSHRT_MAXRange of short int
USHRT_MAXMax value of unsigned short int
INT_MININT_MAXRange of int
UINT_MAXMax value of unsigned int
LONG_MINLONG_MAXRange of long int
ULONG_MAXMax value of unsigned long int
LLONG_MINLLONG_MAXRange of long long int
ULLONG_MAXMax value of unsigned long long int

C floating-point types

<float.h>, alias <cfloat>
SymbolMeaning
FLT_DIGMantissa decimal digits (float)
DBL_DIGMantissa decimal digits (double)
LDBL_DIGMantissa decimal digits (long double)
FLT_MIN_10_EXPFLT_MAX_10_EXPExponent range (float)
DBL_MIN_10_EXPDBL_MAX_10_EXPExponent range (double)
LDBL_MIN_10_EXPLDBL_MAX_10_EXPExponent range (long double)
FLT_MINFLT_MAXValue range (float)
DBL_MINDBL_MAXValue range (double)
LDBL_MINLDBL_MAXValue range (long double)

Pop Quiz

Quick, now:

C++ way

<limits> defines the template class numeric_limits, which is specialized for all built-in types. It has many static methods and constants, including:

It is not clear why .min() and .max() are not constants. Surely, these values are known at compile-time. In fact, for C++11, they’re all constexpr. Fine.

Integral Example

cout << numeric_limits<int>::digits10 << ' '
     << numeric_limits<int>::min() << ' '
     << numeric_limits<int>::max() << '\n';
9 -2147483648 2147483647

Floating-Point Examples

typedef numeric_limits<float> nl;
cout << nl::digits10 << ' ' << nl::min() << ' ' << nl::max() << '\n';
6 1.17549e-38 3.40282e+38
using nl = numeric_limits<double>;
cout << nl::digits10 << ' ' << nl::min() << ' ' << nl::max() << '\n';
15 2.22507e-308 1.79769e+308
auto nl = numeric_limits<long double>();    // an instance
cout << nl.digits10 << ' ' << nl.min() << ' ' << nl.max() << '\n';
18 3.3621e-4932 1.18973e+4932

A Mystery

Why do these both compile? One must be wrong!

cout << numeric_limits<unsigned short>::min   << '\n';
cout << numeric_limits<unsigned short>::min() << '\n';
1
0

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-06-10T16:53

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building