[Tutor] Order Of Operations Question

David Hutto smokefloat at gmail.com
Thu Jul 29 01:00:15 CEST 2010


On Wed, Jul 28, 2010 at 6:11 AM, Evert Rol <evert.rol at gmail.com> wrote:
> Sorry, forgot to reply-to-all:
>
>
> I don't know the book nor the exercise, but see my comments interspersed in the code, and a few general remarks at the bottom
>
>> From a practice exercise in Building Skills In Python page 64 I'm
>> working on How Much Does The Atmosphere Weigh? Part 1:
>> To check it states that the answer should be app. 10**18kg However,
>> and I've checked to make sure that the math I've layed out matches up
>> with the texts, I get 5.07360705863e+20
>>
>> In the code I have broken the order of operations down to more
>> parenthetical, and tried outright, but see nothing obvious about how
>> it's strung together. If anyone has had a similar experience with the
>> problem given, or see anything blatantly obvious I've done wrong with
>> the ordering of operations. I tried to include as much of the
>> problem(formulas are just above variables they're used in) as comments
>> as possible.
>>
>> import math
>> def atmosphereWeight():
>>   pi = math.pi
>>   """Air Pressure (at sea level) P0. This is the long-term average.
>>   P0 = 1.01325 × 10**5"""
>>   airPressCLevl = 1.01325*(10**5)
>>   gravity = 9.82
>>   """We can use g to get the kg of mass from the force of air
>> pressure P0. Apply the acceleration of gravity
>>   (in m/sec2) to the air pressure (in kg · m/sec2). This result is
>> mass of the atmosphere in kilograms per
>>   square meter (kg/m2).
>>   Mm2 = P0 × g"""
>>   masAtmoInKgPerSqM = airPressCLevl * gravity
>
> Simply from looking at the units left and right of the equality sign, you'll need to *divide* by g, not multiply: [kg] = [kg m / s^2] / [m / s^2]
>
>
>>   """Given the mass of air per square meter, we need to know how
>> many square meters of surface to apply
>>   this mass to. Radius of Earth R in meters, m. This is an average
>> radius; our planet isn’t a perfect sphere.
>>   R = 6.37 × 10"""
>>   avgRadiusEarth = 6.37 * (10**6)
>>   """The area of a Sphere.
>>   A = 4πr2"""
>>   areaSphere = 4 * pi * (avgRadiusEarth**2)
>>   """Mass of atmosphere (in Kg) is the weight per square meter,
>> times the number of square meters
>>   Ma = P0 × g × A"""
>>   masEarthAtmoInKgPerSqM = airPressCLevl * gravity * areaSphere
>
> ditto here: divide by gravity, not multiply by it.
>
>
>>   print(masEarthAtmoInKgPerSqM)
>>
>> atmosphereWeight()
>
>
> Few general remarks:
> - the standard way of writing numbers with a power of ten in code is something like 1.01325e5. I guess this is also easier/quicker to execute (not that this code is time-critical, but in general)

I'll probably ending up reading something about it later in the book,
but as a quick question, why does:

>>> 5e18 == 5**18
False
>>> int(5e18) == int(5**18)
False
>>> 1.01325e5 == 1.01325**5
False

> - why do you assign masTmoInKgPerSqM, then later not use it when calculating masEarthAtmoInKgPerSqM?

I turned all of the formulas given into a variables, however in
masEarthAtmoInKgPerSqM it would have been neater to use
masTmoInKgPerSqM * areaSphere, instead of reusing the two variables
again in .masEarthAtmoInKgPerSqM.


> - just use math.pi when calculating areaSphere, instead of "pi = math.pi" and then later using pi. For me, that's just as clear.
> - no need to put parentheses around powers; they are evaluated before the multiplication (unless this is what you meant by "to more parenthetical"

Yes, it was just to break it into smaller, more readable, pieces.

> - try indenting the comments as well; more readable

It looked better when color coded in the editor, but not here in black
and white.

>
> Probably not all of the above are necessary, if you wrote this for debugging your problem, but they're just some thoughts that occurred to me.
>
> Enjoy your calculations.

Will the 'fun' never end?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list