Summary
This lesson closes the second unit of the course, devoted to data and their types. It reconstructs the chain of concepts that runs from the variable to the constant that the compiler guards, passing through the binary representation of integers and their two overflows, approximate reals and tolerance, characters as codes and the conversions that lose information; it separates what of all this holds in any language from what is specific to C++; it gathers the undefined behavior of the unit, with what the compiler detects, what the sanitizer detects and what nobody detects; it repeats in its exit assessment the skills of the entry assessment; and it ends with six challenges that combine the whole unit.
Learning objectives
- Choose and justify the type of each datum in a problem according to the values it can take, the accuracy it requires and the operations that will be performed on it.
- Predict the effects of representation limits in a given program: integer overflow, floating-point rounding and the loss of information in a conversion.
- Use named constants and explicit conversions, and explain what the compiler checks in each case.
- Recognize the undefined behavior of the unit and state, for each case, what detects it and how it is prevented.
- Separate, in what was learned in the unit, what holds in any language from what is specific to C++.
What the unit achieved
The unit set out to teach that every datum lives in memory with a type that fixes which values it can take, how much space it occupies and which operations it admits, and that these limits have observable consequences. It has achieved this relying only on what the introduction declared as taken for granted: the cycle of compiling and running with the course options, formatted output, reading with std::cin and reading compiler messages. The program of the four calculations with which the introduction posed the problem can now be explained line by line, and corrected.
Variables and memory
The lesson Variables and memory established that a variable is a memory cell with a name, a fixed type and a value that changes, and that the type fixes its size, which sizeof measures. It distinguished initialization from assignment, showed that braces reject a conversion that loses information, and taught that assignment copies and does not bind, and that the replaced value is lost, from which follows the auxiliary variable of the swap. It defined the state of a program as the set of the values of its variables and taught how to follow it with a trace table. It fixed the naming conventions and closed with the first undefined behavior of the unit: reading a variable that never received a value.
Integers and their representation
The lesson Integers and their representation taught positional notation in bases 10, 2 and 16. From the fact that a type has bits it deduced its range: patterns, which unsigned represent from to and signed, in two’s complement, from to , with one more negative than positives. It distinguished the two overflows: that of unsigned integers, which is arithmetic modulo and is defined (0u - 1 is ), and that of signed integers, which is undefined behavior and which the sanitizer stops. It showed that comparing a signed integer with an unsigned one converts the former, so that is false, and ended with a criterion for choosing the integer type according to the foreseeable range of the datum.
Floating-point numbers
The lesson Floating-point numbers showed that a real is stored as a sign, an exponent and a binary significand, so that the representable values are a finite set of rationals whose denominator is a power of 2. From this it followed that does not exist in the machine: in its place the nearest double is stored, and 0.1 + 0.2 is written 0.30000000000000004. The lesson bounded the error of each rounding, showed that errors accumulate and that addition stops being associative; from which it deduced that two computed reals are not compared with ==, but by their distance, with a justified tolerance. It fixed double as the default real type, and presented infinity and the not-a-number value, NaN.
Characters, Booleans, conversions and constants
The lesson Characters, Booleans, conversions and constants showed that a type is a rule for reading bits: a char stores a byte whose value is the ASCII code of the character, and since digits and letters have consecutive codes, '7' - '0' is 7; a bool is true or false, and any nonzero number converts to true. It studied implicit conversions and established that the conversion happens inside the operation, according to the type of the operands, and not when the result is stored: double r{7 / 2} is 3. It taught how to write the intended conversion with static_cast, which does not avoid the loss, but declares it; it made clear that braces reject narrowing, and showed the third undefined behavior of the unit, the conversion to integer of a real that does not fit. It closed with constants (const, constexpr and static_assert), the type of literals and auto.
Loading the content…
Completed
Sign in to save your progress