Page 1 of 3

modulus operation

PostPosted: Thu Aug 15, 2013 6:16 pm
by rajitha_nair01
hi all,

i have a number:
lets say
1124568796981831928381768678756 which is being passed to a pic x(44)(max length that i can get as an input) variable.

this number needs to be gone through a mod operation for 97.

lets say i am dividing this number in 9,7,7,7

112456687 mod 97 = 28
appending 28 with next 7 289698183 mod 97 = 20
appneding 20 with next 7 201928381 mod 97 = 86
appending 86 with next 7 867686787 mod 97 = 59
now when i try 5956 mod 97 gives me a wring result since it appnds zeroes in the right and gives me an answer.

can anyone tell me how to remove the extra spaces when i get number less than 44 chanracter and take the mod of the remaining.

help me.

Re: modulus operation

PostPosted: Thu Aug 15, 2013 6:55 pm
by BillyBoyo
Please show the code that you are using. Leading zeros will not affect a calculation, but trailing zeros would, but I don't know what you coded to come up with trailing zeros.

Re: modulus operation

PostPosted: Thu Aug 15, 2013 6:57 pm
by Robert Sample
 01  WS-VARS.
     05  WS-DATA                 PIC X(44)
     VALUE '11245668796981831928381768678756'.
     05  WS-REDEF                REDEFINES WS-DATA.
         10  WS-A                PIC 9(09).
         10  WS-B                PIC 9(07).
         10  WS-C                PIC 9(07).
         10  WS-D                PIC 9(07).
         10  WS-E                PIC X(14).
     05  WS-E-NUM                PIC 9(14).
     05  WS-MODULUS-VALUE        PIC 9(02) VALUE 97.
     05  WS-RESULT               PIC 9(02).
     05  WS-E-DIGITS             PIC 9(02).
     05  WS-DIVISOR              PIC 9(09).
     05  WS-DIVIDEND             PIC 9(09).
      DIVIDE WS-A BY WS-MODULUS-VALUE
          GIVING WS-DIVISOR
          REMAINDER WS-RESULT.
      DISPLAY 'FIRST  DIVISION ' WS-A ' ' WS-RESULT.
      COMPUTE WS-DIVISOR = WS-RESULT * 10000000 + WS-B.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'SECOND DIVISION ' WS-DIVISOR ' ' WS-RESULT.
      COMPUTE WS-DIVISOR = WS-RESULT * 10000000 + WS-C.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'THIRD  DIVISION ' WS-DIVISOR ' ' WS-RESULT.
      COMPUTE WS-DIVISOR = WS-RESULT * 10000000 + WS-D.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'FOURTH DIVISION ' WS-DIVISOR ' ' WS-RESULT.
      COMPUTE WS-E-NUM = FUNCTION NUMVAL (WS-E) .
      COMPUTE WS-E-DIGITS = FUNCTION LOG10 (WS-E-NUM) .
      COMPUTE WS-DIVISOR = WS-RESULT * 10 ** WS-E-DIGITS +
              WS-E-NUM.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'FIFTH  DIVISION ' WS-DIVISOR ' ' WS-RESULT ' '
              WS-E-NUM ' ' WS-E-DIGITS.
      STOP RUN.
produces results of
 FIRST  DIVISION 112456687 28
 SECOND DIVISION 289698183 20
 THIRD  DIVISION 201928381 86
 FOURTH DIVISION 867686787 59
 FIFTH  DIVISION 000005956 39 00000000000056 02

Re: modulus operation

PostPosted: Thu Aug 15, 2013 7:01 pm
by rajitha_nair01
i dont have a screen shot for the same... but yeah i coded in the same way like i hhave given above. there are trailing zeroes which i am getting. how are you appending the answer with next digits. i used the string operation which can be done only on x variable.

Re: modulus operation

PostPosted: Thu Aug 15, 2013 7:13 pm
by Robert Sample
As my code shows, there are multiple ways to achieve what you want -- I did it with no STRING verbs at all.

Re: modulus operation

PostPosted: Thu Aug 15, 2013 8:57 pm
by rajitha_nair01
since i am not able to run this code can you pls explain what are we doig here:

COMPUTE WS-DIVISOR = WS-RESULT * 10 ** WS-E-DIGITS +
WS-E-NUM.

???




Robert Sample wrote:
 01  WS-VARS.
     05  WS-DATA                 PIC X(44)
     VALUE '11245668796981831928381768678756'.
     05  WS-REDEF                REDEFINES WS-DATA.
         10  WS-A                PIC 9(09).
         10  WS-B                PIC 9(07).
         10  WS-C                PIC 9(07).
         10  WS-D                PIC 9(07).
         10  WS-E                PIC X(14).
     05  WS-E-NUM                PIC 9(14).
     05  WS-MODULUS-VALUE        PIC 9(02) VALUE 97.
     05  WS-RESULT               PIC 9(02).
     05  WS-E-DIGITS             PIC 9(02).
     05  WS-DIVISOR              PIC 9(09).
     05  WS-DIVIDEND             PIC 9(09).
      DIVIDE WS-A BY WS-MODULUS-VALUE
          GIVING WS-DIVISOR
          REMAINDER WS-RESULT.
      DISPLAY 'FIRST  DIVISION ' WS-A ' ' WS-RESULT.
      COMPUTE WS-DIVISOR = WS-RESULT * 10000000 + WS-B.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'SECOND DIVISION ' WS-DIVISOR ' ' WS-RESULT.
      COMPUTE WS-DIVISOR = WS-RESULT * 10000000 + WS-C.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'THIRD  DIVISION ' WS-DIVISOR ' ' WS-RESULT.
      COMPUTE WS-DIVISOR = WS-RESULT * 10000000 + WS-D.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'FOURTH DIVISION ' WS-DIVISOR ' ' WS-RESULT.
      COMPUTE WS-E-NUM = FUNCTION NUMVAL (WS-E) .
      COMPUTE WS-E-DIGITS = FUNCTION LOG10 (WS-E-NUM) .
      COMPUTE WS-DIVISOR = WS-RESULT * 10 ** WS-E-DIGITS +
              WS-E-NUM.
      DIVIDE WS-DIVISOR BY WS-MODULUS-VALUE
          GIVING WS-DIVIDEND
          REMAINDER WS-RESULT.
      DISPLAY 'FIFTH  DIVISION ' WS-DIVISOR ' ' WS-RESULT ' '
              WS-E-NUM ' ' WS-E-DIGITS.
      STOP RUN.
produces results of
 FIRST  DIVISION 112456687 28
 SECOND DIVISION 289698183 20
 THIRD  DIVISION 201928381 86
 FOURTH DIVISION 867686787 59
 FIFTH  DIVISION 000005956 39 00000000000056 02

Re: modulus operation

PostPosted: Thu Aug 15, 2013 9:17 pm
by Akatsukami
rajitha_nair01 wrote:i dont have a screen shot for the same

Good, since you ought not to waste space with screen shots, but rather copy and paste from your emulator, enclosing the text so added in Code tags.
since i am not able to run this code can you pls explain what are we doig here:

COMPUTE WS-DIVISOR = WS-RESULT * 10 ** WS-E-DIGITS +
WS-E-NUM.

Note also the two previous lines from Mr. Sample's code:
COMPUTE WS-E-NUM = FUNCTION NUMVAL (WS-E) .
COMPUTE WS-E-DIGITS = FUNCTION LOG10 (WS-E-NUM) .

After validating the variable left from the prior (fourth) division, the number of digits in that result, less one, is gotten by the LOG10 function. The result of that division is multiplied by 10^(# of digits - 1) (effectively moving it left that many positions) and the remaining digits are added (i.e., appended).

Re: modulus operation

PostPosted: Fri Aug 16, 2013 12:28 am
by Robert Sample
Actually, I didn't have the time to figure out what the LOG10 function did -- I displayed WS-E-DIGITS because it appears to have rounded the value to 2 instead of leaving it 1 as I expected -- so I used the LOG10 value instead of adding 1 to it as I was expecting to do. As long as the 31st through 44th digits are not all zero, the code will process correctly.

Re: modulus operation

PostPosted: Fri Aug 16, 2013 1:29 am
by Akatsukami
Robert Sample wrote:Actually, I didn't have the time to figure out what the LOG10 function did

:shock:
Robert, I have suddenly lost all faith in you :D

Re: modulus operation

PostPosted: Fri Aug 16, 2013 3:16 am
by Robert Sample
Sorry, Akatsukami, to ruin your faith in me. :oops: Production problems got me in the office at 6, 5:30, and 6 the last three mornings -- and one of those I was not even supposed to work! I may be able to finish my investigation tomorrow but it may be next week.