modulus operation



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

Re: modulus operation

Postby BillyBoyo » Fri Aug 16, 2013 3:53 am

A less CPU-strenuous version:

 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-B1-CALC              PIC 9(09).
      05  FILLER REDEFINES WS-B1-CALC.
          10  WS-A1-REMAINDER     PIC 99.
          10  WS-B1               PIC 9(07).
      05  WS-C1-CALC              PIC 9(09).
      05  FILLER REDEFINES WS-C1-CALC.
          10  WS-B1-REMAINDER     PIC 99.
          10  WS-C1               PIC 9(07).
      05  WS-D1-CALC              PIC 9(09).
      05  FILLER REDEFINES WS-D1-CALC.
          10  WS-C1-REMAINDER     PIC 99.
          10  WS-D1               PIC 9(07).
      05  WS-E1-STRING           PIC X(16).
      05  FILLER REDEFINES WS-E1-STRING.
          10  WS-D1-REMAINDER     PIC 99.
          10  WS-E1               PIC X(14).
      05  WS-E1-CALC              PIC 9(16).
      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).
 PROCEDURE DIVISION.
                                                         
     MOVE WHEN-COMPILED           TO W-WHEN-COMPILED
     DIVIDE WS-A BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-A1-REMAINDER
     MOVE WS-B TO WS-B1
     MOVE WS-C TO WS-C1
     MOVE WS-D TO WS-D1
     MOVE WS-E TO WS-E1
     DIVIDE WS-A BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-B1-REMAINDER
     DIVIDE WS-B1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-B1-REMAINDER
     DIVIDE WS-C1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-C1-REMAINDER
     DIVIDE WS-D1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-D1-REMAINDER
     DIVIDE WS-D1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-D1-REMAINDER
     UNSTRING WS-E1-STRING DELIMITED BY SPACE
         INTO WS-E1-CALC
     DIVIDE WS-E1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-RESULT
     DISPLAY 'FIRST  DIVISION ' WS-A ' ' WS-A1-REMAINDER
     DISPLAY 'SECOND DIVISION ' WS-B ' ' WS-B1-REMAINDER
     DISPLAY 'THIRD  DIVISION ' WS-C ' ' WS-C1-REMAINDER
     DISPLAY 'FOURTH DIVISION ' WS-D ' ' WS-D1-REMAINDER
     DISPLAY 'SECOND DIVISION ' WS-E ' ' WS-RESULT
                                                         
     GOBACK
     .


Output is:

FIRST  DIVISION 112456687 28     
SECOND DIVISION 9698183 20       
THIRD  DIVISION 1928381 86       
FOURTH DIVISION 7686787 59       
SECOND DIVISION 56             39


The remainder is preprended to a copy of each segment of the input. The final one, with a variable number of digits prepended with the previous remainder, is made into a number using UNSTRING to a numeric field. It will even work when the last 14 bytes are blank, because there will always be a numeric value (the remainder from the previous calculation).
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Re: modulus operation

 

Re: modulus operation

Postby rajitha_nair01 » Fri Aug 16, 2013 3:35 pm

Robert Sample wrote:. As long as the 31st through 44th digits are not all zero, the code will process correctly.



and what if a series of 22 charachters is provided as an input.

And is there any way to try the same thing without using intrinsic functions?
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: modulus operation

Postby rajitha_nair01 » Fri Aug 16, 2013 4:06 pm

BillyBoyo wrote:A less CPU-strenuous version:

 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-B1-CALC              PIC 9(09).
      05  FILLER REDEFINES WS-B1-CALC.
          10  WS-A1-REMAINDER     PIC 99.
          10  WS-B1               PIC 9(07).
      05  WS-C1-CALC              PIC 9(09).
      05  FILLER REDEFINES WS-C1-CALC.
          10  WS-B1-REMAINDER     PIC 99.
          10  WS-C1               PIC 9(07).
      05  WS-D1-CALC              PIC 9(09).
      05  FILLER REDEFINES WS-D1-CALC.
          10  WS-C1-REMAINDER     PIC 99.
          10  WS-D1               PIC 9(07).
      05  WS-E1-STRING           PIC X(16).
      05  FILLER REDEFINES WS-E1-STRING.
          10  WS-D1-REMAINDER     PIC 99.
          10  WS-E1               PIC X(14).
      05  WS-E1-CALC              PIC 9(16).
      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).
 PROCEDURE DIVISION.
                                                         
     MOVE WHEN-COMPILED           TO W-WHEN-COMPILED
     DIVIDE WS-A BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-A1-REMAINDER
     MOVE WS-B TO WS-B1
     MOVE WS-C TO WS-C1
     MOVE WS-D TO WS-D1
     MOVE WS-E TO WS-E1
     DIVIDE WS-A BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-B1-REMAINDER
     DIVIDE WS-B1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-B1-REMAINDER
     DIVIDE WS-C1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-C1-REMAINDER
     DIVIDE WS-D1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-D1-REMAINDER
     DIVIDE WS-D1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-D1-REMAINDER
     UNSTRING WS-E1-STRING DELIMITED BY SPACE
         INTO WS-E1-CALC
     DIVIDE WS-E1-CALC BY WS-MODULUS-VALUE
        GIVING WS-DIVISOR
        REMAINDER WS-RESULT
     DISPLAY 'FIRST  DIVISION ' WS-A ' ' WS-A1-REMAINDER
     DISPLAY 'SECOND DIVISION ' WS-B ' ' WS-B1-REMAINDER
     DISPLAY 'THIRD  DIVISION ' WS-C ' ' WS-C1-REMAINDER
     DISPLAY 'FOURTH DIVISION ' WS-D ' ' WS-D1-REMAINDER
     DISPLAY 'SECOND DIVISION ' WS-E ' ' WS-RESULT
                                                         
     GOBACK
     .


Output is:

FIRST  DIVISION 112456687 28     
SECOND DIVISION 9698183 20       
THIRD  DIVISION 1928381 86       
FOURTH DIVISION 7686787 59       
SECOND DIVISION 56             39


The remainder is preprended to a copy of each segment of the input. The final one, with a variable number of digits prepended with the previous remainder, is made into a number using UNSTRING to a numeric field. It will even work when the last 14 bytes are blank, because there will always be a numeric value (the remainder from the previous calculation).






just to know:

just to tell you. i mite get numbers that are smaller than 31 too.

and this code dint work for

111023200000435195001213180

just an expample i tried.

correct me if i am wrong in using your code.
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 » Fri Aug 16, 2013 4:12 pm

rajitha_nair01 wrote:And is there any way to try the same thing without using intrinsic functions?

COBOL intrinsic functions will not attack you the moment you take your eye off of them.
"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: 1053
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: modulus operation

Postby rajitha_nair01 » Fri Aug 16, 2013 4:32 pm

Akatsukami wrote:
rajitha_nair01 wrote:And is there any way to try the same thing without using intrinsic functions?

COBOL intrinsic functions will not attack you the moment you take your eye off of them.




thats right indeed!!

but we here are asked to use intrinsic functions only if there is no otjer way out. STANDARDS!!!!...
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 » Fri Aug 16, 2013 4:37 pm

Since you set out the 9,7,7,7 yourself, I'm a little surpised that you can have fewer than 30 digits, and freely admit I didn't cater for it.

However, by applying the same technique as I used for the last field to any field which may contain (or be entirely) trailing space, you can easily overcome the mis-description.

Continuing with the two-digit remainder after your number has ended is not a problem, but you can also avoid it by testing the last byte of any possible "space" fields for being space, and knowing that you don't then need to go further.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: modulus operation

Postby BillyBoyo » Fri Aug 16, 2013 4:41 pm

Yes, you can avoid using intrinsic FUNCTIONs. Since they are only here for the "last part" of your data, you already have a non-FUNCTION way to deal with it, and there are any number of others.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: modulus operation

Postby Robert Sample » Fri Aug 16, 2013 4:58 pm

and what if a series of 22 charachters is provided as an input.

And is there any way to try the same thing without using intrinsic functions?
All right, you need to stop and define the rules for us. Your data contained fields of 9, 7, 7, 7 bytes or 30 bytes total with 14 additional bytes after that. If you can have only 22 bytes then the code could be written to check each 7-byte field to be numeric and if not use NUMVAL on it. You would also need to build in some logic to check for all spaces since 22 bytes means the last 7-byte field would be all spaces. Since you are only giving us pieces of the picture, we will let you figure out the code changes to implement such a strategy -- once you've fully defined the rules.

And why would you want to code up without intrinsic functions? They are a part of COBOL just like the STRING statement and in their place just as useful. It would not be hard (merely tedious) to move the 9-byte and 7-byte fields, one byte at a time using reference modification (or arrays), to a variable that is redefined as numeric -- but why bother when the intrinsic functions provide what is needed?

You have solutions to your original problem -- so now you need to stop trying to change the rules on us and go implement one of the solutions.
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

Re: modulus operation

Postby rajitha_nair01 » Fri Aug 16, 2013 5:01 pm

BillyBoyo wrote:Since you set out the 9,7,7,7 yourself, I'm a little surpised that you can have fewer than 30 digits, and freely admit I didn't cater for it.


its not somthing i thought of. thats an algorithm fr somthing that we are workingg here. its just a way to acheive what is required.
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: modulus operation

Postby rajitha_nair01 » Fri Aug 16, 2013 5:09 pm

thanks all for ur replies. will figure out snthing based on all of your solutions.
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

PreviousNext

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post