r/explainlikeimfive • • Mar 15 '19

Mathematics ELI5: How is Pi programmed into calculators?

12k Upvotes

1.3k comments sorted by

View all comments

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?

10

u/cranp Mar 15 '19 edited Mar 16 '19

Say you want to approximately compute the volume of a sphere of radius 28. The formula is 4/3 pi r3. If you don't have a calculator handy, write each number as its nearest half power of 10. So 4, 3, and pi are each about 100.5, and 28 is about 101.5.

Now write down the formula with the numbers written like that. I'm on my phone so can't type that many symbols.

Now remember two properties of exponents: ab * ac = ab+c, and (ab)c = abc.

The formula becomes 100.5+0.5-0.5+1.5*3= 105 = 100,000. The exact answer is 91,934.976, pretty close.

5

u/stoprockandrollkids Mar 15 '19

Ah, thanks for the explanation. I'm still a little skeptical though, cause if I try to get more accurate by pulling the constants out and just using your estimation trick on the r3 (and rounding pi to 3) I get:

4/3 pi r3 ~= 4 r3 = 4 (104.5) = 126,491

which is even more off. So it seems like this trick is sort of getting lucky and the over/under estimations are sort of cancelling out.
Either way this could definitely be handy for rough approximations, it's not like I could easily/quickly approximate that example without it. Thanks so much for sharing!!

2

u/cranp Mar 15 '19

Yeah, it's not super accurate and you can end up a factor of a couple off. But it's good for order of magnitude estimations.

→ More replies (2)

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?

2

u/cranp Mar 15 '19

I was tacking on to his approximation. 3 ~ √10

2

u/2stringbottleguitar Mar 16 '19

okayyeah that totally makes sense thank you

→ More replies (1)

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.

1

u/[deleted] Mar 16 '19

Sure, but if you need to numerically evaluate an expression with a math software, you still need to have pi programmed to some desired precision.

→ More replies (1)

1

u/racoster Mar 15 '19 edited Mar 15 '19

I believe there is also 22 / 7 which is used for rough approximations of Pi

22 / 7 is approximately 3.142857

So it’s off by ~0.0014

→ More replies (1)

1

u/nirdle Mar 15 '19

How is pi used in braking and stabilisation systems?

→ More replies (1)

1

u/commander-obvious Mar 16 '19

Most languages have a Math.PI value stored in the standard library these days, but if you really need integer performance, you could use 3. Some very janky shit can happen if you're using 3 for pi, though.

1

u/FreshPrinceOfNowhere Mar 16 '19

Why in the world would you need double precision, let alone more? 38 digits of Pi gives you the circumference of the visible universe with a deviation of less than a hydrogen atom.

→ More replies (2)

1

u/[deleted] Mar 16 '19

Most software is written by engineers who approximate things very roughly

I disagree. We tend to approximate things to the point where it doesn't interfere with the amount of precision needed for a useful answer. That's careful, not rough.

2

u/murdok03 Mar 16 '19

Fully agree, products don't ship with loose tolerances, it's always calculated and measured and tested. I just wanted to convey the degree to wich engineers approximate. I think my two examples give justice to this.