CS253: Software Development with C++

Spring 2023

Limits

Show Lecture.Limits as a slide show.

CS253 Limits

Inclusion

To do numeric_limits, you need to:

    
#include <limits>

Rare uses of the old C limits #defines requires:

    
#include <climits>
#include <cfloat>

C integer types in <limits.h> or <climits>

TypeRangeTypeRange
signed charSCHAR_MINSCHAR_MAXintINT_MININT_MAX
unsigned char0…UCHAR_MAXunsigned0…UINT_MAX
charCHAR_MINCHAR_MAXlongLONG_MINLONG_MAX
shortSHRT_MINSHRT_MAXunsigned long0…ULONG_MAX
unsigned short0…USHRT_MAXlong longLLONG_MINLLONG_MAX
  unsigned long long0…ULLONG_MAX

Also, CHAR_BIT is how many bits in a char.

C floating-point types in <float.h> or <cfloat>

TypeMantissa digits₁₀Exponent rangeValue range
floatFLT_DIGFLT_MIN_10_EXPFLT_MAX_10_EXPFLT_MINFLT_MAX
doubleDBL_DIGDBL_MIN_10_EXPDBL_MAX_10_EXPDBL_MINDBL_MAX
long doubleLDBL_DIGLDBL_MIN_10_EXPLDBL_MAX_10_EXPLDBL_MINLDBL_MAX

Pop Quiz

Quick, now:

What symbol says how many decimal digits are in the mantissa of a long double?

LDBL_DIG

What symbol gives the maximum value of a short?

SHRT_MAX

What symbol gives the maximum value of a size_t?

There isn’t one. It’s probably ULLONG_MAX, because size_t is often an alias for unsigned long long, but it doesn’t have to be.

C++ way

Simple Example

What is the range of values for a char?

constexpr int smallest = numeric_limits<char>::min(),
              largest  = numeric_limits<char>::max();
cout << "Plain char: " << smallest << "…" << largest;
Plain char: -128…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
constexpr 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>::min()      << '\n'
     << numeric_limits<size_t>::max()      << '\n';
false
true
64
19
0
18446744073709551615

boolalpha is an I/O manipulator. It 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.