Given $a$ and $b$ are integers, can there exist a negative value for $a \pmod b$?
Can $a \pmod b$ be negative for any integral value of $a$ and $b$?
0
$\begingroup$
arithmetic
-
1What is a%b, is it modulo? – 2017-02-18
-
0Usually, 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$a\%b$ to be $r$. – 2017-02-18
-
0$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. – 2017-02-18
-
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. – 2017-02-18
-
0@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$. – 2017-02-18
-
0@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. – 2017-02-18
-
0@Kaynex: I disagree with your comments about mathematicians' preferences, and think they're actually misleading. – 2017-02-18
1 Answers
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\}.$$
-
0I 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?" – 2017-02-18
-
0Yes, that's true @GregMartin , I should have said that the class of $a$ admits a negative integer as a representative. – 2017-02-18