Drexel dragonThe Math ForumDonate to the Math Forum

Ask Dr. Math - Questions and Answers from our Archives
_____________________________________________
Associated Topics || Dr. Math Home || Search Dr. Math
_____________________________________________

Remaining Confusion

Date: 09/10/2011 at 19:16:26
From: Annie
Subject: Using % (Modulus) in Python/order of operations

OK, so I started learning Python for this Intro to AI course they're
teaching online via Stanford this fall. And I was buzzing along breezily
until we got to this:

   100 - 25 * 3 % 4

I interpreted the problem as

   100 - [(25 *3)%4]

In other words: multiply 25 times 3, divide the result (75) by 4 and get
the remainder, then subtract that remainder from 100.

But both in the text and via running the actual program, the answer was
given as "97."

If the order of operations is the standard one (i.e.,
multiplication/division, left to right, followed by addition/subtraction,
left to right), and if I understand modulus correctly (the remainder left
over when you divide the left number by the right number), then I don't
get how "97" is the right answer.

So maybe I don't understand that "%" (the modulus symbol in Python) as
well as I think I do? What, exactly, is modulus telling me to do? Find the
remainder of 3 divided by 4? Seventy-five divided by 4? Either way, when I
run the problem according to standard operation order, I get something
very different than 97! 

Or maybe it's the order I'm getting wrong?



Date: 09/10/2011 at 20:31:38
From: Doctor Peterson
Subject: Re: Using % (Modulus) in Python/order of operations

Hi, Annie.

Most of what you say is correct -- especially the order; but you haven't
told me what answer you got, so I can't tell what went wrong.

The "order of operations" (operator precedence) for any computer language
is defined by that language, not necessarily by human mathematical
standards; but if you look that up for Python, you'll find that it does
agree. Multiplication, division, and remainder are done with the same
precedence, from left to right. 

And if you do that, just the way you said, you should get the 
right answer:

   100 -   25 * 3  % 4

   100 - [(25 * 3) % 4]
          \______/
   100 - [   75    % 4]
         \____________/
   100 -        3
   \____________/
         97

To see that 75%4 = 3, you can either do a long division ...

     __18_r_3
   4 ) 75
       4
       -
       35
       32
       --
        3

... or divide, multiply, and subtract ...

    75 / 4 = 18.75, quotient = 18
    18 * 4 = 72
   75 - 72 = 3

... or divide, take the fraction part, and multiply:

    75 / 4 = 18.75, fraction part = .75
   .75 * 4 = 3

The calculator accessory in Windows has a MOD button that will do this for
you.

To check a remainder, you can make sure that

   quotient * divisor + remainder = dividend
      18    *    4    +     3     =    75

If this isn't enough to clarify what you did wrong, write back and show me
what you actually did and what answer you got, and I can probably figure
it out. My best guess is that you may have done what some people do when
they write to us about remainders, and took 18.75 to mean the remainder 
is 75.

- Doctor Peterson, The Math Forum
  http://mathforum.org/dr.math/ 



Date: 09/10/2011 at 21:13:48
From: Annie
Subject: Thank you (Using % (Modulus) in Python/order of operations)

YES! That's exactly what I did, and got 18.75. Thank you so much! Very
clear. I just needed to take it one step further. 

You rock!
Associated Topics:
High School Basic Algebra
High School Calculators, Computers

Search the Dr. Math Library:


Find items containing (put spaces between keywords):
 
Click only once for faster results:

[ Choose "whole words" when searching for a word like age.]

all keywords, in any order at least one, that exact phrase
parts of words whole words

Submit your own question to Dr. Math

[Privacy Policy] [Terms of Use]

_____________________________________
Math Forum Home || Math Library || Quick Reference || Math Forum Search
_____________________________________

Ask Dr. MathTM
© 1994-2013 The Math Forum
http://mathforum.org/dr.math/