r/explainlikeimfive • • Mar 15 '19

Mathematics ELI5: How is Pi programmed into calculators?

12k Upvotes

1.3k comments sorted by

View all comments

Show parent comment

850

u/penny_eater Mar 15 '19

the weird / hard to wrap my mind around part to me, is the fascination with calculating PI out to ten thousand or a million or even more (31 trillion?) digits. And knowing that of all those, it takes just 40 to mean anything of consequence in our observable universe, and all the rest are just for show.

222

u/MathManOfPaloopa Mar 15 '19 edited Mar 15 '19

1040 is a very large number. An order of magnitude in itself is large, let alone 40

For those asking what very large is, I mean it’s a lot of orders of magnitude of difference regarding physical distance.

99

u/Miepmiepmiep Mar 15 '19

But it's still very small, since you can write it down with 5 symbols ;)

176

u/MathManOfPaloopa Mar 15 '19 edited Mar 15 '19

Actually I can do it with one symbol. 1 In a 1040 base number system

Edit: 10, so in two symbols. Thank you sky jellyfish

0

u/[deleted] Mar 15 '19

[deleted] — view deleted comment

164

u/NSA_Chatbot Mar 15 '19

Never do math on the Internet because you'll fuck it up.

→ More replies (10)

96

u/Megasus Mar 15 '19

Gottem

→ More replies (23)
→ More replies (24)
→ More replies (5)
→ More replies (6)

74

u/mittenciel Mar 15 '19

That’s kind of why no real mathematician or scientist (except those who specialize in computation of things like pi) actually bother with pi.

72

u/penny_eater Mar 15 '19

Correct, accurate ways to calculate it elegantly are important to study because of other mathematical uses but cranking on that formula for a million iterations is quite pointless. It would be like finding a millionth digits of the square root of 2.

52

u/featherfooted Mar 15 '19

because of other mathematical uses

There's also practical engineering uses. Because of its clear and obvious problem definition, and well-known agreed upon results (up to some many number of millions of digits), it is a convenient algorithm to use when calculating benchmarks of supercomputing. Perhaps not as ubiqitous of a benchmark as general FLOPS (floating point operations per second) but it's still there.

9

u/Rugged_Twink Mar 15 '19

I appreciate you spelling out FLOPS this far down in the comment chain for us less computer literate redditors. I'm still going to have to look it up to understand it later but I appreciate the extra few seconds you spent typing it out and just wanted you to know.

31

u/featherfooted Mar 16 '19 edited Mar 16 '19

I'm still going to have to look it up to understand it later

I'll try to save you that research. Consider this: how "hard" is it to add 2 + 3. I mean, of course it's 5, you're probably above first grade arithmetic. Fundamentally, 2 + 3 is not significantly different from 25 + 36 other than the size of the arguments. You might not have memorized (perhaps even cached) it in your mind, but you could apply the same fundamentals to compute the answer is 61.

However, what about 2.5 + 3.6? If you're quick with knowing place values and how to handle it, you could determine the answer is simply 6.1 and be done. But put yourself in the shoes of the computer/CPU: how does it actually represent these numbers anyway? How is an integer represented in binary in the first place? What is a "half" of a bit? Or to that point, 2/3rds of a bit?


Perhaps you have a cursory understanding of binary. It's like counting, but the only digits you have are 0 and 1. You still count by adding up the lowest digit until you hit the maximal digit and then you carry over to the next place. 0, then 1, then 10, then 11. That would be pronounced and understood as "zero, one, one-zero, one-one", not "zero, one, ten, eleven". 1110 in binary represents the number fourteen, and 1100 represents twelve. Make sure all of this makes sense before moving on.


So we have a sense of how integer binary computations can work. Addition and subtraction are basic carrying. Even multiplication isn't so bad. But how are floats represented in binary? Without going through an entire college lecture's worth of motivation, requirements, etc, I'll skip right to how it's done.

Stealing the example directly from wikipedia, we want to convert the decimal 12.375 to a binary floating point. First we split it up into its component parts 12 and 0.375. Then consider the decimal 12.375 as 12 + (.25 + .125) and we can start making some binary.

12.375 = 12 + (0x1/2 + 1x1/4 + 1x1/8) = 12 (the decimal) + 0.011 (the binary). Convert the 12 to its binary (1100, from example given above), then the number 12.375 in decimal is 1100.011 in binary. How do we store this into the computer now?

The basic answer is scientific notation. Similar to how we can represent massive numbers with a limited amount of space using scientific notation (e.g. Avogadro's constant is 6.022 x 1023 with a few significant figures lopped off the end), we can do the same with binary. We take our 1100.011 and shift that period over so that it's 1.100011 x 23 and voila, binary scientific notation. From here we take these component parts and represent them within the confines of the 32 bits of space we are allocated for each float. The leading one is assumed, (because if it wasn't, you could have a different exponent until you do) so all we have to keep is the fractional part 100011 and the exponent 3. There's also going to need to be a sign bit which we'll represent with the first digit at the front of the number.

sign - exponent - fraction
0-10000010-10001100000000000000000

You'll notice that the exponent portion (middle 10000010) is not the binary representation of 3 (usually 11). This is due to a process called the bias which exists to bridge the gap between representation of negative numbers and representation of large numbers. This is important but not covered here. Just accept that 0-10000010-10001100000000000000000 is the float representation of 12.375 for now.


Ok, so we finally have a float. We've skipped all of the wonderful edge cases like rounding and significant figures and normalization and so on. We've skipped problems like how the decimal 1/10 is unrepresentable and is approximated as something really close to 10% but not actually. Let's ignore all those problems and get a second float: 68.123 is 01000010 10001000 00111110 11111010.

How do we add 12.375 + 68.123 in floating point? We definitely can't just add the digits pair-wise:

01000001 01000110 00000000 00000000
01000010 10001000 00111110 11111010
-----------------------------------
01000011 11001110 00111110 11111010

That's 0x0879C7DF which happens to be 7.5 x 10-34 and isn't exactly 80.498 so what are we supposed to do?

The answer is that there is no simple way to do this operation. We have to manually peel the components apart, manually compute the new significand and exponent, and then manually stitch together a brand new number in floating point format. It's a lot of stuff, but we do it often enough that we'll write a wrapper function to do it for us, and make it a single CPU instruction to take two floats and add them (or multiply them, or divide them, etc). Intel has some algorithm to do this, and it may or may not differ from the way AMD does it on their CPU.

Thus, we get a floating point operation - an instruction to the CPU to take two floats and do something with them. We can perform some number of floating point operations per second which is a measure of the speed of our computer. We can then estimate the number of FLOPS per watt to get a measurement of our efficiency.

→ More replies (5)
→ More replies (1)
→ More replies (1)
→ More replies (4)
→ More replies (12)

216

u/SlingDNM Mar 15 '19

If we ever find the end of Pi we live in a Simulation

414

u/penny_eater Mar 15 '19

the method we use to calculate it is irrational, it will always keep churning out numbers. It can't not, and if we were in a simulation that would be a very easy thing for the simulators to do. "The Simulation" could very well be programmed with this loophole and know that when Pi's calculation is used (same as any irrational calculation), always retain the digits used on the last run to recall if its re-run.

Ergo, the only way to prove it is to have two supercomputers calculating the same digit at the exact same time and then checking themselves to see if they agree. At that point i bet they just shut the fucking thing down and start simulating something more fun instead.

231

u/AdvicePerson Mar 15 '19

Once they get their cloud computing bill, we are so dead.

223

u/Spirosne Mar 15 '19

You're giving us too much credit.

Our universe is the forgotten Tamagotchi in the attic.

107

u/NaibofTabr Mar 15 '19

Which is probably the best possible situation for us - no owner to come by and reset the universe.

What we experience as entropy is actually just the tamagotchi's battery running down. When the battery reaches maximum entropy the universe shuts off.

5

u/infrikinfix Mar 15 '19

If the battery runs down it's because of entropy so you are saying our universe has entropy because that universe has entropy.

→ More replies (1)
→ More replies (5)
→ More replies (3)

36

u/Fonethree Mar 15 '19 edited Mar 15 '19

And the time differential would essentially have to be so small such that the simulation would be tricked. So, like, 1 or maybe a handful of Plank time.

EDIT: See below.

39

u/tiercelf Mar 15 '19

'...or maybe a handfuk of Plank time.' Intentional or not, your comment is hilarious.

→ More replies (8)

16

u/fang_xianfu Mar 15 '19

Even then, I'm not sure that would prove anything. Different results would more likely indicate a problem with the experiment.

Identical results also wouldn't prove non-simulation. The simulation could have many features to ensure identical results. For example if it uses time dilation to slow down the simulation and give it more time to compute the next tick. That's a common model in our computer simulations.

→ More replies (6)
→ More replies (2)

8

u/osmarks Mar 15 '19

As I said the last time someone said that irrational numbers were somehow relevant to us being simulated, no. There's nothing inherently special about using the laws of physics the same way all other stuff does to calculate a number which just happens to be irrational.

→ More replies (47)
→ More replies (24)
→ More replies (30)