Quiz: floating point

Short questions to test your understanding. Try each on paper first, then open the answer.


Q1. Why is 0.1 not exactly representable as a float, while 0.5 and 0.375 are?

A float is a fraction with a power of two in the denominator. 0.5 = 1/2 and 0.375 = 3/8 are such fractions; 0.1 = 1/10 is not (10 has the factor 5), so its binary expansion 0.000110011\ldots never ends, just as 1/3 = 0.333\ldots never ends in decimal. It is rounded to the nearest float.


Q2. In float16, the gap between neighbouring numbers is 0.5 near 1000. What is it near 2000, near 4000, and near 0.001?

The gap doubles at every power of two: 1000 is in [512, 1024).

2000 is in [1024, 2048): gap 1. 4000 is in [2048, 4096): gap 2. 0.001 is in [2^{-10}, 2^{-9}), where the gap is 2^{-10} \cdot 2^{-10} = 2^{-20} \approx 9.5 \times 10^{-7}. The relative gap is always about 0.05–0.1\,\%.


Q3. A 16-bit camera image (values up to 65 535) is converted to half floats to save GPU memory. What goes wrong?

Half floats are exact for integers only up to 2048; above that, values are rounded to multiples of 2, 4, …, 32, so the low bits of bright pixels are lost. The largest half float is 65 504, so the brightest values (above 65 519) become infinity. Use float32, or a 16-bit integer or normalised format.


Q4. You compute the variance of a bright, nearly flat image as “mean of the squares minus the square of the mean” in float32 and get a negative number. How is that possible?

Both terms are large (around \bar x^2) and nearly equal; their difference, the variance, is tiny compared with them. Each term carries a rounding error that is larger than the variance itself, so the difference can come out with either sign: cancellation. Compute the mean first, then average the squared deviations from it.


Q5. One pixel of an image is NaN. You smooth with a 3 × 3 mean filter, then compute the mean of the whole image. What do you get?

After filtering, the 9 pixels whose neighbourhood contains the NaN are NaN; the image mean is then NaN, because any sum containing NaN is NaN. Find and treat NaNs (mask them, or replace them) before filtering.


Q6. Why does adding up the pixels of a large image in float32 give a different answer depending on the order in which you add them?

Every addition is rounded, and the rounding depends on the size of the partial sum. Float addition is therefore not associative: (a + b) + c and a + (b + c) can differ. Adding left to right makes the running sum large early, so later pixels lose their low bits; adding in pairs (a tree) keeps the partial sums small and the error much smaller.


Q7. (math) Show that in a float format with m fraction bits, every integer up to 2^{m+1} is exactly representable, but 2^{m+1} + 1 is not.

An integer n with 2^k \le n < 2^{k+1} needs the significand n/2^k = 1.f with f a multiple of 2^{-k}, i.e. k fraction bits. With m fraction bits that works for every k \le m, so all integers below 2^{m+1} are exact, and 2^{m+1} itself is a power of two. In [2^{m+1}, 2^{m+2}) the spacing is 2^{m+1-m} = 2, so the odd number 2^{m+1} + 1 falls between two floats.