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
That is perfectly okay because they are in the same equivalency class. So, it isn't really that they have a different sign, it is just a different representative for that equivalency class (in the rationals, it is similar to ; same number, just different representatives for that equivalency class).
To answer the question in your title, the modulus (in your example, it is five) must always be at least for anything (interesting) to make sense. However, it is perfectly fine to write both
and
as
.
As a general statement, we write to mean that
. Since
and
, both statements are correct. However, the division algorithm implies that given integers
and
with
, then there exist unique integers
and
such that
and
. Since
, most mathematicians usually write
where
as this is the natural choice for a member of the congruence class.
How is negative modulus of a number calculated?
what is the modulo of negative number?
Blueprint modulo node doesn't handle negatives? - Programming & Scripting - Epic Developer Community Forums
math - Mod in Java produces negative numbers - Stack Overflow
Videos
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.
As I understand it, the modulus of a positive number x with a number n is - the remainder of x divided by n.
But how is the modulus of a negative number calculated?
Example - -3 mod 12 = 1, -1 % 19 = 18.
I spent more than an hour going through the answers to this question on Math.StackExchange, but nothing made sense to me.
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.
The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder. These functions give the same values for positive arguments, but the modulus always returns positive results for negative input, whereas the remainder may give negative results. There's some more information about it in this question.
You can find the positive value by doing this:
int i = (((-1 % 2) + 2) % 2)
or this:
int i = -1 % 2;
if (i<0) i += 2;
(obviously -1 or 2 can be whatever you want the numerator or denominator to be)
Since Java 8 you can use the Math.floorMod() method:
Math.floorMod(-1, 2); //== 1
Note: If the modulo-value (here 2) is negative, all output values will be negative too. :)
Source: https://stackoverflow.com/a/25830153/2311557