$\bmod\!$ operator precedence: does $\,a\bmod b+c\,$ mean $\,(a\bmod b)+c\ $ or $\ a\bmod (b+c)\,$?

106 Views Asked by At

$\bmod\!$ operator precedence: does $\,a\bmod b+c\,$ mean $\,(a\bmod b)+c\ $ or $\ a\bmod (b+c)\,$?

My intution is that a mod b + c == (a mod b)+c, but, in Wolfram Alpha, it seems to prefer a mod b + c == a mod (b+c), while in a virtual machine I often work in does a mod b + c == (a mod b)+c.

What are the conventions for sequence of evaluation in modulo?

2

There are 2 best solutions below

0
On

As you have already seen, there is not a universal convention, so write parentheses to indicate what you mean. mod is not a "standard enough" operation to have a universally agreed convention.

3
On

A numbers modulo to another number is not a number, it is an equiavilance class that can contain other numbers, for example:

$13$ mod($2$) is $1$ but it is also $3$, also $5$, and so on, you have to define what you mean clearly, assuming you mean, the smallest positive integer $n$ that satisfies: $13=n$ mod($2$)

Then the writing:

$a$ mod($b$)+$c$

has a meaning, which is your virtual machines meaning, otherwise it does not, you can not add a number and an equiavilance class together.

By the way, is your input $a\%b$ in your machine? It is defined as the latter way I defined mod to be.