Show Lecture.Limits as a slide show.
CS253 Limits
C integral types
<limits.h>
, alias <climits>
Symbol | Meaning |
CHAR_BIT | char : number of bits |
SCHAR_MIN …SCHAR_MAX | signed char : range |
UCHAR_MAX | unsigned char : max value |
CHAR_MIN …CHAR_MAX | char : range |
SHRT_MIN …SHRT_MAX | short int : range |
USHRT_MAX | unsigned short int : max value |
INT_MIN …INT_MAX | int : range |
UINT_MAX | unsigned int : max value |
LONG_MIN …LONG_MAX | long int : range |
ULONG_MAX | unsigned long int : max value |
LLONG_MIN …LLONG_MAX | long long int : range |
ULLONG_MAX | unsigned long long int : max value |
C floating-point types
<float.h>
, alias <cfloat>
Symbol | Meaning |
FLT_DIG | float : mantissa decimal digits |
DBL_DIG | double : mantissa decimal digits |
LDBL_DIG | long double : mantissa decimal digits |
FLT_MIN_10_EXP …FLT_MAX_10_EXP | float : exponent range |
DBL_MIN_10_EXP …DBL_MAX_10_EXP | double exponent range |
LDBL_MIN_10_EXP …LDBL_MAX_10_EXP | long double : exponent range |
FLT_MIN …FLT_MAX | float : value range |
DBL_MIN …DBL_MAX | double : value range |
LDBL_MIN …LDBL_MAX | long double : value range |
Pop Quiz
Quick, now:
- What symbol says how many decimal digits are in the exponent of a
long double
?
- What symbol gives the maximum value of a
short
?
- What symbol gives the maximum value of a
size_t
?
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:
.min()
.max()
.digits10
.min_exponent10
.max_exponent10
- It’s unclear why
.min()
& .max()
are methods,
not variables like .min_exponent10
.
- Surely, these values are known at compile-time.
- In fact, they’re all
constexpr
methods & constants. Fine.
Integral Example
cout << numeric_limits<int>::digits10 << '\n'
<< numeric_limits<int>::min() << '\n'
<< numeric_limits<int>::max() << '\n';
9
-2147483648
2147483647
- Note the
::
. We’d use .
if we had an instance of this class.
digits10
is a constant, not a method.
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