Which happens first?

Lloyd Hugh Allen vze2f978 at mail.verizon.net
Fri Apr 6 23:29:16 EDT 2001


John Yeager wrote:
> 
> I know that this may be a dumb questio, yet I am new to programming and I
> am courious in a mathmatical expression such as  ( 16 * ( 15j + ( 4.2 -
> 3.9 ) + 20  ) / -2 ) % 5 which happens first is the () or is it the
> conversion to the complex number?? I would think it would be the ()

US folks use the convention "PEMDAS", "Please excuse my dear aunt
sally", to remember <parentheses> <exponents> <multiplication, division>
<addition, subtraction>.

Aussie folks (according to some mailing list a year or two ago--maybe AP
Calc?) use "GEMA": <grouping> <exponents> <multiplication> <addition>.

Python follows the same order of operations that mathematicians do, as
is confirmed by the result of (2.6-120j)

(16 * (15j + (4.2 - 3.9) + 20 ) / -2 ) % 5
(16 * (15j + (       20.3     ) / -2 ) % 5
(    240j   -  324.8          ) / -2 ) % 5
(   -120j   +  162.4                 ) % 5
    -120j   +    2.6

What is interesting to me is that the modulo only operates on the real
part of the number. Hadn't thought about which part it would affect how.

Remember that anything that computers do, we should be able to do by
hand, albeit more slowly. Remember when your Algebra I teacher said
"show all your work!"? This is where that pays off. (Unless, as an
Algebra I teacher, I just feel self-important).

-LHA



More information about the Python-list mailing list