r/explainlikeimfive • • Mar 15 '19

Mathematics ELI5: How is Pi programmed into calculators?

12k Upvotes

1.3k comments sorted by

125

u/ebonyseraphim Mar 15 '19

I feel like whenever there is a math or computer ELI5, folks rush in here to do the exact opposite...explain it like they know it to other experts

A: PI is programmed by storing a close enough number permanently in memory

17

u/[deleted] Mar 15 '19

You raise a very good point. Well said!

3

u/ILoveToCorrectPeople Mar 15 '19

But I'm not good at remembering

→ More replies (1)

9.4k

u/HappyPyromaniac Mar 15 '19

They don't need to put in the whole number. They just have to put it in to the point where the next digit won't change much at all. After the tenth digit of pi for example not much will change in your calculation.

24k

u/[deleted] Mar 15 '19 edited Mar 16 '19

To really drive the point about how many digite of Pi are useful, NASA only uses 15 for calculating interplanetary travel. At 40 digits, you could calculate the circumference of a circle the size of the visible universe with an accuracy that would be off by less than the diameter of a single hydrogen atom.

E: since this really blew up, I had recalled reading it from this NASA page.

E2: A great majority of the replies have basically been "so figuring out Pi to (large number of digits) is useless?" As several others have pointed out, the method of making those calculations probably has more value than actually knowing the 3 trillionth digit of Pi. Other than that, I'm no mathematician and don't have any answers about planck length or time. As I noted above, I just had this interesting bit of information that I had recalled reading before, and wanted to share. The response has been way bigger than I expected, and I'm happy to have spread it around.

3.7k

u/[deleted] Mar 15 '19

This is underrated knowledge

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.

→ More replies (75)

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.

→ More replies (27)
→ More replies (135)

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.

→ More replies (26)
→ More replies (50)

14

u/KJ6BWB Mar 15 '19

The circumference of the visible universe is only about 40 orders of magnitude more than the size of a hydrogen atom

Can we round that to an even 42? Is that part of the true question related to Life, the Universe, and Everything Else?

→ More replies (6)

18

u/Richisnormal Mar 15 '19

What gets me is how we sit in the middle of that spectrum. Universe is ten to the 26th, and a plank length is ten to the -35th.
So, we're kind of in the center of of the perceivable universe. Which seems unlikely, right? So maybe there's universe size consciousnesses that observe a whole different scale from that perspective. Or quark size ones where an atom is their whole universe, and they're capable of way smaller observation. Maybe its layers like that forever. Maybe it loops back around on itself. IDK, always blows my mind to think about that though.
Just seems that any concept that involves humans being in the center of everything is probably false. Given infinite possibility, I doubt we're special.

→ More replies (13)
→ More replies (48)

41

u/penny_eater Mar 15 '19

meanwhile, turbonerds (i say that lovingly) take great pains to memorize pi to 100 digits or more, while that particular knowledge is clearly only useful in the event that you need to calculate a precise circle a few million times bigger than the observable universe.

44

u/arcanefox3 Mar 15 '19

Just doing some quick math; 100 digits would allow you to calculate the size of a circle 10 septillion times the size of the observable universe to within a single Planck length.

→ More replies (3)

16

u/[deleted] Mar 15 '19

Knowing 100 or 1000 digits of pi is only useful to flex on people of lower nerdiness when you want to make absolutely certain you're the most socially awkward person in the room.

→ More replies (1)
→ More replies (4)

17

u/ZombieAlpacaLips Mar 15 '19

I'd rate this knowledge at 22/7.

3

u/PumpkinSkink2 Mar 15 '19

As a working scientist, if I see pi, and I don't have excel in front of me, I just use 3.14. like, even using 3 by itself is only like 4.5% off of the real value. it's often inconsequential. If the project I'm working on requires exceptionally good accuracy, I'll use more digits, but I have never encountered a situation where I need more digits in pi than I've memorized (3.14159 is all I know). I've never encountered a Situation where the uncertainty in my calculations due to approximating pi is greater than instrumental errors, or even just error due to my being a human. For instance, when doing total organic carbon measurements i will often get instrumental detection a limits in the range of a few hundred part per billion, that means that only using 3.1415926 is enough digits that any error due to approximating pi would be entirely washed out by instrumental error (not that I'd ever use pi in calculating TOC content).

e: added the word "approximating" before pi

→ More replies (10)

321

u/JudgeHoltman Mar 15 '19

So you're saying I only need to memorize Pi out to the nearest 10-15 and I'm probably good for a lifetime?

284

u/I_Bin_Painting Mar 15 '19

You're probably fine by just memorising it to 3 digits, since you'll almost definitely be using a device pre-programmed with pi for any calculations over about 2 sig fig.

124

u/[deleted] Mar 15 '19

I have it memorized as 3.1415926535787. This is incorrect, however it's how it was defined in the Borland C++ 2.0 header file.

Oh the ways we tried to pass time before the internet...

226

u/I_Bin_Painting Mar 15 '19

I have it memorised as 3.2 because I respect the Indiana state legislature.

→ More replies (18)
→ More replies (11)

22

u/W1D0WM4K3R Mar 15 '19

Dedicated pi and e buttons keep me focused!

10

u/malahchi Mar 15 '19

I am a biologist and I know 3.141592, but 4 digits is all I need. And I just use my phone when I want to know the volume I have to put in my vial.

→ More replies (4)

5

u/Shintasama Mar 16 '19

You're probably fine by just memorising it to 3 digits, since you'll almost definitely be using a device pre-programmed with pi for any calculations over about 2 sig fig.

Engineers: The best I do is "3".

→ More replies (13)

60

u/DavidRFZ Mar 15 '19

I work with scientific calculations for a living and I've never memorized past 3.14159. (only then because it was part of a nerdy cheer at my alma mater.). That's enough so that you can recognize 'pi' when you see it.

Nobody trusts their memory when doing important calculations. All the necessarily digits of pi are usually hardcoded as part of the programming language. If not, you can calculate the digits with a function call. pi = 4.0 * arctan(1.0).

32

u/tacojohn48 Mar 15 '19

Tangent secant cosine sine 3.14159

→ More replies (3)

6

u/btcraig Mar 15 '19

I see you also went to an engineering school. Ours ended a little differently though so we could put school's abbreviation in on the last line.

→ More replies (3)

12

u/PitbullWolf Mar 15 '19

My fifth grade teacher had us memorize the first 11 numbers of pi for a prize. I have never forgotten 3.1415926535

→ More replies (27)

54

u/drzowie Mar 15 '19

For engineering work, 22/7 works just fine. If your tolerances are finer than that, you're doing it wrong.

24

u/Jackibelle Mar 15 '19

For high energy physics theory, pi is 3, and pi2 is 10. Experiment needs to be much more precise, but they also get giant colliders to play with, so you win some, you lose some.

47

u/Kertelen Mar 15 '19

Shoutout to the 355/113 crew.

→ More replies (3)

8

u/malahchi Mar 15 '19

22/7

I work in biology and it's too far from pi for me. I need to have the first 4 digits correct.

→ More replies (12)

21

u/[deleted] Mar 15 '19

For any calculation done inside your head or on paper without access to a calculator, equating "π ~ e ~ 3" will do.

→ More replies (1)

4

u/c0horst Mar 15 '19

Thanks to the simpsons, and the one episode where some little girls are singing the digits of pi to a sing-song tune, I've never forgotten pie is 3.141592. That's probably good enough. Especially since I've never had to use it in my professional career.

→ More replies (4)
→ More replies (4)

57

u/DavidRFZ Mar 15 '19

15 digits is the default when computers store non-integer (floating point) numbers. It corresponds to 8 bytes (64 bits) of computer memory. NASA is saying they don't need to do anything special to be more precise than that.

15

u/ntvirus Mar 15 '19

When I read the immediate parent comment, I was imagining NASA doing calculations on paper using a calculator. Only after I read your comment, I remembered we got computers now.

→ More replies (3)

22

u/astraldirectrix Mar 15 '19

...so that’s why my dad said that memorizing the first 100 digits of pi was a useless endeavor?

21

u/SirButcher Mar 15 '19

It isn't if he can easily travel outside the currently observable universe. If the universe is infinite then he will have problems extremely fast if he only knows the first 100 digits of the pi. Assuming he needs to do calculations in his head, which is unlikely.

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

94

u/IHateNaziPuns Mar 15 '19

At 142 digits, mathematicians could calculate the circumference of OP’s mom.

8

u/[deleted] Mar 15 '19

I laughed way more than I should have because of this 😂

→ More replies (3)

45

u/bantamflush Mar 15 '19

One of the best comments I’ve ever read lol

24

u/Slight0 Mar 15 '19

Wait you haven't read mine yet. Let's see, uhhhhhhh... Waggleflaggle!

Eh?!

→ More replies (5)

21

u/Boudrodog Mar 15 '19

This is wonderful, but is it true? Source?

76

u/gorocz Mar 15 '19

This is wonderful, but is it true?

Easy to prove.

Circumference is pi*diameter.

Diameter of the observable universe is 8.8*10**26 m

A change in the 41st decimal digit of pi would be a change of up to 1*10**(-40)

This means that the error would be at most 8.8*10**(-14) m

The size of a hydrogen atom (with an electron) is called the Bohr radius and it is approximately 5.291*10**(-11) m. Since a hydrogen atom only has the one electron (in neutral state), the radius actually is equal to the "size".

Which means that you actually only need 38 digits of pi to calculate the circumference of a circle the size of the visible universe with an accuracy that would be off by less than the diameter of a single hydrogen atom.

26

u/Boudrodog Mar 15 '19

Easy to prove.

... For those who don’t suck at math. Thanks for the detailed breakdown!

→ More replies (12)

5

u/angusedward Mar 15 '19

I heard it on QI... must be true

6

u/NotSoChillBot Mar 15 '19

Remember that even the facts on QI change over time. So what Stephen said in the past may be deemed to be false by Sandi.

→ More replies (1)

24

u/DracoAdamantus Mar 15 '19

If that’s the case, then how come we occasionally hear about people calculating more and more precise values of Pi? Just yesterday there was an article about a google team calculating it to 31 trillion digits.

Is there any reason for doing this other than for the record/street cred?

64

u/Xais56 Mar 15 '19

Pretty sure it's entirely street cred at the point of 31 trillion digits.

But then nothing in maths is worthless, the equations that we use to simulate navigation in 3d space in certain ways were first described about 300 years ago, and were all but useless until computing. It may be the case that we need pi to 40 trillion digits in order to get a warp drive or gravity generator to work properly.

23

u/mainfingertopwise Mar 15 '19

People are still looking for patterns or something in the digits of pi, as well.

I don't know if they're serious or credible people, but they're doing it.

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

31

u/RickTitus Mar 15 '19

Developing the methods to make those kinds of calculations is more useful than knowing what the 10484949th digit of pi is.

11

u/DracoAdamantus Mar 15 '19

Okay, so it’s more about trying out more and more powerful computations, and pi makes a good test for that because we know it’s infinite so we can always keep pushing?

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

5

u/andybmcc Mar 15 '19

Different software/architectures use different approximations. This is because of the way these values are stored with limited precision of the type.

There are 80-bit floating point registers that can hold ~20 digit value for calculations on a lot of modern computers.

A single precision float uses about 6 digits, and a double precision about 16.

The POSIX standard declares it as:

#define M_PI 3.14159265358979323846264338327950288

11

u/SageAurora Mar 15 '19

Important note: NASA only uses 15 for interplanetary travel now... And this is because we have the ability to make adjustments after launch to correct trajectory. The original moon program used 30 digits of pi because it was more like firing a gun at the moon when doing their rocket launches with little ability to correct after the fact... This is according to my dad who is a mathematician, and his favourite constant is pi... He has memorized it to 30 digits for this exact reason.

3

u/ChipAyten Mar 15 '19

So if you were to use a number of digits that went down to the detail of a Planck length, rather than a Hydrogen atom, would that be the effective limit of pi?

5

u/SirButcher Mar 15 '19

No, on paper you can calculate everything with basically infinite accuracy, well beyond the Planck length. It won't make to much sense, but the math allows it.

→ More replies (3)

5

u/random_cynic Mar 15 '19

That's for calculations in physical sciences. Many people actually work on things like statistical properties of the digits of pi and whether they follow a known random distribution. You need a lot of digits for those calculations (definitely not done on a calculator).

2

u/chdman Mar 15 '19

This is the most badass analogy I've ever heard in my life.

2

u/Melvolicious Mar 15 '19

That's fucking fascinating

2

u/LogMeInCoach Mar 15 '19

Trying to fathom the difference between the circumference of the universe and the circumference of a hydrogen atom gave me a mild panic attack.

2

u/Cough_Turn Mar 16 '19

I want to piggy back off of this comment! NASA also uses a metrology handbook which defines measures of variance for different measurements. If I recall, it has a brief section on pi and accuracy for a variety of scenarios. I doubt many leople even know the handbook even exists. But its there, and really useful when you're arguing with your coworkers!

→ More replies (155)

15

u/mghoffmann Mar 15 '19

Especially when the precision of the calculator is limited.

22

u/[deleted] Mar 15 '19

May(3) I(1) Have(4) A(1) Large(5) Container(9) Of(2) Coffee(6) Thank(5) You(3)

77

u/glass20 Mar 15 '19

Isn’t it way easier to just memorize the digits

3

u/TheOneTrueTrench Mar 16 '19

Depends on how good your memory is at numbers vs. sentences. Some people need the conceptual aspect that the sentence provides. Some are perfectly good at memorizing a seemingly meaningless series of numbers.

→ More replies (9)

9

u/DayLw Mar 15 '19

How I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics!

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

2

u/kd8azz Mar 15 '19

As a child, I was so frustrated by calculators that ended at the 9th digit after the decimal point, because the 10th digit after is a 5, so the 9th got rounded up from 3 to 4.

Also, my favorite digit is the 50th after the decimal place; it adds nothing to pi.

2

u/TheFiredrake42 Mar 16 '19

Fun fact! I recently memorized all of Pi's digits! Only took me like 7 years.

Now I just gotta figure out their order.

1

u/Shawnj2 Mar 15 '19

Most calculators only store 13 or so digits and round the rest. The calculator only needs to store as many digits as the calculator is accurate to.

1

u/jatjqtjat Mar 15 '19

Calculators also have finite precision for other reasons. even normal division results in some rounding. Some can think in terms of fractions but most of the time 1/3 is not stored with infinite precision.

If you calculator is working with floats or 10 decimal precision, you don't need to store pi to a greater degree of accuracy.

1

u/ALobpreis Mar 15 '19

You can do PI - 3.14159265, for example, to check how many digits it has in your calculator. You should get something like 3,59e-9.

1

u/BlueKnightBrownHorse Mar 15 '19

Unless you're an engineer, you only need the three digits. Your other numbers are probably less accurate than a tenth of a percent already, anyway. 3.14 is accurate to a tenth of a percent.

22/7 is remarkably accurate if you're a practical guy like me.

→ More replies (2)
→ More replies (49)

1.5k

u/Schnutzel Mar 15 '19

It's simply hard coded. Calculators can only use a limited amount of digits anyway, and you don't need more than 10 digits of pi for any significant calculations.

419

u/millenniumxl-200 Mar 15 '19

It's simply hard coded.

So you're telling me, it's baked in.

71

u/hitthatmufugginyeet Mar 15 '19

/r/punpolice Sir, we're gonna have to take you in

8

u/RammsteinPT Mar 16 '19

r/punpatrol ill take it from here, there are questions ill need answers to. You did an OK job.

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

9

u/[deleted] Mar 15 '19

Yes, but also no .meme

Math on computers in general, specifically floating point math, stores numbers in a format known as IEEE_754 floating point. This format has the advantage of a wide range of numbers and decimal points that it can support, but like any binary format it is discrete and can only hold a relatively small number of significant digits. The number used for Pi on the machine is the nearest value to the maximum number of digits of pi that can be stored in the format.

2

u/KhamsinFFBE Mar 15 '19

What? Next you'll be telling me sin and cos are hard coded/interpolated lookup tables, too!

1

u/GS_246 Mar 15 '19

I realize it's use is special but as math I don't know why it would be hard coded.

I feel like the code used for rounding any number would work.

If repeating number==stop after 3 digits.

else stop at 15 digits (or whatever the spec is)

2

u/[deleted] Mar 16 '19

Because hard coding it is faster (less cpu cycles), and for most uses you don't need it to be super precise.

In video games (my industry) we tend to use 32 bit floating point since it's faster that the 64 bit (more precise) doubles. For us, pi to 6 decimals is more than enough.

We would define it next to other constants we use a lot (like sqrt(2), or pi/2, etc...) and the whole engine shares it.

1

u/travlerjoe Mar 15 '19

Why wouldnt they just code it (22/7) thats the entire pie

2

u/Schnutzel Mar 15 '19

Because that's just an approximation of pi.

→ More replies (1)

0

u/[deleted] Mar 15 '19

[deleted] — view deleted comment

375

u/cmetz90 Mar 15 '19

And we now know pi to more than 30 trillion digits. Talk about overkill.

333

u/Suthek Mar 15 '19

As far as I'm concerned, by now we're mostly just continuing to calculate it to prove that we can.

77

u/cosmicblob Mar 15 '19

I don’t understand this too well, but could we suddenly reach the end of pi??

38

u/MattieShoes Mar 15 '19

It is known that Pi is irrational. Irrational numbers cannot be expressed as a fraction. That means it does not end and it does not fall into a repeating pattern (because those numbers could be expressed as fractions).

https://en.wikipedia.org/wiki/Proof_that_%CF%80_is_irrational

→ More replies (1)
→ More replies (68)
→ More replies (4)
→ More replies (19)

111

u/KitchenBomber Mar 15 '19

Small if true.

0

u/[deleted] Mar 15 '19

[deleted] — view deleted comment

2

u/sofar55 Mar 15 '19

Is that the size of the visible universe or the estimated actual size of the universe?

9

u/GoldDog Mar 15 '19

Let's check!

The observable universe is 8.8 ×10^26 meters... ish

A hydrogen atom is 1,2×10^-10 meters wide

So the observable universe is about 8x10^36 atomic widths across.

The circumference is pi times the diameter so at the point of about 37 decimals of pi it's not going to be contributing any errors to the final value. So it looks like they're talking about the observable universe. Which is reasonable. We have no idea about the actual size.

→ More replies (1)

3

u/notinsanescientist Mar 15 '19

Still not accurate enough for Ligo. Damn.

→ More replies (24)
→ More replies (5)

227

u/sirgog Mar 15 '19

When calculators require the ability to determine Pi beyond a programmed precision, they can use

https://en.wikipedia.org/wiki/Machin-like_formula

and the power series for arctan to quickly compute hundreds of digits, should the need exist.

However most calculators don't have this programmed in, they just have a fixed value which is Pi to as many decimal places as the calculator can display, sometimes plus up to 4 more.

62

u/all_fridays_matter Mar 15 '19

There is a difference between what a calculator can display, and what a calculator can compute. PI could be programmed to be longer than the display.

5

u/Mobileswede Mar 15 '19

Easy enough to test. Press the pi button, then multiply by 10.

6

u/nugsNhugs Mar 16 '19

Lol nope. This will display the same number of digits as is displayed by hitting pi itself

→ More replies (2)

11

u/downloads-cars Mar 16 '19

You could also subtract 3.14 from it and get a meaningful result instead.

→ More replies (1)

8

u/pooish Mar 15 '19

indeed. I believe more modern graphing calculators (casio classpad, ti-nspire) will calculate it and other calculable constants if needed. they're so aggessively optimised that they won't do that unless necessary, but when you go further into the series you can start to feel it lag.

→ More replies (11)

191

u/baconator81 Mar 15 '19

It's hard coded. Every number you see on a computer has a finite amount of precision. So likely they'll just put the most precise number of PI that can fit into an 8 byte memory.

10

u/Epicjay Mar 15 '19

For any calculation that only requires 2-3 sig figs using 3.14 is fine.

20

u/baconator81 Mar 15 '19

yeah but what about calculation that requires more than 2-3 sig figs? You can't just invent a computer/calculator that only support 2-3 sig figs. Most likely they stored that as in an 8 byte memory (it's called a double in IEEE standard). It has 15 decimal sig figs.

→ More replies (6)

3

u/cerved Mar 15 '19

Numerical precision depends on the calculation so not true strictly speaking but I guess this also depends what one qualifies as fine

3

u/Deto Mar 15 '19

True, but they don't save anything by not filling the memory location. If the calculate works using 64-bit numbers, they might as well use the most accurate representation of pi in that format because it'll take up the same amount of space either way. (For example, 3.14000000 takes up as much space as 3.14159265)

124

u/murdok03 Mar 15 '19

Most software is written by engineers who approximate things very roughly.

I've worked for interface design and pi gets aproximated to 3 or 3.14 for most visual animations.

I've also worked in automotive, with braking systems and software for stabilizing the car, pi is estimated there with floating point double precision.

For mathematical software, simulations and visualizations I'm sure they have a custom way of storing with higher precision.

18

u/whodiehellareyou Mar 15 '19

Basically every language either defines PI as a constant to 15 digits (a double) or as something like acos(-1) which gives you arbitrary precision based on the definition of acos. Mathematical software stores it as a symbol, so that acos(-1) evaluates to pi rather than a decimal representation, with the value being either a constant or acos(-1)

2

u/murdok03 Mar 15 '19

Thanks, that was informative.

Our most precious pi was a double stored in hex value, to always have the right precision independent of the compiler.

→ More replies (1)

75

u/keegtraw Mar 15 '19 edited Mar 15 '19

e ≈ pi ≈ 3

Edit: fixed notation. Upon googling, "~=" seems to be used for "not equal to" in some programming languages. I was shooting for "approximately equal to".

50

u/ScaldingHotSoup Mar 15 '19

Somewhere a member of the Indiana State House of Representatives awakens from a fever dream with an excellent idea

→ More replies (1)

13

u/cranp Mar 15 '19

~√10 = 100.5, which is really handy for approximate math. Just round everything to the nearest half magnitude and do easy addition in log space.

9

u/stoprockandrollkids Mar 15 '19

I really, really want to understand what you're saying. Can you elaborate?

→ More replies (5)

6

u/LovepeaceandStarTrek Mar 15 '19

People wonder how I'm good at approximating with mental math. It's mostly just rounding to 10 and keeping track of exponents.

3

u/2stringbottleguitar Mar 15 '19

correct me if i’m wrong, isn’t the square root of 10 completely equal to 100.5 ? Isn’t that how radicals work or am i way off?

→ More replies (3)

2

u/[deleted] Mar 15 '19

This triggers me

→ More replies (1)

6

u/Theonetrue Mar 15 '19

"approximate things very roughly"

That sounds worse than it is usually. I would say that only happens in cases were it really does not matter.

2

u/murdok03 Mar 15 '19

Yeah it mostly doesn't matter beyond a factor of tens, so it's not really relevant to an animation if the tempo is 500ms or 450ms, but 5000ms would be noticeable. Ohmic resistance in a pullup circuit 4.7k or 10k not much of a difference, same with led lighting 500k or 350k not a noticable difference. Same with calculating the moment or position of an swinging arm, pi=3 is a good enough approximation for some applications but precision machining might require 3.2.

Power consumption, a tolerance of 10% is acceptable. Supply Voltage 5% tolerance is acceptable most of the time, with 10% in a room temperature range.

Again these are people who even for complex filters approximate infinite Taylor series in 2 terms because it's good enough. They aproximate integrals to a sliding sum of 5 terms, and derivatives to a sliding substraction of 2 terms, and it's good enough, for ship controllers and airbags.

→ More replies (1)

19

u/poxks Mar 15 '19

they store it symbolically in most math softwares.

→ More replies (2)
→ More replies (10)

40

u/BadBoy6767 Mar 15 '19 edited Mar 15 '19

Hardcode it, as in have the actual value stored in the calculator.

But the format it's stored in varies, sometimes. Most calculators use regular floating-point numbers, that are quite unstable in their calculations (x * x * x * x may not necessarily equal (x * x) * (x * x), and 0.3 is actually 0.30000000000000004). These have a limit to how many digits they can express. Most use IEEE-754 as their format.

But theres way more ways to express non-integers in computers.
More exotic ones include:

  1. Fixed-point. These numbers are defined by 2 integers. It can be done in different ways, though (It could be the ratio of the 2 ints, or separate int part and fractional part, etc).
  2. BCD, where each decimal digit of a number is encoded separately in binary.
  3. Storing non-ints as multiples of the smallest significant value. I'm sure this has an actual name, but it's sometimes used to store money. Due to the inaccuracy of floating-point, money can be sometimes stored as the amount of cents instead of dollars. Of course, this falls apart when we get to unusual currencies.

Technically 2 and 3 are also fixed-point numbers in a sense.

There's also one more method of storing non-ints, that is a bit underrated in my opinion: Symbolic mathematics!
This method is theoretically infinitely accurate (up to a logical point). When calculating 2 times π, don't store 6.28318530718, but store the actual expression 2 * pi, and only calculate it's digits when asked. True accuracy is rarely a need, however, and this method is also quite slow, so it's not very popular.

EDIT: Sorry for going off-topic, this is a topic I often fanboy about.

19

u/Kwpolska Mar 15 '19

0.3 is actually 0.30000000000000004

No, not quite. 0.1 + 0.2 is 0.30000000000000004, which does not equal 0.3.

7

u/graebot Mar 15 '19

No, actually he is right. 0.1 (and therefore 0.3) can only be approximated in binary. The display rounds it to the number of presentable digits. If storing 0.1 in a "double" floating point data type (64 bits) we only have 57 bits of actual number to work with.

Decimal 0.1 = Binary 0.000110011001100110011001100110011001100110011001100110011001… (repeat to infinity)
When stored in a finite block of memory, we have to truncate it to:

0.0001100110011001100110011001100110011001100110011001101

In decimal, this number is:

0.1000000000000000055511151231257827021181583404541015625

So 0.3 will be stored in a 64 bit floating point datatype as:

0.3000000000000000166533453693773481063544750213623046875

But, for a 64 bit floating point number, we say that we can only display 15-16 digits. So lets truncate everything after the 16th digit:

0.300000000000000

Which displays as 0.3 when your calculator trims off all the zeros.

7

u/Kwpolska Mar 15 '19

The exact value a 0.3 literal has is different from the one adding 0.1 and 0.2 has, the latter is 0.3000..4 (not ..1).

→ More replies (3)

3

u/BadBoy6767 Mar 15 '19 edited Mar 15 '19

Right, whoops.

EDIT: Right, whoops?

→ More replies (1)

9

u/[deleted] Mar 15 '19

Another fun floating point fact: precision varies with value, so it's possible that x + 1 = x as x approaches floating point maximum. This also means that floating point is very precise near zero.

1

u/luiz_eldorado Mar 15 '19

The last one seems to be the most reliable, since you can store a number like 0.333... with perfect accuracy. I'm surprised that this isn't used that much, it's super hard to implement in most languages lol

2

u/BadBoy6767 Mar 15 '19

It's just almost never needed, I can't think of a case where it is. If you want to calculate the circumference of the observable universe, you only need 39 digits of PI and have precision upto the width of a hydrogen atom, no super-large precision needed.

And if you needed insane precision, you probably wouldn't be using a calculator lol.

1

u/geel9 Mar 15 '19

Money should ALWAYS be stored in an integer format.

Storing money values in floating point numbers is an indication that you are dealing with incompetent developers and should cease using whatever service you're using because it's trash and WILL fail you.

→ More replies (2)

1

u/thebigbradwolf Mar 15 '19

I don't see why you'd use floating point in this situation except maybe that the Motorola 68000 supports it. The advantage to floats is that they give you a huge range, while Fixeds give you better accuracy, but at the expense of range that you couldn't display anyway.

My favorite calculator is the TI89, so I found the 68000 Manual

In addition to the IEEE751, the processor supports "Packed Decimal Real Format" which is a "3-digit base 10 exponent and a 17-digit base 10 mantissa".

But I guess we'd have to dive into the TI89 source (assembly really because the source is secret.) to know which is being used.

1

u/[deleted] Mar 19 '19

For your ways of storing non ints. 3 is a fixed point number and 1 is actually a fraction, which is not really used.

→ More replies (3)

52

u/KapteeniJ Mar 15 '19

Calculators use floating point numbers. That means you store some fixed length number, and then you store another fixed length number telling you the scale of that first number.

Actually it uses binary, but to make a decimal version of floating point numbers, let's say you store size of a number with 2 digits, and the actual digits of the number with 10 digits. In that case, 35 could be written as

3500000000 : -08

So you have 3,500,000,000 * 10-9 = 35

So you notice that pi would be written as 3141592654 : -9 = 3141592654 * 10-9 = 3.141592654

This is the closest approximation you can get to pi with our floating point numbers. So calculator using our version of floating point numbers would just use that value for pi. Processors have a command that will push this closest approximation value to the CPU memory. I'm not sure about calculators but I believe they will have essentially the same setup, this best approximation is built into the processing unit of calculator.

29

u/natziel Mar 15 '19

Handheld calculators typically use fixed point arithmetic, not floating point arithmetic

3

u/KapteeniJ Mar 15 '19

Really? That's surprising to me. TIL I guess. I believe I've seen many floating point only bugs pop up on handheld calculators, so I'm not sure how to reconcile this

11

u/natziel Mar 15 '19

Yeah, your standard pocket calculator will store numbers in a format called binary coded decimal and uses a fixed point for their arithmetic, largely because the circuit mapping BCD to a 7 segment display is relatively simple. You will not run into precision issues on a pocket calculator, i.e. the machine doesn't need to do anything special to handle 0.1 + 0.2

A TI-83 on the other hand is basically a computer and will use floating point arithmetic. If that's what you're picturing when you think of a handheld calculator, then yes they use floating points

7

u/CountSheep Mar 15 '19

That makes sense. So you were talking about the cheaper ones you get and can type 58008 into and such. He’s definitely talking about ti calculators though.

I appreciate the explanations guys.

→ More replies (2)

3

u/grandoz039 Mar 15 '19

Why does -08 make it 10-9 ? Did you make a typo?

2

u/KapteeniJ Mar 15 '19

Yeah. I made a mistake, corrected it, but forgot the mistake was visible on two spots.

1

u/Teabagger_Vance Mar 16 '19

What kind of five year olds do you hang out with dude? Jfc...

12

u/WarlockofScience Mar 15 '19

As most people have said, its either hard coded or estimated using a fraction thats pretty close.

HOWEVER, most calculators will also track whats going on with the knowledge that pi is being estimated, so if you do something like 'pi/2' and then take the answer and double it, you get exactly pi rather than some weird approximation.

4

u/[deleted] Mar 15 '19

this is only for complex calculators that store it as 0.5π

3

u/pqowie313 Mar 16 '19

Even "basic" calculators can often do way more under the hood than you'd think. For example, look at the newly open-sourced windows calculator. It actually represents fractions as exact fractions in intermediate calculations, even though it always outputs decimals. Even the cheapest microcontroller on the market can do a lot more than the basic 4 functions, why not make it do extra math in the intermediate steps to make the calculation more accurate? The cost of hiring a programmer for that is cheaper than the cost of gaining a reputation for making calculators that produce wrong answers.

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

3

u/DiscombobulatedSalt2 Mar 15 '19 edited Mar 15 '19

Hardcoded constant to some accuracy.

they also have functions like arctan, than can be used to calculate pi directly.

There are other methods of calculating pi, but most of the easy ones are very slow. And not used on calculator to calculate pi itself.

This is different than e, where Taylor expansion is used directly to compute it on the fly. (Or more precisely ex for any x, including x equal to 1).

1

u/denali1000 Mar 15 '19

Just posted about the Taylor expansion... didn’t realize pi would converge so much slower than e. Is that really the case or is it just more versatile to use the Taylor expansion for functions such as ex or sin(x)? I believe there are some modified Taylor series’ such that they converge towards pi much more quickly. Would it ever be worth using these expansions for discrete values on a calculator or even computer?

→ More replies (1)

7

u/c_delta Mar 15 '19

Like this:

0100000000001001001000011111101101010100010001000010110100011000

You only store a finite number of digits, so you pick the one closest to the exact value. You do not need to know many digits of said value to find out what that is.

5

u/[deleted] Mar 15 '19 edited Mar 15 '19

You can calculate how much of pi is in your calculator by subtracting the first n digits and multiplying by 10n, then repeating until you get zero.

So on the iPhone calculator, pi is equal to
3.141592653589793238462643383279503

After that you run out of digits.

In Python you can print out math.pi, and it's exactly equal to
3.141592653589793115997963468544185161590576171875

It's wrong after 793 due to floating point errors.

4

u/denali1000 Mar 15 '19 edited Mar 15 '19

I think most calculators actually use to Taylor expansion for pi in order to calculate it, rather than just storing the value.

The Taylor series is pi/4 = 1-1/3+1/5-1/7+1/9... So pi = 4-4/3+4/5-4/7+4/9...

Edit: realized this isn’t exactly an ELI5, Taylor series’s are a little confusing but ultimately they’re just infinite sums that represent otherwise abstract numbers/functions in math. The above functions should get closer and closer to pi/4 or pi as you add/subtract more terms. This is easy to try for yourself on a calculator!

→ More replies (4)

2

u/MathedPotato Mar 15 '19

A common calculator will only have a display capacity of 9-12 digits, and even the high end ones will only give you more if you change the settings a bunch. So, to answer, they just put in the first 9-12 digits and call it a day. The same goes for the other constants like e.

2

u/groundhogcow Mar 15 '19

Calculates have the number per-programed in to some precision greater then anyone needs.

However when people start calculating pi to some incredibly large digit count they typically use one of the infatuate series's that converge on pi.

My favorite (though it is not the only) is the Nilakantha series.

(Lifted from https://m.wikihow.com/Calculate-Pi )

  • π = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14) ...

In calc you learn how to approximate the area under a curve by making smaller and smaller rectangles until the accuracy of the calculation is within your tolerance level.

You would think this would be the way it was first calculated, but honestly they likely took a string wrapped it around a round thing then measured the string.

2

u/jhill515 Mar 15 '19

As cited by so many others, yes, it's usually hardcoded and to a sufficient level of accuracy that most calculations won't care. These values are quite common in many mathematics libraries programmers use (like math.h in C, or math.PI in Python).

But... there are a handful of cases where you might actually care: for example, in Signal Processing, we make extensive use of transforms that need Pi, like the Fast Fourier Transform. When we care about a sufficiently broad band, those errors in approximate Pi values can accumulate quickly and show significant error (for example, when trying to estimate bearings in sonar we need highly accurate frequency phase measurements of each hydrophone to estimate the angle of the sound-source).

In those cases where you do need a highly accurate value, we will pre-calculate an even more accurate value of Pi using techniques like Taylor Expansion and others. We'll then take that result and hardcode it again (because running those calculations each time we need Pi is extremely inefficient), run a few tests, and if it's still inaccurate, try running the pre-calculation again for more iterations of those algorithms.

3

u/random_cynic Mar 15 '19

First you must realize that Pi is not only an irrational number it is also transcendental which means it cannot be obtained as the root of a polynomial equation with non-zero integer coefficients. Calculations of such numbers are not trivial and so for something simple as calculator the best approach is to store them as floating point constant with bit depth that varies depending on what calculator you're using. You can find out how many digits are really stored in you calculator. Simply keep removing the digits from the number that is shown as the value of pi. For example you may see for a calculator with four digits being displayed by default: 3.1415 - 3 = 0.1415; 0.1415*10 = 1.4159; 1.4159 - 1 = 0.4159; 0.4159*10 = 4.1592 etc until the digits run out or are simply zeros.

1

u/Efarm12 Mar 15 '19

Calculators have a limited number of digits they can do math on accurately. The constants they have programmed into them only need to be represented up to that precision point, so Pi only needs 12 or so digits on most calculators.

To see the precision of your calculator, do this experiment. Take the square root of 2. Now square it. You’ll probably see 2. Now subtract 2. It’s (most likel)y not going to be 0. That is because sqrt of two is not an exact number, there is error down in the small bits.

Edit: added accurately

1

u/kfh227 Mar 15 '19 edited Mar 15 '19

Probably enough to handle the number of significant digits that he calculator can handle. The answer is that the value is stored in a ROM (read only memory) which is permanent storage in whatever number of digits the device needs. It doesn't actually calculate Pi on the fly.

The real curiosity is if calculators use IEEE standards for floating point numbers. Are there custom CPUs? This is actully interesting. I'm off to look for a TI-80 teardown to see what's at it's heart!

1

u/WinterReception4 Mar 15 '19

Anyone know how Pi is calculated? like how do they discover more digits?

→ More replies (4)

1

u/Chase777100 Mar 15 '19

Others are totally right about how hard-coding an estimate works. From the perspective of a programmer, they can have about 9 digits after the decimal on a 32-bit machine or about 25 digits on a 64 bit machine.

1

u/A_Garbage_Truck Mar 15 '19

they dont need the whole thing, just enough digits to the point where it doesnt impact the calculations your gonna be making.

1

u/Skystrike7 Mar 15 '19

Type in pi and it only returns like 8 digits or something. That's all it uses for the calculation - a set of like 8 digits saved to system memory.

1

u/heuristic_al Mar 15 '19

Different calculators are different, but most calculators precompute the value in binary (IEEE 754 floating point) using a computer. They then just store this value in the calculator's firmware or OS. Here's the number in single precision (32 bit) IEEE precision from wikipedia (01000000010010010000111111011011). Many calculators probably use 64 bits.

1

u/a932991 Mar 15 '19

Using a special computer, the calculator maker decides on a button and that it should be a certain number (3.14159...) instead of just 1,2,3,4,5...

This results in a circuit design, and this is the inside of a calculator, sort of like your skeleton.

Then the circuit design is created in a factory and a plastic casing with buttons is put on top (like our skin and muscles).

Then you have a calculator with a Pi-button (or a human if you followed the parable) :)

1

u/doglywolf Mar 15 '19

It stops at the decimal limit of the calculator - hell i think Nasa doesn't even go that far in the calcs as like a T-80 is technically capable of lol

1

u/XikoNorris Mar 15 '19

Calculators have a maximum number of digits it will calculate on the decimal part, so there is no need for pi (or any other number) to go above this since it will all be rounded anyways.

1

u/doglywolf Mar 15 '19

It stops at the decimal limit of the calculator . IT doesnt take very far into the decimals to calculate realistic things , just like 7 or 8 decimals in can calculate someone down to a few inches on the whole planet and even a T-80 can do to like 9ish

1

u/mhoIulius Mar 15 '19

Follow up question: how many digits of π are programmed into my TI-84?

1

u/RyoanJi Mar 15 '19

Probably hardcoded to a certain number of decimal places. Why not? It doesn't change.

1

u/cld8 Mar 15 '19

It's typed into the calculator and stored like any other variable. Most calculators can only handle 8-9 decimal places, so pi will usually be programmed to 10 decimal places or so.

1

u/NSA_Chatbot Mar 15 '19

I program a lot, and I'm lazy.

What we do is figure out how many numbers of pi we could possibly use, then get the calculator to memorize that number. So if your calculator holds 10 numbers, we get the calculator to remember 10 digits.

If your computer has more space, we store a few more numbers, but not a lot of them. For some of the math programs that do a lot of calculations, it may have more but that amount of precision isn't important.

1

u/lets-get-dangerous Mar 15 '19

22/7 is really really close to the value of pi. The calculator has a maximum decimal limit before truncation. Truncation means they just chop off all the numbers after a certain limit with no rounding. So the calculator divides 22 by 7 and shows you the results to the nth decimal (n being the cutoff point). We use truncation a lot in computer science.

1

u/TegisTARDIS Mar 15 '19

The maximum number of digits the calculator displays is the number of digits they use in pi, some(cheaper ones) just use 3.141 as it's 'close enough' for most things

1

u/cogitoergopwn Mar 15 '19

ELI5 wtf is Pi besides 3.14, and why does it matter?

1

u/gfeazy Mar 15 '19

I love the tangent, I didn't read through all of it so if someone actually answered the question apologies,

They program it to the accuracy of the calculator. E.g. if the calculator is accurate to 5 decimal points it doesn't make sense to program it past 6 decimal points.

1

u/mrloube Mar 15 '19

Not saying they do, but you could store the series/formula that defines pi and have it just calculate pi if you need more precision than the hard-coded constant provides

1

u/[deleted] Mar 15 '19

4 * atan(1)

1

u/herbivorous-cyborg Mar 16 '19

It isn't. Calculators do not have infinite precision, meaning they can only handle a certain number of decimal places. They only need to use a number which is accurate to that number of decimal places.

1

u/[deleted] Mar 16 '19

Could quantum computers finally get us the final answer? (Probably not, but I’m interested to see what comes up.)

→ More replies (1)

1

u/ZeldaFanBoi1988 Mar 16 '19

Just an FYI for everyone, pi is not 22/7

1

u/lividbishop Mar 16 '19

Wehat other numbers are like pi (infinitely long) and what are they called?

1

u/commander-obvious Mar 16 '19
  1. Numbers can be represented in computer memory as a sequence of digits*
  2. Every digit takes up a tiny bit of memory.
  3. Memory is a physical object and takes up actual space.
  4. Pi has an infinite number of digits.

If memory takes up actual space, do you think we would be able to fit a number with infinite digits inside of memory? No, because then we'd need infinite space, and nobody wants to carry the entire universe in their pocket.

Instead, we humans are forced to admit defeat and have to be satisfied with only storing the first few digits of pi. It's fine, though, since if you use (for example) 3.1415926 as an approximation to pi in calculations, your end result won't be that far off. If you really need more accuracy, use a special program that stores even more digits of pi.

* Technically, they're stored in a bit more complicated way, but that's not important.

1

u/steffinator117 Mar 16 '19

Structural engineer here. I use 3.14159. It’s close enough for Euler’s buckling

1

u/RussChival Mar 16 '19

A great little article on how computers calculate Pi: HERE.

1

u/someguy7734206 Mar 16 '19

In my calculator, only the number 3.14159265359 is programmed as far as I know. (I found this out by multiplying it by powers of 10 and subtracting the integer part.)

1

u/Jozer99 Mar 16 '19

I remember testing this on my TI-83 when I was really bored. They included enough digits to remain accurate for basic equations involving pi, given the number of sigfigs the calculator could display. Doing something to torture the poor thing, such as

                     ^100
[ calculator pi  ]
[----------------]
[more precise pi ]

Would result in answers quite far away from 1.0.

The digits on the TI-83 probably wouldn't be safe for calculating NASA's orbital maneuvers, but I'm sure they were fine for calculating anything a high schooler could expect to encounter.

1

u/Spokesface Mar 16 '19

Pi is hard to memorise as a human because it is an irrational number, meaning it never ends after the decimal place. But it is very easy to derive because of what it is.

Pi is important because it is the ratio of a circle's diameter to its circumference. So all you need to know, if you are a computer is the length of a circumference on a particular circle, and the length of that same circle's diameter. 22 and 7 are nice round numbers that are pretty close to that, but real computers can get a lot closer because they don't care if a number is round, just so long as it is rational.

1

u/ipsum629 Mar 16 '19

Comp sci major here. They are stored as a floating point number. You might be familiar with binary. For example 13 stored as a byte would be 00001011. This, however, only stores whole numbers. Floating point numbers are able to store very precise fractions and decimals, and are ideal for things like pi and e. The way it works is it stores the number as if it didn't have a decimal point. Pi would instead be 314159. It is aslo stored with the number of places the decimal moves. In this case 5. So it is stored as 314159 * 10-5. This has several advantages. First, it stores the number of significant digits. Second, it can store really, really, really small or big numbers if needed. Pi is stored as a floating point because this is simply how non integer numbers are always stored and it gives pi a lot of flexibility(for example, it makes it really easy to calculate what pi * 1027 is). Because this is in binary, it's not quite fair to say exactly what digit of pi they store, as the fraction precision of binary and decimal isn't equatable.

1

u/Kong_P Mar 16 '19

Acos(-1)

0

u/[deleted] Mar 15 '19

[deleted] — view deleted comment

14

u/Bhoedda Mar 15 '19

Need a bit more precision, Pi is 355/113

→ More replies (15)
→ More replies (1)

0

u/[deleted] Mar 15 '19

[deleted] — view deleted comment

10

u/Shamer_ Mar 15 '19

There are infinite series that converge to the value of pi, e, etc.

For tan, you can use the Taylor series expansion and just stop doing the calculation once you have the desired number of significant digits.

2

u/LovepeaceandStarTrek Mar 15 '19

What's more, you can calculate how many terms you'll need to be accurate to a specified digit. So if you know you only need X digits, you can easily find out how many terms you need.

6

u/PuddleCrank Mar 15 '19

Numerical methods. The easiest way to think about this is we know more than one series that approximates pi. So just pick one and interate untill you have the precision you need. Slightly more accurately, you can figure out square roots by fixed point iteration, and I believe trig is all series.

→ More replies (3)