About Modulo operation hibitor Junior Member Posts: 21 Threads: 10 Joined: Dec 2011 08-31-2012, 01:15 AM hi, can any friend can tell me how to understand the Modulo(%) operation in the turbo pmac. I try my best to grasp it by SRM and URM. but I'm failed. I don't know how does the "11%-4" gets 3, while "-11%-4" gets -3, and "3%-2.5" gets -2,while "-3%-2.5" gets 2. how does it come out? Thanks. Sina Administrator Posts: 675 Threads: 50 Joined: Jun 2008 08-31-2012, 10:27 AM I think the following equations will get you the same results: X%Y= If (Y>0): ABS(X-(INT(X/Y)*Y)) If (Y<0): INT(X-(INT(X/Y)*Y)) If (Y=0): 0 Sina Sattari Hardware Engineering Manager Delta Tau Data Systems, Inc. Gregs Delta Tau Insider Posts: 151 Threads: 2 Joined: Mar 2010 08-31-2012, 12:32 PM Here is the math for each case: 11%-4: -4 x -2 = 8, and so 8 + remainder of 3 = 11 -11%-4: -4 x 2 = -8, and so -8 + remainder of -3 = -11 3%-2.5: -2.5 x -2 = 5, and so 5 + remainder of -2 = 3 -3%-2.5: -2.5 x 2 = -5, and so -5 + remainder of 2 = -3 There are different ways of doing the math for cases in which the dividend and divisor are both negative, and unfortunately, there is no convention on which method to use. One method was just picked. The other posibilities are standard. Gregs Delta Tau Insider Posts: 151 Threads: 2 Joined: Mar 2010 08-31-2012, 01:56 PM I have to correct myself: there is no set convention for any case in which the divisor is negative, and the methods that Delta Tau uses provide advantage in handling rollover. For example, rollover in 24-bit registers can be handled with the following equations: Delta = (New24 - Old24)%-2^23 ;Handles rollover in both directions Delta = (New24 - Old24)%2^24 ;Handles rollover in positive direction hibitor Junior Member Posts: 21 Threads: 10 Joined: Dec 2011 09-03-2012, 09:26 PM thanks for your answers, Sina and Gregs, a related question about this for Sina. By the foluma,I take the INT(X-(INT(X/Y)Y)) to calculate the "-11%-4" and "-3%-2.5" like this: because -2.5<0,-4<0, so choose foluma "INT(X-(INT(X/Y)*Y))" -3%-2.5 INT(-3-(INT(-3/-2.5)*-2.5)) INT(-3/-2.5)=INT(1.2)=?;// 1 or 2, if gets 2 INT(-3-(2*-2.5))=2; -11/-4 INT(-11-(INT(-11/-4)*-4)) INT(-11/-4)=INT(2.75)=?;//2 or 3, if gets 3 INT(-11-(3*-4))=1,not -3, if want the result to be -3, INT(2.75) must be 2, but if it is 2, the INT(1.2) may be 1, then, -3%-2.5 will gets -0.5,not 2. Is it a contradiction, or some mistakes I did? Sina Administrator Posts: 675 Threads: 50 Joined: Jun 2008 09-05-2012, 02:03 PM hibitor, You are correct. I had to revise the formula I posted earlier. Here is a new one: X%Y = IF(Y>0) then X-(INT(X/Y)*Y), IF(Y<0) then IF(ABS(X)>=2*ABS(Y)) then X+SIGN(X*Y)*(FLOOR(ABS(X)/ABS(Y))*ABS(Y) IF(ABS(X) < 2*ABS(Y)) then X+SIGN(X*Y)*(CEILING(ABS(X)/ABS(Y))*ABS(Y) A few notes on the functions: INT: Rounds a number down to the nearest integer FLOOR: Rounds number down, toward zero, to the nearest integer CEILING: Returns number rounded up, away from zero, to the nearest integer ABS: Returns the absolute value of a number SIGN: Determines the sign of a number. Returns 1 if the number is positive, zero (0) if the number is 0, and -1 if the number is negative. Here is the calculated results in Excel:     And here is the formula for use in excel if X is in A1 and Y is in B1: Code: `=IF(B1>0,A1-(INT(A1/B1)*B1),IF(B1<0,IF(ABS(A1)>2*ABS(B1),A1+SIGN(A1*B1)*(FLOOR(ABS(A1)/ABS(B1),1)*ABS(B1)),SIGN(A1*B1)*(CEILING(ABS(A1)/ABS(B1),1)*ABS(B1))+A1),0))` Sina Sattari Hardware Engineering Manager Delta Tau Data Systems, Inc. « Next Oldest | Next Newest »