Pi and e In Binary
Copyright © 2008-2013 Exploring Binary
Some people are curious about the binary representations of the mathematical constants pi and e. Mathematically, they’re like every other irrational number — infinite strings of 0s and 1s (with no discernible pattern). In a computer, they’re finite, making them only approximations to their true values. I will show you what their approximations look like in five different levels of binary floating-point precision.
Binary Floating-Point Formats
In binary floating-point, infinitely precise values are rounded to finite precision. Here’s how rounding works in five different levels of precision:
- In half-precision, values are rounded to 11 significant bits.
- In single-precision, values are rounded to 24 significant bits.
- In double-precision, values are rounded to 53 significant bits.
- In extended-precision, values are rounded to 64 significant bits.
- In quadruple-precision, values are rounded to 113 significant bits.
The rounding rule used most often in practice is round-to-nearest, round-half-to-even; that’s the rule I will use. For pi and e, there are no “half to even” cases, since their binary expansions are infinite. This makes the rounding rule simple: if the rounding bit is 0, round down; if the rounding bit is 1, round up.
I will show the correctly rounded approximations of pi and e in these five formats.
Here are the first 50 decimal digits of pi:
Here are the first 128 bits of pi:
Here are the 128 bits again, with the rounding bit for each level of precision highlighted (bits 12, 25, 54, 65, and 114):
pi = 1.1001001 x 21 = 0x1.92p+1
This equals 3.140625 in decimal (all binary floating-point numbers have exact decimal representations), which approximates pi accurately to about 4 decimal digits.
(You can verify this conversion by hand, by adding the powers of two corresponding to the positions of the 1 bits: 11.001001 = 21 + 20 + 2-3 + 2-6 = 3.140625.)
pi = 1.10010010000111111011011 x 21 = 0x1.921fb6p+1
This equals 3.1415927410125732421875, which approximates pi accurately to about 8 decimal digits.
pi = 1.1001001000011111101101010100010001000010110100011 x 21 = 0x1.921fb54442d18p+1
This equals 3.141592653589793115997963468544185161590576171875, which approximates pi accurately to about 16 decimal digits.
pi = 1.100100100001111110110101010001000100001011010001100001000110 101 x 21 = 0x1.921fb54442d1846ap+1
which approximates pi accurately to about 20 decimal digits.
pi = 1.100100100001111110110101010001000100001011010001100001000110 1001100010011000110011000101000101110000000110111 x 21 = 0x1.921fb54442d18469898cc51701b8p+1
which approximates pi accurately to about 35 decimal digits.
Euler’s Number (e)
Here are the first 50 decimal digits of e:
Here are the first 128 bits of e:
Here are the 128 bits again, with the rounding bits for each level of precision highlighted:
Here are the correctly rounded values of e in each of the five levels of precision:
e = 1.010111 x 21 = 0x1.5cp+1
This equals 2.71875, which approximates e accurately to about 4 decimal digits.
e = 1.010110111111000010101 x 21 = 0x1.5bf0a8p+1
This equals 2.71828174591064453125, which approximates e accurately to about 8 decimal digits.
e = 1.0101101111110000101010001011000101000101011101101001 x 21 = 0x1.5bf0a8b145769p+1
This equals 2.718281828459045090795598298427648842334747314453125, which approximates e accurately to about 16 decimal digits.
e = 1.010110111111000010101000101100010100010101110110100101010011 011 x 21 = 0x1.5bf0a8b145769536p+1
which approximates e accurately to about 20 decimal digits.
e = 1.010110111111000010101000101100010100010101110110100101010011 010101011111101110001010110001000000010011100111101 x 21 = 0x1.5bf0a8b1457695355fb8ac404e7ap+1
which approximates e accurately to about 34 decimal digits.