educative.io

Educative

Regarding Modulo

Hi,
Please clarify:
twenty_eight = 28
print (twenty_eight % 10) #Result is 8. That is the remainder

Similarly,
print (28%10) #Result is also 8. That is the remainder

Then why print(-28 % 10) is 2. Even using any number as (-298 %10) also gives 2.
Please explain the difference.

image

1 Like

I think you know how to calculate the remainder when both dividend and divisor are positive.
Once the dividend is negative such as in this case, -28÷10, the quotient should be -3, because quotient multiplies divisor have to be less than the dividend, when remainder exists. In addition, the remainder need to be positive definitely. Hence, -28-(-3*10)=2, the remiander is 2. Similarly, -298÷10, the quotient is -30, the remainder is equal to 2.

I do not understand - how did we get to -28-(-3*10)?

Hi @Pinaki_Ghosh @Tianyang.Yu @Pinaki_Ghosh !!
Let’s break down the calculations step by step to understand how the remainder is calculated in these cases.
Case 1: 28 % 10
When both dividend and divisor are positive, calculating the remainder is straightforward. For example, when you calculate 28 % 10, the quotient is 2 and the remainder is 8. This is because:

28 = 10 * 2 + 8

Case 2: -28 % 10
Now, let’s consider the case of a negative dividend, such as -28 % 10. In this case, the goal is to find a positive remainder that satisfies the equation:

-28 = 10 * quotient + remainder

However, because we want the remainder to be positive, we need to adjust the equation accordingly. Since the divisor is positive (10), the remainder should be positive, and the quotient might need to be decreased to make this equation work.

To find the correct quotient, we can test values until we find one that works:

  1. Quotient = -2, Remainder = -28 - (10 * -2) = -8 (Not positive)
  2. Quotient = -3, Remainder = -28 - (10 * -3) = 2 (Positive)

So, in the case of -28 % 10, the quotient is -3, and the remainder is 2, which means:

-28 = 10 * -3 + 2

Case 3: -298 % 10
The same reasoning applies to your other example, -298 % 10, where the quotient is -30 and the remainder is 2:

-298 = 10 * -30 + 2

Case 4: print(28 % -10)

In this case, the dividend is 28 and the divisor is -10. Similar to the previous examples, we want to find the quotient and remainder that satisfy the equation:

28 = -10 * quotient + remainder

Since the divisor is negative (-10), we want the remainder to be negative to satisfy this equation. To find the appropriate quotient, we can test values:

  1. Quotient = -2, Remainder = 28 - (-10 * -2) = 8 (Not negative)
  2. Quotient = -3, Remainder = 28 - (-10 * -3) = -2 (Negative)

So, in the case of 28 % -10, the quotient is -3, and the remainder is -2, which means:

28 = -10 * -3 + (-2)

This demonstrates that when the right-hand operand (divisor) is negative, the remainder can also be negative.

Case 5: 34.4 % 2.5

In this case, we have a floating-point dividend (34.4) and a floating-point divisor (2.5). The modulo operation works similarly for floating-point numbers. The goal is to find the quotient and remainder that satisfy the equation:

34.4 = 2.5 * quotient + remainder

We can perform the division to find the quotient:

quotient = 34.4 / 2.5 = 13.76

Now, we need to find the remainder:

remainder = 34.4 - (2.5 * 13.76) = 34.4 - 34.4 = 0

So, in the case of 34.4 % 2.5, the quotient is approximately 13.76, and the remainder is 0, which means:

34.4 = 2.5 * 13.76 + 0

This shows that the remainder can indeed be a floating-point value.
In summary, when dealing with a negative dividend and a positive divisor, the remainder is adjusted to be positive by finding the appropriate quotient that makes the equation work. This is why you get a positive remainder in these cases.
The modulo operation can handle both negative dividends and negative divisors, and it also works with floating-point numbers, calculating the remainder accordingly.
I hope it helps. Happy Learning :blush:

1 Like