6
$\begingroup$

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
3

Final 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.

Perl Code Google Apple Spotlight Bing Calculator Wolfram Alpha

So we have two possible ways to handle this equation,

(11%2)*3 = 3
11%(2*3) = 5

Which way is correct and definitive? I need sources as if Wolfram alpha is doing incorrectly, I would like to have them change it.

$\endgroup$
  • 2
    $\begingroup$ There might be differences in the correct order of operations for mathematics and for various programming languages. $\endgroup$ – naslundx Apr 24 '14 at 16:57
  • $\begingroup$ For cases like this, where there is no ubiquitous convention, the denotation is ambiguous, so you should always use parentheses to disambiguate. $\endgroup$ – Bill Dubuque Apr 24 '14 at 17:08
  • $\begingroup$ I would but with a test question they don't alway do stuff like that. $\endgroup$ – traisjames Apr 24 '14 at 17:42
  • 1
    $\begingroup$ @naslundx: examples show the order for Google is not the same as for the other softwares tested. Which is order "for mathematics"? $a\mod b\cdot c=a\mod (b\cdot c)$ or $a\mod b\cdot c=(a\mod b)\cdot c$? Shouldn't this be the answer? $\endgroup$ – MattAllegro Apr 24 '14 at 19:25
0
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.