I am doing some calculations with %
operator in java
and python
.
While doing calculations, i found out that %
operator works differently in both languages when dealing with negative numbers.
For example
-21 % 4 = -1 // Java
-21 % 4 = 3 # Python
so i looked at some of the posts here on stackoverflow and found out that in java
, %
gives remainder whereas in python
, %
gives modulus. They both are same for positive numbers but give different result in case of negative numbers as shown in example above.
So i searched for the difference between modulus and remainder, read some posts online but difference between remainder and modulus is still not clear to me
Question
Can someone explain the difference between modulus
and remainder
in simple terms using above example?