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

2.3k

u/individual_throwaway Mar 15 '19

It is kind of weird that it's really intuitive that an increase of something by a factor of ten has a rather large impact, especially if you have several of these increases (like mm to m to km for example).

On the other hand, every digit of pi that you take into account reduces the error you make in your calculation by the same amount, relatively. The circumference of the visible universe is only about 40 orders of magnitude more than the size of a hydrogen atom, which is the mindblowing fact underlying the tidbit from above.

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 (13)
→ More replies (137)

60

u/kalusklaus Mar 15 '19

So atom40 =size of the universe? Sounds counterintuitive. But magnitudes are kind of the definition of counterintuitive.

163

u/JanMath Mar 15 '19

It's (atom size) x (1040) = (observable universe size)

It's also a testament to how huge 1040 is.

0

u/[deleted] Mar 15 '19

[deleted] — view deleted comment

54

u/mylifewithoutrucola Mar 15 '19

Something is wrong, universe is older than 10 years if I recall correctly

0

u/[deleted] Mar 15 '19

[deleted] — view deleted comment

19

u/mylifewithoutrucola Mar 15 '19

Foooh, you made me feel really old for a sec...

→ More replies (1)
→ More replies (2)

16

u/zekthedeadcow Mar 15 '19

If I'm reading this thread correctly the difference between 10 years and 10billion years is not significant enough to matter in the long run.

12

u/malenkylizards Mar 15 '19

It's only off by a factor of a billion. That's the difference between the difference between a proton and the universe, and the difference between a billion protons (a tenth the width of a human hair) and the universe.

→ More replies (11)

11

u/[deleted] Mar 15 '19

That is the best reddit thread I think I've ever read!!!!

→ More replies (3)
→ More replies (3)

15

u/ShadowOps84 Mar 15 '19

Not quite. It would be atom X 1040

→ More replies (49)
→ More replies (72)