Current location - Training Enrollment Network - Mathematics courses - -10 mod 3 The answer in the discrete mathematics book is 2, C++ runs-10% 3 =-1, how to do it? ...
-10 mod 3 The answer in the discrete mathematics book is 2, C++ runs-10% 3 =-1, how to do it? ...
The modular operation of natural numbers is defined as follows:

If A and D are two natural numbers and D is not zero, it can be proved that there are two unique integers Q and R, which satisfy a = qd+r and 0 ≤ R.

So for negative numbers, can we follow this definition?

If we find the result of (-10) mod 3 according to the law of positive remainder, we can say that-10 is (-4)* 3 +2.

Where 2 is the remainder and -4 is the quotient.

In addition, if the allowed remainder is negative and its absolute value is less than the divisor, then

It can be expressed as-10 is (-3)* 3-1. -1 is the remainder and -3 is the quotient.

c++:cout & lt; & lt(- 10) % 3; //Output-1

Java:system . out . println((- 10)% 3); //Output-1

python:& gt; & gt& gt(- 10)% 3 // Output 2

Baidu calculator: (-10) mod 3 = 2

Google calculator: (-10) mod 3 = 2

Youdao calculator: (-10) mod 3 =-1

So there is no unified conclusion about negative modulus operation. It's hard to say which is absolutely right or wrong.

Personally,-10% 3 =-1 is reasonable.

Because-10/3 = -3, it should not be equal to -4.

So Yu-1 is logical, reasonable and easy to understand.