CS253: Software Development with C++

Spring 2021

Limits

Show Lecture.Limits as a slide show.

CS253 Limits

C integer 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.

Integer Example

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

Floating-Point Examples

using fl = numeric_limits<float>; // a type alias
cout << fl::digits       << '\n'
     << fl::digits10     << '\n'
     << fl::min_exponent << '\n'
     << fl::max_exponent << '\n'
     << fl::epsilon()    << '\n'
     << fl::lowest()     << '\n'
     << fl::min()        << '\n'
     << fl::max()        << '\n';
24
6
-125
128
1.19209e-07
-3.40282e+38
1.17549e-38
3.40282e+38
numeric_limits<long double> ld; // an instance
cout << ld.digits       << '\n'
     << ld.digits10     << '\n'
     << ld.min_exponent << '\n'
     << ld.max_exponent << '\n'
     << ld.epsilon()    << '\n'
     << ld.lowest()     << '\n'
     << ld.min()        << '\n'
     << ld.max()        << '\n';
64
18
-16381
16384
1.0842e-19
-1.18973e+4932
3.3621e-4932
1.18973e+4932

Aliases work, too

size_t is an alias for an unsigned integer type. Which one? We don’t care. Since size_t is an alias for another type, it works with numeric_limits:

cout << boolalpha
     << numeric_limits<size_t>::is_signed  << '\n'
     << numeric_limits<size_t>::is_integer << '\n'
     << numeric_limits<size_t>::digits     << '\n'
     << numeric_limits<size_t>::digits10   << '\n'
     << numeric_limits<size_t>::is_signed  << '\n'
     << numeric_limits<size_t>::is_integer << '\n'
     << numeric_limits<size_t>::min()      << '\n'
     << numeric_limits<size_t>::max()      << '\n';
false
true
64
19
false
true
0
18446744073709551615

boolalpha says to display bool values as false and true, not 0 and 1.

Types

cout << numeric_limits<long>::max()  << '\n'
     << numeric_limits<int>::max()   << '\n'
     << numeric_limits<short>::max() << '\n'
     << numeric_limits<char>::max()  << '\n';
9223372036854775807
2147483647
32767
␡

A Mystery


Holmes & Watson in Zurich
cout << numeric_limits<size_t>::min   << '\n'
     << numeric_limits<size_t>::min() << '\n';
1
0
Why did that compile? One line must be wrong!
  • The smallest value for size_t, an unsigned type, is 0. The second line is correct: .min() is a function.
  • How does the first line compile?
  • A function name is a pointer to the function, just like an array name is a pointer to its first element.
  • A pointer-to-method can’t be displayed with <<; but bool can.
  • A built-in conversion from any pointer to bool exists.
  • The pointer is non-null, so converts to true, which displays as 1, without boolalpha.