Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About Modulo operation
#1
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.
#2
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.
#3
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.
#4
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
#5
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?
#6
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.


Forum Jump:


Users browsing this thread: 1 Guest(s)