modulus operation



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

modulus operation

Postby rajitha_nair01 » Thu Aug 15, 2013 6:16 pm

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.
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: modulus operation

Postby BillyBoyo » Thu Aug 15, 2013 6:55 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: modulus operation

Postby Robert Sample » Thu Aug 15, 2013 6:57 pm

 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
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: modulus operation

Postby rajitha_nair01 » Thu Aug 15, 2013 7:01 pm

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.
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: modulus operation

Postby Robert Sample » Thu Aug 15, 2013 7:13 pm

As my code shows, there are multiple ways to achieve what you want -- I did it with no STRING verbs at all.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: modulus operation

Postby rajitha_nair01 » Thu Aug 15, 2013 8:57 pm

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
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: modulus operation

Postby Akatsukami » Thu Aug 15, 2013 9:17 pm

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).
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: modulus operation

Postby Robert Sample » Fri Aug 16, 2013 12:28 am

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: modulus operation

Postby Akatsukami » Fri Aug 16, 2013 1:29 am

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
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: modulus operation

Postby Robert Sample » Fri Aug 16, 2013 3:16 am

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post