How do I best represent the number $\pi$ in binary?

I have been thinking about it for sometime and, if it was me, I would use four bits for each digit, because four bits are sufficient to represent 10-based digits. Something like:

0011 , 0001 0100 0001 ...

I wouldn't know, also, how to represent the decimal point here.

I hope my question is clear enough.


I will put this into context. I didn't want to, because I thought you would find it stupid.

I want to make a full back tattoo, representing pi in binary so, what I am looking for is a way that will be more or less simple to scale, as I will want thousands of digits, and I want it to be precise.

  • 5
    $\begingroup$ Pray tell me, how do you represent 8 and 9 with 3 bits? I would just write $\pi$ in base 2: $\pi=11.0010\ldots=2+1+2^{-3}+\cdots$ $\endgroup$ – Jyrki Lahtonen Apr 23 '12 at 10:18
  • 11
    $\begingroup$ @André: What you are doing is not a genuine binary representation of $\pi$. You are just taking the decimal representation and then encoding every digit into binary. I don't know what you want to do with that. Might be exactly what you need for your purposes, but it's not a binary representation of $\pi$. What Jyrki is showing is the true binary representation of $\pi$. $\endgroup$ – Raskolnikov Apr 23 '12 at 10:26
  • 1
    $\begingroup$ In base 2 $5=101_2$ because $5=1\cdot2^2+0\cdot2^1+1\cdot2^0$. Notice how the exponents indicate the position of the bit. We just continue with negative exponents. For example, $$1011.11_2=2^3+2^1+2^0+2^{-1}+2^{-2}=11\frac34.$$ $\endgroup$ – Jyrki Lahtonen Apr 23 '12 at 10:30
  • 1
    $\begingroup$ About the decimal point (which I guess you could call the 'binary point' in base 2): as you can see from the comments and answers, we just represent it the same, and it marks the transition point from non-negative powers of $n$ to negative powers of $n$, for a number written in base $n$. $\endgroup$ – Tara B Apr 23 '12 at 10:43
  • 1
    $\begingroup$ @J.M. Why have you removed the (binary) tag? The word binary currently occurs 17 times in the title, the question body, both answers and several comments. If someone searches for (binary)-related questions they may be interested in this one. $\endgroup$ – Peter Phipps Apr 23 '12 at 19:48

What you're suggesting is known as binary coded decimal representation. It is used by some simple calculators, and used to be commonly used in financial computing, because it can represent dollar amounts without rounding the cents (and also makes it easier to produce decimal output for human consumption). But it is harder to calculate with than true binary representation, which for $\pi$ is


as found here. Here each bit after the point represents successively $1/2$, $1/4$, $1/8$, $1/16$, ..., $2^{-n}$, ..., so according to this representation $$ \pi = 2+1+1/8+1/64+1/2048+1/4096+\cdots$$

In practical computing, irrational numbers are nowadays almost always represented in binary, rounded to a fixed number of significant binary digits and then represented in a binary variant of scientific notation. There's a widely-used standard for how to do this, IEEE-754, which allows different programs or systems to exchange floating-point values without first converting them to decimal notation. (A recent update of the standard defines more format, but is not as widely adopted).

  • $\begingroup$ thank you. can you please see my update in the question? $\endgroup$ – André Alçada Padez Apr 23 '12 at 10:59
  • $\begingroup$ The link Henning included in his answer (the one that says 'here') should give you all the digits you could want! $\endgroup$ – Tara B Apr 23 '12 at 11:01
  • $\begingroup$ @André: The link marked "here" gives you 32768 bits. There are probably millions more to find if you google for them. $\endgroup$ – hmakholm left over Monica Apr 23 '12 at 11:02
  • $\begingroup$ thank you, that is really what i was looking for $\endgroup$ – André Alçada Padez Apr 23 '12 at 11:05
  • 1
    $\begingroup$ @André: Well, funnily enough that page is the first result when I google 'pi in binary'. Did you not think of trying that? $\endgroup$ – Tara B Apr 23 '12 at 11:10

Representing any real number in binary (or any base) is just the same principle as representing numbers in decimal.

For example, in decimal $\frac{25}{8} = 3.125 = 3\cdot 10^0 + 1\cdot 10^{-1} + 2\cdot 10^{-2} + 5\cdot 10^{-3}$.

Now to write the same number in binary, first express it as a sum of powers of $2$ (this is exactly what you do when writing natural numbers in binary):

$\frac{25}{8} = 2 + 1 + \frac{1}{8} = 2\cdot 2^1 + 1\cdot 2^0 + 0\cdot 2^{-1} + 0\cdot 2^{-2} + 1\cdot 2^{-3} = 11.001_2.$

  • $\begingroup$ Sorry about the typos. I had a different example at first but then decided to keep it really simple, but then didn't change everything. $\endgroup$ – Tara B Apr 23 '12 at 10:38
  • $\begingroup$ thank you. can you please see my update in the question? $\endgroup$ – André Alçada Padez Apr 23 '12 at 10:59

Any series of bytes can be represented in binary. You might as well encode Pi in bytes first, and then represent those bytes in binary.

If you use a "pure" binary format like described above, you'd need to write "," which is not a valid binary digit.

File formats (series of bytes) are standardized unlike ad-hoc improvisations of binary encoding.

So what I'd suggest is: get hold of the B-file (a standardized format) of OEIS A000796 (B-file available here). Now you have your bytes. Now convert the bytes into an ASCII representation of binary digits.

Python 3:

>>> import urllib.request
>>> bin(int.from_bytes(urllib.request.urlopen("http://oeis.org/A000796/b000796.txt").read(),byteorder='big'))

Beware that the generated string (from bin) will start 0b.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.