What is the difference between remainder and modulo in programming languages?

[Principle of modulo and remainder]
Definition: a = bq + r and 0 <= |r| < |b|.
Question: Given a and b, require mod(a,b) and rem(a,b);

Analysis: For data satisfying the condition a = bq + r and 0 <= |r| < |b|, if a is not divisible by b, there are two pairs (q, r), in which r is a positive number (positive remainder), and r is negative (negative remainder) in the other pair.

Result: There are many definitions of modulo, and the definitions of modulo in different languages ​​may be different. The most common one is:
modulo: r when q is closer to infinitesimal (negative infinity), that is, mod(a, b)
; Remainder: r when q is closer to 0, that is, rem (a, b);
(take the remainder, follow the principle of making the quotient as close to 0 as possible; take the modulo, follow the principle of making the quotient as close to negative infinity as possible;)


[Example]
Example 1: mod(7, 3)=1, rem(7, 3)=1
Candidate group 1: (q1, r1)=(2, 1); 7=2*3+1
Candidate group 2: (q2, r2)=(3, -2); 7=3*3+(-2)
modulo: because q1 is closer to negative infinity than q2, so take (q1, r1) = (2, 1) , mod (7, 3) = 1;
remainder: because q1 is closer to 0 than q2, so take (q1, r1) = (2, 1), rem (7, 3) = 1;

Example 2: mod(7, -3) = -2, rem(7, -3) = 1
Candidate group 1: (q1, r1) = (-2, 1); 7 = (-2)*(-3 )+1
candidate group 2: (q2, r2)=(-3,-2); 7=(-3)*(-3)+(-2)
modulo: because q2 is closer to negative infinity than q1 , so take (q2, r2) = (-3, -2), mod (7, -3) = -2;
remainder: because q1 is closer to 0 than q2, so take (q1, r1) = ( -2, 1), rem(7, 3) = 1;

Example 3: mod(-7, 3) = 2, rem(-7, 3) = -1
Candidate group 1: (q1, r1) = (-2, -1); -7 = (-2)*3 +(-1);
Candidate group 2: (q2, r2)=(-3, 2); -7=(-3)*3+2;
Modulo: Because q2 is closer to negative infinity than q1, so Take (q2, r2) = (-3, 2), mod (7, -3) = 2;
remainder: because q1 is closer to 0 than q2, so take (q1, r1) = (-2, - 1), rem(7, -3) = -1;

Example 4: mod(-7, -3) = -1, rem(-7, -3) = -1
Candidate group 1: (q1, r1) = (2, -1); -7 = 2*(- 3) +(-1)
candidate group 2: (q2, r2) = (3, 2); -7=3*(-3)+2
modulo: because q1 is closer to negative infinity than q2, so take (q1, r1) = (2, -1), mod (7, -3) = -1
remainder: because q1 is closer to 0 than q2, so take (q1, r1) = (2, -1) , rem(7, -3) = -1

[More] 1. Other modulo operations, such as r must be the same as the negative sign of a, etc. The principle is similar. The candidate group is selected when modulo is performed according to the conditions, so it is no longer expanded. 2. Modulus is often used in grayscale schemes and abtest (not demanding random algorithms). Turned over the Baidu calculator, got the answer right, Mi question, hand in the paper~;

Author: Xiao Tingzi
Link: https://www.zhihu.com/question/30526656/answer/160437482
Source: Zhihu
Copyright belongs to the author . For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324454572&siteId=291194637
Recommended