[Tutor] Temerature Conversion??

BELSEY, Dylan dylan.belsey@baesystems.com
Sun Dec 1 19:25:02 2002


 Chuck,
    I believe that the problem lies in truncation.  Because you use a
floating point number (98.6) in your first calculation, the answer that the
Python interpreter returns, is in fact a floating point number.  In the
second calc. you only use integers (whole numbers) so the interpreter
truncates the decimal part off, during the calculation.  The missing
fractional part gets compounded by the multiplication.
    See the python session below.

>>> ((98.6-32)/9)*5
37.0
>>> ((37/5)*9)+32
95
>>> 37.0/5
7.4000000000000004
>>> 37.0/5*9+32
98.600000000000009
>>> num = 37.0/5
>>> num = num*9
>>> num
66.600000000000009
>>> 

    As an additional tip, you may want to look at the order of operations
(precedence rules) for things like +, -, * and / and the use of parentheses
for equations.  It works OK for your calcs as / and * take precedence over +
and -, however this may cause a problem in the future.  On the other hand
you may already be aware of this and if so please disregard.

    HTH,

        Dylan