0

$\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?

Eric Wofsey
  • 330,363
Johan
  • 41
  • 1
    You will find a comprehensive answer to this question in the wikipedia entry Order of operations. – J.-E. Pin Jul 25 '21 at 17:07
  • 1
    In mathematics, it is more common to consider modulo a comparison of two numbers, rather than a binary operation like multiplication and division. – Arthur Jul 25 '21 at 17:09
  • 1
    @J.-E.Pin Yeah the one there is what I'd assume, that modulo came before addition, but Wolfram Alpha contradicted that. I think Ted's answer to my question makes sense, that modulo is relatively unusual (I hadn't heard of it before I started working with IT stuff) and conventions lacking for that reason. – Johan Jul 25 '21 at 17:30

2 Answers2

1

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.

Ted
  • 33,788
-2

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.

Ata
  • 232
  • 1
  • 1
    @J.W.Tanner Following that post I got to this one, https://math.stackexchange.com/a/3052963/942992. It says that mod is not an operation in mathematics, but, it is in computer science. That could explain why Wolfram Alpha, and the virtual machine I write a computer program in, have different conventions. – Johan Jul 25 '21 at 17:48
  • @Johan But that post - like the above answer - is incorrect, as I explain in the above linked thread. As for the OP, there is no universal convention in use to disambiguate that expression, so generally we need to introduce further context or syntax (e.g. paren's) to do so – Bill Dubuque Jul 26 '21 at 00:06