CS253: Software Development with C++

Spring 2020

Limits

Show Lecture.Limits as a slide show.

CS253 Limits

C integral types

<limits.h>, alias <climits>

SymbolMeaning
CHAR_BITchar: number of bits
SCHAR_MINSCHAR_MAXsigned char: range
UCHAR_MAXunsigned char: max value
CHAR_MINCHAR_MAXchar: range
SHRT_MINSHRT_MAXshort int: range
USHRT_MAXunsigned short int: max value
INT_MININT_MAXint: range
UINT_MAXunsigned int: max value
LONG_MINLONG_MAXlong int: range
ULONG_MAXunsigned long int: max value
LLONG_MINLLONG_MAX   long long int: range
ULLONG_MAXunsigned long long int: max value

C floating-point types

<float.h>, alias <cfloat>

SymbolMeaning
FLT_DIGfloat: mantissa decimal digits
DBL_DIGdouble: mantissa decimal digits
LDBL_DIGlong double: mantissa decimal digits
FLT_MIN_10_EXPFLT_MAX_10_EXPfloat: exponent range
DBL_MIN_10_EXPDBL_MAX_10_EXPdouble exponent range
LDBL_MIN_10_EXPLDBL_MAX_10_EXP  long double: exponent range
FLT_MINFLT_MAXfloat: value range
DBL_MINDBL_MAXdouble: value range
LDBL_MINLDBL_MAXlong double: value range

Pop Quiz

Quick, now:

C++ way

Simple Example

What is the range of values for a char?

constexpr int smallest = numeric_limits<char>::min();
constexpr int largest = numeric_limits<char>::max();
cout << "A plain char value ranges from "
     << smallest << " to " << largest << ".\n";
A plain char value ranges from -128 to 127.

Integral Example

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

Floating-Point Examples

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

A Mystery

Why do these both compile? One must be wrong!

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

Holmes & Watson in Zurich