Lab 2 · Lecture 2 · Floating-Point Numbers
Floating-Point Lab
See the bits inside a number, measure machine epsilon, and watch errors accumulate.
Type any number (you can also type fractions such as 1/3). The sign, exponent and fraction bits update instantly, with the exact decimal value that is stored.
Try this: 0.7 (is the stored value above or below?), 1e22 and 1e23 (one is exact), 1/3 in single vs double, 2^53 + 1 = 9007199254740993.
A toy floating-point system with only a few bits. Move x and see which number it is stored as. Change the number of fraction bits.
Decimal to binary
Try this
Machine epsilon
Try this
float vs double
Try this
Q1 Convert to binary.
, , , . So (exact).
Q2 Write in normalised decimal floating-point form. How many significant digits?
; 4 significant digits (the trailing zero after 7 is significant, the leading zeros are not).
Q3 Represent with 4-digit chopping and 4-digit rounding. Give the absolute and relative error of each.
Chopping: , error , relative . Rounding: , error , relative .
Q4 Give the IEEE 754 single-precision bit pattern of .
. Sign 0, exponent , fraction . Bits: 0 10000001 01110000000000000000000 = 0x40B80000.
Q5 What is machine epsilon, and why can an iteration never meet the tolerance in double precision?
is the gap between 1 and the next double. Neighbouring doubles near differ by about , so two different iterates near 1 always differ by at least .
Q6 Why is if (x == 0.3) dangerous after computing x = 0.1 + 0.2? What should be written?
Both sides carry different rounding errors (0.30000000000000004 vs 0.29999999999999998890), so the test is false. Write
fabs(x - 0.3) < 1e-12, or a relative test fabs(x - y) <= 1e-12 * fabs(y).