I was taking a programming test last night that had a math equation that simplified to 11 % 2 * 3, no () or likewise. When I compute it, being taught modulous occurs at the same level of multiplication or division. As a result I get
11 % 2 * 3
1 * 3
3Final result I get is 3. When I checked my math in spotlight calculator on my mac I get 5. And then we jump into the rabbit hole.
I go to my preferred math calculator and visit WolframAlpha. Answer is 5. Google the formula and get 3.
So we have two possible ways to handle this equation,
(11%2)*3 = 3
11%(2*3) = 5Which way is correct and definitive? I need sources as if Wolfram alpha is doing incorrectly, I would like to have them change it.
$\endgroup$ 41 Answer
$\begingroup$There is no well established convention for the order of precedence between "the modulus operator" and multiplication (or addition, for that matter, because the same problem arises if you replace multiplication with addition in your question.) You have in your hands an example demonstrating that they can't be used with equal precedence.
The acceptability of the final answer hinges entirely upon the choice made for precedence.
While % can certainly be treated as a binary operation of natural numbers, it is not really the main way mathematicians use modulus. Rather than looking at it as an operation, we think of it as indicating the 'environment' where arithmetic is taking place. So $\pmod n$ indicates that we are not working in the natural numbers but with a quotient ring of the integers.
In that context, you can establish an identity between modulus and $+/*$ operations. You could state it this way:
$$(a+b)\%n = ((a \%n)+(b\%n)) \%n$$
and
$$(a*b)\%n = ((a \%n)*(b\%n)) \%n$$
$\endgroup$