Pi and e In Binary
Copyright © 2008-2016 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.
Addendum (July 2016)
I wanted to add a new perspective following the publishing of my articles “Number of Digits Required For Round-Trip Conversions” and “Decimal Precision of Binary Floating-Point Numbers”.
For starters, I was inconsistent in how I evaluated decimal precision for the ten conversions. Using the definition that decimal precision is the maximum number of matching digits after rounding, here are the new results (changes in bold):
|Format||pi (Digits)||e (Digits)|
The four that changed all have one less digit of precision than I had stated.
Furthermore, I’d now like to consider the representations of pi and e in the context of the precision of their formats. For each binary precision format you can compute a range of equivalent decimal precisions:
|Format||Decimal Precision (Digits)|
Those values represent the range of decimal precision over the whole format; individual segments of the range will have their own unique precision. For example, in the segment [21, 22), which includes both pi and e, the precision is:
|Format||Decimal Precision (Digits)|
You can see that some of the conversions of pi and e are more accurate than their segment allows. For example, the single-precision value of pi is accurate to 8 digits, but only 7 digits of precision are provided in the segment in which it resides. I call this coincidental precision. pi’s proximity to a floating-point number — along with some beneficial rounding — makes its precision look greater than that for numbers in its segment in general.