0

Given $a$ and $b$ are integers, can there exist a negative value for $a \pmod b$?

kub0x
  • 2,117
  • 1
  • 15
  • 25
  • 1
    What is a%b, is it modulo? – kingW3 Feb 18 '17 at 12:52
  • Usually, in programming, this represents a remainder in a standard division. As a result, the output will always be non-negative. that is to say, as a matter of definition, we write $a=qb+r$ for $0≤r<b$ and the define $a%b$ to be $r$. – lulu Feb 18 '17 at 12:52
  • $3 = 1 = -1 = -3 (mod 2)$. However, since mathematicians love single output operations, you'll never see $3 = -1 (mod 2)$ in a paper, unless they specifically need that number for some reason. We like the smallest positive number by convention. – Kaynex Feb 18 '17 at 12:56
  • 2
    @lulu: Actually in many programming languages (e.g. C99, Java, Swift) the remainder a % b has the same sign as the dividend b (and can be negative). Here is an overview: https://en.wikipedia.org/wiki/Modulo_operation. – Martin R Feb 18 '17 at 12:58
  • @MartinR I was just looking that up, thank you. Even in Excel, $Mod(3,-2)$ outputs $ -1$. So, my comment only applies to the case $b>0$. – lulu Feb 18 '17 at 13:00
  • @kub0x: Re your suggested edit: We don't know if OP is talking about a % b (in some programming language) or about $a \pmod b$ (in the sense of modulo arithmetic). – Until that is clarified, the question is unclear. – Martin R Feb 18 '17 at 13:04
  • @Kaynex: I disagree with your comments about mathematicians' preferences, and think they're actually misleading. – Greg Martin Feb 18 '17 at 13:26

1 Answers1

1

The Euclidian division theorem says that for any two integers $a,b\in \mathbb{Z}$ and $b\neq 0$, there exist unique integers $r,q\in\mathbb{Z}$ such that $a=bq+r$ and $0\leq r< |b|$. So if what you mean by $a\%b$ is calculating the residue of the division of $a$ and $b$, no, $r$ can not be negative.

If you meant that $a\% b$ is calculating $a$ mod$(b)$, then it can be negative, because the class of equivalence of $a$ in $\mathbb{Z}/b\mathbb{Z}$, $$[a]=\{\dots,a-2b,a-b,a,a+b,a+2b,\dots\}.$$

GSF
  • 914
  • I would say, more precisely, that (in the second interpretation) saying "$a\pmod b$ is negative" is not a meaningful assertion, because some members of the equivalence class are negative and others are not. Similarly, it would make no sense to talk about whether $a\pmod 7$ is even or odd. Both are the mathematical equivalent of asking something like "Which country's people are named Lee?" – Greg Martin Feb 18 '17 at 13:24
  • Yes, that's true @GregMartin , I should have said that the class of $a$ admits a negative integer as a representative. – GSF Feb 18 '17 at 13:28