Is there any predominant convention as to where the binary modulo operator (i.e., the variant of the modulo operator that is not applied to a whole equation) ranks in the order of operations, in particular in respect to addition and multiplication?

Some examples:

  • Is $a·b \bmod n$ to be read as $a·(b\bmod n)$ or $(ab) \bmod n$?

  • Is $a + b \bmod n$ to be read as $a + (b\bmod n)$ or $(a+ b) \bmod n$?

Should no convention exist that is de facto carved in stone, I am happy about any convention prevalent in some field (outside of programming languages).

I am aware that this has been asked before in this question, but since this were two questions in one and the accepted answer did not address this aspect at all, I considered it better to ask a new question than setting a bounty on that question.


Using $\bmod$ as binary operation is actually quite rare in mathematics, so authors who use it will usually not assume any knowledge about precedence rules on the part of readers; they will prefer to put in possibly redundant parenthesis if necessary. Therefore it will be hard to deduce from usage any hard evidence about the assumed precedence rules. Nevertheless I think most would agree at least that $\bmod$ binds more strongly than addition or subtraction, but not more strongly than multiplication. This does not fix rules completely, but at least it implies that $a+b\bmod n$ reads $a+(b\bmod n)$ while $a\cdot b\bmod n$ reads $(a\cdot b)\bmod n$.

[Though it does not play a very important role in the current discussion, one should note that what counts for precedence it the symbols used, not their meaning, as an expression must be read (parsed) before any meaning can be attached to it. So I should not have just said "multiplication", but rather either "operator $\cdot$" or "juxtaposition", and it would be conceivable to give those two a different precedence.]

Of course programming languages must fix rules of precedence, and I'm pretty sure that most languages consider $\bmod$ to have the same precedence as division, which almost always is the same as for multiplication (at least when the latter is written using an operator symbol, not by juxtaposition), while associating left as most other operators.

I know one book that spends some time on this operation, Concrete Mathematics, whose section 3.4 has title "Mod, the binary operation". This section gives some interesting evidence. It contains an equation $$ x = \lfloor x\rfloor + x \bmod 1 $$ for $x\in\Bbb R$, from which one can deduce that $\bmod$ binds stronger than addition. Indeed the sentence following the equation says

Notice that parentheses aren't needed in this formula; we take $\bmod$ to bind more tightly than addition or subtraction.

A bit latter equation $(3.23)$ reads $$ c(x\bmod y) = (cx)\bmod(cy) $$ from which as such nothing can be concluded, other than that $\bmod$ does not obviously bind stronger than multiplication written as juxtaposition (otherwise the LHS could be "$cx\bmod y$" instead) nor obviously weaker (otherwise the RHS could be "$cx\bmod cy$" instead). But in fact the authors write after this equation:

(Those who like $\bmod$ to bind less tightly than multiplication may remove parentheses from the right side here, too.)

Note that while programming languages usually have no precedence level between additive and multiplicative operations, mathematical notation does seem to have such a level; indeed it seems, as I argued in this answer, that large operators like $\sum$ and $\prod$ as well as things like $\lim$ all have this precedence level towards their right (being unary operators, they have no precedence at all to their left). It would then be perfectly acceptable to allow $\bmod$ to share this precedence level, and the authors of Concrete Mathematics are apparently willing to admit (but not impose) this point of view. If one should decide so, I think it would be logical to also accept division written with "$/$" at this level, so that $ab/cd$ can mean what at first glance it seems to mean.

| cite | improve this answer | |

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.