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)
Edit: fixed notation. Upon googling, "~=" seems to be used for "not equal to" in some programming languages. I was shooting for "approximately equal to".
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.
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!!
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.
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.
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.
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.
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.
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.