C99 requires that when a/b is representable:
(a/b) * b + a%b shall equal a
This makes sense, logically. Right?
Let's see what this leads to:
Example A. 5/(-3) is -1
=> (-1) * (-3) + 5%(-3) = 5
This can only happen if 5%(-3) is 2.
Example B. (-5)/3 is -1
=> (-1) * 3 + (-5)%3 = -5
This can only happen if (-5)%3 is -2
C99 requires that when a/b is representable:
(a/b) * b + a%b shall equal a
This makes sense, logically. Right?
Let's see what this leads to:
Example A. 5/(-3) is -1
=> (-1) * (-3) + 5%(-3) = 5
This can only happen if 5%(-3) is 2.
Example B. (-5)/3 is -1
=> (-1) * 3 + (-5)%3 = -5
This can only happen if (-5)%3 is -2
The % operator in C is not the modulo operator but the remainder operator.
Modulo and remainder operators differ with respect to negative values.
With a remainder operator, the sign of the result is the same as the sign of the dividend (numerator) while with a modulo operator the sign of the result is the same as the divisor (denominator).
C defines the % operation for a % b as:
a == (a / b * b) + a % b
with / the integer division with truncation towards 0. That's the truncation that is done towards 0 (and not towards negative inifinity) that defines the % as a remainder operator rather than a modulo operator.
Blank output when entering negative numbers - Forum
modular arithmetic - How can I find a mod with negative number? - Mathematics Stack Exchange
modulo - Modulus with negative numbers in C++ - Stack Overflow
modular arithmetic - Modulus of negative numbers - Mathematics Stack Exchange
Videos
lass Solution {
public:
int reverse(int x) {
int ans=0;
while(x!=0)
{
int i= x%10;
x=x/10;
if(ans>INT_MAX/10||ans<INT_MIN/10)
{return 0;}
ans =ans*10 +i;
}
return ans;
}
};this is the code to reverse the integer number , so 123 will be 321 and -123 will be -321, but i am not understanding the negative number part, what i know is % of negative number is positive
like for example:
take x= -123 in above program
-123%10 = 3 , -123/10=-12 ;
-12%10 = 2, -12/10 = -1;
-1 % 10 = ?? , here i am confused , if it is 1 which is positive like above modulo then the answer will be wrong and if it will be -1 then it is not making sense to me, please someone explain, thanks.
edit : the question i was doing has size limit of [-2^31 to 2^31 -1] , so i can't convert negative number to positive then again convert into negative , because a boundary condition of one number because of ' -1' in positive integer side causing error.
so i look up solution , and the above code worked, but i didn't understand the negative number part.
It's $3$ because $-11 = 7(-2) + 3$.
Another way to see this is to take $-11$ and keep adding $7$ to it until you get a positive number. This works because, if you're working modulo $7$, then adding $7$ is the same as not changing the number (modulo $7$). So:
$-11 + 7 \equiv -11 \pmod 7$, and $-11 + 7 = -4$. Therefore $-4 \equiv -11 \pmod 7$. Well, we're still negative. Let's do it again:
$-4 + 7 \equiv -11 \pmod 7$, and $-4 + 7 = 3$. Therefore, $3 \equiv -11 \pmod 7$.
Or, equivalently, $-11 \equiv 3 \pmod 7$.
How do we know to use $-2$? Let's recall how it works with positives first.
If you want to evaluate $31 \pmod 7$, you first recognize that $31 = 28 + 3 = 7 \cdot 4 + 3$. Therefore $31 \equiv 3 \pmod 7$. What did we do here? We found the largest multiple of $7$ that's less than or equal to $31$.
Alternatively, with division, you can evaluate $31/7 \approx 4.429$. The largest integer less than or equal to this is $4$. Therefore $31 = 7 \cdot 4 + \text{some number}$, where your goal is to determine what $\text{some number}$ is.
This same exact process applies for negative numbers.
If you want to evaluate $-11 \pmod 7$, you need the largest multiple of $7$ that's less than or equal to $-11$. This is $-14$. And $-14 + 3 = -11$, therefore your answer is $3$.
Alternatively, with division, you can evaluate $-11/7 \approx -1.571$. The largest integer less than or equal to this is $-2$. Therefore $-11 = 7 \cdot (-2) + \text{some number}$, where your goal is to determine what $\text{some number}$ is.
As others have said, it is because $โ11 = 7(โ2) + 3$
However, I believe all other answers failed to mention that you need(ed) to define the division algorithm FIRST.
The division algorithm is defined as Euclidean division, where given two integers a and b, with b โ 0, there exist unique integers q and r such that
$a = bq + r$ and $0 \leq r < |b|$
therefore, $r$ MUST always be positive and numbers modulo fit into this definition therefore, the mod of a number is never negative.
The thing is that the % operator isn't the "modulo operator" but the "division remainder" operator with the following equality
(a/b)*b + a%b == a (for b!=0)
So, if in case your integer division rounds towards zero (which is mandated since C99 and C++11, I think), -5/4 will be -1 and we have
(-5/4)*4 + -5%4 == -5
-1 *4 -1 == -5
In order to get a positive result (for the modulo operation) you need to add the divisor in case the remainder was negative or do something like this:
long mod(long a, long b)
{ return (a%b+b)%b; }
Using % a second time in @sellibitze's and @liquidblueocean's answers probably won't be as slow as % tends to be in general, because it boils down to either one subtraction of b or none. Actually, let me just check that...
int main(int argc, char **argv) {
int a = argc; //Various tricks to prevent the
int b = 7; //compiler from optimising things out.
int c[10]; //Using g++ 4.8.1
for (int i = 0; i < 1000111000; ++i)
c[a % b] = 3;
//c[a < b ? a : a-b] = 3;
return a;
}
Alternatively commenting the line with % or the other line, we get:
With
%: 14 secondsWith
?: 7 seconds
So % is not as optimised as I suspected. Probably because that optimisation would add overhead.
Therefore, it's better to not use % twice, for performance reasons.
Instead, as this answer suggests and explains, do this:
int mod(int k, int n) {
return ((k %= n) < 0) ? k+n : k;
}
It takes a bit more work if you want it to work properly for negative n too, but that's almost never necessary.
For every integer $m$ there are are unique integers $q$ and $r$ where $m = 6q + r$ and $0 \le r < 6$. In the case of $m = -8$ the $q= -2$ and $r = 4$ because $-8 = 6(-2) + 4$.
If the remainder has to be an integer $r$ so that $0 \le r < n-1$ then $- 8 \equiv 4 \mod 6$ because $-8 + 2* 6 = 4$. $4$ is the ONLY possible integer between $0$ and $6$ that you can get to from $8$ by adding or subtracting multiples of $6$.
Not sure how familiar with modular arithmetic you are, but deriving a few basic results and appealing directly to definitions, those results become much more obvious:
Proposition 1.1: For any two integers $a,b$, with $a \gt 0$, there exist integers $q,r$ such that $$ b=qa +r , \qquad 0 \leq r \lt a.$$
Proof:
Consider the rational number $\frac{b}{a}$. There exists a unique integer $q$ such that $$q \leq \frac{b}{a} \lt q +1$$ $$\implies qa \leq b \lt qa + a$$ $$\implies 0 \leq b - qa \lt a,$$ and, letting $r=b - qa $, the result follows.
$\square$
Definition 1.2: Let $a,b \in \mathbb{Z}$. We say $a$ divides $b$, if, for some integer $c$, $$b=ac.$$
$\quad$
Definition 1.3: Let $m$ be a positive integer. For any $a,b \in \mathbb{Z}$, if $m$ divides $a-b$, we write $a \equiv b \pmod{m}$.
$\quad$
Proposition 1.4: Every integer is congruent to exactly one of the integers $0,1,2 \cdots, m-1$ $\pmod{m}$.
Proof:
Note that $$a \equiv b \pmod{m} \iff a-b=qm,$$ for some integer $q$, and so Proposition 1.4 follows immediately from Proposition 1.1.
$\square$
Evaluating the example in your question, by Proposition 1.4, $-8$ is congruent to exactly one of the integers $0,1, 2,3,4,5, \pmod{6}$.
Now, it is clear that $-8=-2 \cdot 6 + 4$ and so $$-8 \equiv 4 \pmod{6},$$ or, in your notation $$-8\pmod{6}=4.$$
As a general rule, the modulo and division should satisfy the equation
b * (a/b) + a%b == a
For positive numbers, it is obvious that this means that a%b must be a positive number. But if a/b is negative, then the result is rounded towards zero.
So take for instance a = -4, b = 3. We know that a/b = -1.3333, which rounded towards zero becomes a/b == -1. From the equation above, we have that b * (-1) + a%b == a. If we insert a and b, we get -3 + a%b == -4, and we see that a%b must be -1.
Your suffering stems from embracing the illusion that % is a "modulo" operator. In truth, it is a remainder operator (C11 ยง6.5.5):
The result of the
/operator is the quotient from the division of the first operand by the second; the result of the%operator is the remainder
Reject the illusion and accept the truth, and the behavior of the operator will become clear (Ibid.):
If the quotient
a/bis representable, the expression(a/b)*b + a%bshall equala
In your case, a/b is -4/3, which is -1, hence representable. So a%b satisfies:
(a/b)*b + a%b = a
(-1)*3 + a%b = -4
-3 + a%b = -4
a%b = -1
In the below examples, the modulus with the positive integers makes sense. I've almost been programmed my whole life with division and remainders. However, the negative numbers don't make sense. I've looked at difference formulas, but I can't seem to make them work in my head or on paper. (Using the Window calculator for results)
-4 mod 3 = 2 but 4 mod 3 = 1 -5 mod 3 = 1 but 5 mod 3 = 2