David Holmén David Holmén - 11 months ago 63
Python Question

Precise Python 2.7 calculation including Pi

I am working on a small program that calculates different things related to orbital mechanics, such as the semi-major axis of an orbit, velocity etc. (No experience in this subject is required to answer this).

Everything worked out well, until I tried to calculate the orbital period (a formula where I had to use Pi):

T = 2π √ a3 / µ (Click for image)

Where T is time in seconds, a is the semi-major axis, and µ is the standard gravitational parameter.

Now, my problem is that my program does not calculate it very precise, as the result of the formula is a bit off; for example: a circular orbit at an altitude of 160km should take approx 88 minutes, but my program tells me an approx of 90 minutes and 37 seconds.

My code:

#the standard gravitational parameter of the Earth
gravPara = 3.986004418*10**14

#the semi-major axis of the orbit (the radius of the Earth + the apoapsis and periapsis of your orbit / 2)
semiMajor = (bodyDia + ap + pe) / 2

#formula to calculate orbital period
timeSeconds = (2 * math.pi) * math.sqrt(semiMajor**3 / gravPara)

#making the result appear as minutes and seconds instead of just seconds
timeMinutes = 0
while (timeSeconds > 60):
timeSeconds = timeSeconds - 60
timeMinutes = timeMinutes + 1

#round the variable to store seconds
round(timeSeconds, 0)

#print the result
print timeMinutes
print timeSeconds

So my question is: is it an error in my code, or is
not very precise when used together in such a formula? Should I store it as a float and use the float instead or should I split up the calculation into multiple pieces?

I would be very thankful if you could help me out on this one, as searching through the Python reference as well as other forums did not get me very far.

PS: when using
print math.pi
it returns a precise value of Pi, so the
function seems to be working correctly.

Answer Source

math.pi is a float with 15 decimals: 3.141592653589793

As per chepners comment to your original post, that equates to about the size of an atom when calculating spheres the size of the earth.

So to answer your question: it's not math.pi