Self-Assessment Quiz: Floating-Point Arithmetic
Twenty questions to confirm you can reason about precision, rounding, and error before you build on this foundation in the rest of Part V. Aim for 16 or more. Answers and a topic map are at the end — try the whole quiz first, and predict every "what prints?" before checking.
Question 1
How are the 64 bits of an IEEE double divided? - A. 1 sign, 8 exponent, 55 significand - B. 1 sign, 11 exponent, 52 stored fraction (53 with the hidden bit) - C. 1 sign, 15 exponent, 48 significand - D. 32 exponent, 32 significand
Question 2
Machine epsilon for real(dp), returned by epsilon(1.0_dp), is approximately:
- A. $1.2 \times 10^{-7}$
- B. $2.2 \times 10^{-16}$
- C. $1.8 \times 10^{308}$
- D. $2.2 \times 10^{-308}$
Question 3
Why is 0.1_dp + 0.2_dp == 0.3_dp false?
- A. Fortran rounds all comparisons down
- B. 0.1, 0.2, 0.3 are repeating binary fractions; the rounded sum lands one ULP off the rounded 0.3
- C. == is not defined for reals in Fortran
- D. The compiler optimized the comparison away
Question 4
Roughly how many significant decimal digits does double precision guarantee (precision(1.0_dp))?
- A. 6
- B. 10
- C. 15
- D. 34
Question 5
spacing(x) (one ULP) as x grows in magnitude:
- A. stays constant
- B. shrinks
- C. grows, in proportion to the magnitude of x
- D. becomes negative
Question 6
Catastrophic cancellation occurs when you: - A. multiply two very large numbers - B. subtract two nearly equal numbers, losing their shared leading digits - C. add zero to a number - D. divide by a number close to 1
Question 7
What does 0.0_dp / 0.0_dp produce?
- A. 0.0
- B. Inf
- C. a NaN
- D. a compile-time error
Question 8
Which expression correctly tests whether x is a NaN?
- A. x == 0.0_dp
- B. x /= x
- C. x < tiny(x)
- D. abs(x) > huge(x)
Question 9
Floating-point addition is: - A. associative but not commutative - B. commutative but not associative - C. both associative and commutative - D. neither
Question 10
huge(1.0_dp) is approximately:
- A. $1.8 \times 10^{308}$
- B. $2.2 \times 10^{-16}$
- C. $3.4 \times 10^{38}$
- D. $1.0 \times 10^{100}$
Question 11
The best cure for catastrophic cancellation is usually to:
- A. round more carefully
- B. rearrange the algebra so the near-equal subtraction never happens
- C. use == with a tolerance
- D. cast to integer
Question 12
Conditioning is a property of the __, and stability is a property of the ____: - A. algorithm; problem - B. problem; algorithm - C. compiler; hardware - D. hardware; compiler
Question 13
Which module provides ieee_is_nan and ieee_is_finite?
- A. iso_c_binding
- B. iso_fortran_env
- C. ieee_arithmetic
- D. omp_lib
Question 14
ieee_is_nan(1.0_dp / 0.0_dp) returns:
- A. .true. — division by zero is a NaN
- B. .false. — 1.0/0.0 is Inf, which is not a NaN
- C. a compile error
- D. Inf
Question 15
The unit roundoff $u$, the maximum relative error of a single rounded operation, equals:
- A. epsilon(1.0_dp)
- B. epsilon(1.0_dp) / 2
- C. epsilon(1.0_dp) * 2
- D. tiny(1.0_dp)
Question 16
Which compiler flag makes a 0.0/0.0 or overflow stop the program at its source?
- A. -O3
- B. -Wall
- C. -ffpe-trap=invalid,zero,overflow
- D. -fcoarray=single
Question 17
(1.0e17_dp + 1.0_dp) - 1.0e17_dp evaluates to:
- A. 1.0
- B. 0.0, because 1.0 is absorbed by the ULP gap at $10^{17}$
- C. 1.0e17
- D. a NaN
Question 18
Quadruple precision (selected_real_kind(33, 4931)) on a typical CPU is:
- A. faster than double
- B. the same speed as double
- C. usually software-emulated and much slower than double
- D. unavailable in Fortran
Question 19
For a long time-stepping simulation, single precision is dangerous mainly because:
- A. it cannot represent negative numbers
- B. its rounding error accumulates over many dependent steps, eroding the answer
- C. it has no NaN
- D. it uses more memory than double
Question 20
Python's built-in float compared with Fortran's real(dp):
- A. is a completely different, higher-precision format
- B. is the same IEEE 754 binary64 — identical bits, different default printing
- C. is single precision
- D. cannot represent 0.1 at all, unlike Fortran
Answer Key
| Q | Ans | Why |
|---|---|---|
| 1 | B | 1 + 11 + 52 stored (53-bit significand with the hidden leading bit). |
| 2 | B | $\varepsilon_{\text{mach}} = 2^{-52} \approx 2.22\times10^{-16}$. |
| 3 | B | All three are repeating binary fractions; the rounded sum is one ULP from the rounded 0.3. |
| 4 | C | precision(1.0_dp) returns 15 guaranteed decimal digits. |
| 5 | C | Spacing (ULP) scales with magnitude — the grid coarsens for larger numbers. |
| 6 | B | Cancellation is loss of leading digits when subtracting near-equal values. |
| 7 | C | $0/0$ is an undefined form → NaN. |
| 8 | B | A NaN is the only value not equal to itself. |
| 9 | B | Commutative, but reordering a sum can change the result (non-associative). |
| 10 | A | Largest finite double $\approx 1.8\times10^{308}$. |
| 11 | B | Rearrange the math to remove the near-equal subtraction. |
| 12 | B | Conditioning = the problem's sensitivity; stability = the algorithm's added error. |
| 13 | C | The intrinsic ieee_arithmetic module. |
| 14 | B | 1.0/0.0 is +Inf, which is finite-valued's opposite but not a NaN. |
| 15 | B | Round-to-nearest is off by at most half a gap, so $u = \varepsilon_{\text{mach}}/2 = 2^{-53}$. |
| 16 | C | -ffpe-trap turns floating-point exceptions into an immediate crash with a backtrace. |
| 17 | B | At $10^{17}$ one ULP is 16, so 1.0 is absorbed; the sum stays $10^{17}$, then cancels to 0. |
| 18 | C | Quad is typically emulated in software (libquadmath) and much slower. |
| 19 | B | Single's ~7 digits erode as round-off accumulates over many dependent steps. |
| 20 | B | Python float and NumPy float64 are IEEE binary64, the same as real(dp). |
Topics to review by question
- Q1–5, 10, 15 → §20.1–20.2 (representation, epsilon, ULP, the precision intrinsics).
- Q6, 9, 11, 17 → §20.3 (catastrophic cancellation and non-associativity).
- Q7, 8, 13, 14, 16 → §20.4 (NaN, Inf,
ieee_arithmetic,-ffpe-trap). - Q12 → §20.5 (conditioning vs stability).
- Q18, 19 → §20.6 (choosing precision).
- Q3, 20 → §20.1 and the Python Comparison (floating point is a hardware standard, shared with Python).
Scored below 16? Reread the flagged sections before Chapter 21 — linear algebra leans on every idea here, especially conditioning (Q12) and precision (Q18–19).