Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions

Similar presentations


Presentation on theme: "Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions"— Presentation transcript:

1 Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter 7 Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions

2 Chapter Contents Basic Arithmetic Verbs
Options Available with Arithmetic Verbs COMPUTE Statement Signed Numbers in Arithmetic Operations Intrinsic Functions

3 Basic Arithmetic Verbs
ADD, SUBTRACT, MULTIPLY, DIVIDE All require fields operated on to Have numeric PICTURE clauses Contain numeric data when statements executed

4 ADD … TO Statement identifier-1 ADD … TO identifier-2 ... literal-1
Format 1 identifier-1 ADD … TO identifier-2 ... literal-1 identifier-1 or literal-1 added to identifier-2 Result stored in identifier-2

5 ADD … TO Examples Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD Statement Result Add X To Y Y = 8 Add X, 9 To Y Y = 17 Add X, 6, Y To Z Z = 21

6 ADD … TO Statement Identifiers preceding TO are unchanged
Value of identifier after TO Used in ADD operation Original value replaced with ADD result

7 ADD … GIVING Statement identifier-1 ADD … GIVING identifier-2 ...
Format 2 identifier-1 ADD … GIVING identifier-2 ... literal-1 Identifiers and literals preceding GIVING added together Result stored in identifier-2

8 ADD … GIVING Examples Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD Statement Result Add X, Y Giving Z Z = 8 Add X, 10 Giving Y Y = 15 Add X, 4, Y Giving Z Z = 12

9 ADD … GIVING Statement Identifiers preceding GIVING are unchanged
Value of identifier after GIVING Not part of ADD operation Original value replaced with ADD result May be report-item with edit symbols

10 ADD Statement Comma followed by one space may be used to separate operands Result of ADD always placed in field(s) after TO or GIVING Result field must be data-name, not a literal

11 Producing More Than One Sum
Several ADD operations can be done in single statement Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD Statement Result Add X To Y, Z Y = 8, Z = 12 Add X, 6 Giving Y, Z Y = 11, Z = 11

12 ADD … TO vs ADD … GIVING Use ADD … TO when original contents of result operand Need to be included in operation But are not needed after operation Use ADD … GIVING when Original contents of all operands except result field are to be retained ADD HOURS-WORKED TO WEEKLY-HOURS Vs. Giving

13 SUBTRACT Statement identifier-1 SUBTRACT … FROM identifier-2 ...
Format 1 identifier-1 SUBTRACT … FROM identifier literal-1 identifier-1 or literal-1 subtracted from identifier-2 Result stored in identifier-2

14 SUBTRACT Examples Assume A, B and C are numeric fields A = 6, B = 2 and C = 18 SUBTRACT Statement Result Subtract A From C C = 12 Subtract B, 5 From C C = 11 Subtract B From A, C A = 4, C = 16

15 SUBTRACT … GIVING Statement
identifier identifier-2 SUBTRACT … FROM literal literal-2 GIVING identifier-3 ... identifier-1 or literal-1 subtracted from identifier-2 or literal-2 Result stored in identifier-3 Format 2

16 SUBTRACT … GIVING Examples
Assume A, B and C are numeric fields A = 6, B = 2 and C = 18 SUBTRACT Statement Result Subtract B From A Giving C C = 4 Subtract A From 15 Giving C C = 9 Subtract A, 4 From C Giving B B = 8

17 MULTIPLY Statement identifier-1 literal-1 MULTIPLY BY identifier-2 ...
Format 1 identifier-1 MULTIPLY BY identifier-2 ... literal-1 identifier-1 or literal-1 multiplied by identifier-2 Result stored in identifier-2

18 MULTIPLY Examples MULTIPLY Statement Result
Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5 MULTIPLY Statement Result Multiply Q By R R = 28 Multiply 10 By S S = 50 Multiply 2 By R, S R = 14, S = 10

19 MULTIPLY … GIVING Statement
identifier-1 identifier-2 MULTIPLY BY literal-1 literal-2 GIVING identifier-3 ... identifier-1 or literal-1 multiplied by identifier-2 or literal-2 Result stored in identifier-3 Format 2

20 MULTIPLY … GIVING Examples
Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5 MULTIPLY Statement Result Multiply Q By R Giving S S = 28 Multiply Q By 3 Giving S S = 12 Multiply 6 By Q Giving R, S R = 24 S = 24

21 MULTIPLY Statement Only two operands can be multiplied using the MULTIPLY statement To obtain product of 3 operands requires two instructions To find Price x Qty x Discount Multiply Price By Qty Giving WS-Amt Multiply Discount By WS-Amt

22 DIVIDE Statement identifier-1 literal-1 DIVIDE INTO identifier-2 ...
Format 1 identifier-1 DIVIDE INTO identifier-2 ... literal-1 identifier-1 or literal-1 divided into identifier-2 Result stored in identifier-2

23 DIVIDE Examples Assume X, Y and Z are numeric fields X = 2, Y = 12 and Z = 8 DIVIDE Statement Result Divide X Into Y Y = 6 Divide 3 Into Y Y = 4 Divide 2 Into Y, Z Y = 6, Z = 4

24 DIVIDE … GIVING Statement
Format 2 identifier INTO identifier-2 DIVIDE literal BY literal-2 GIVING identifier-3 ... identifier-1 or literal-1 divided into or by identifier-2 or literal-2 Result stored in identifier-3

25 DIVIDE … GIVING Examples
Assume X, Y and Z are numeric fields X = 2, Y = 12 and Z = 8 DIVIDE Statement Result Divide X Into Y Giving Z Z = 6 Divide Y By X Giving Z Z = 6 Divide 16 By Z Giving X, Y X = 2 Y = 2

26 REMAINDER Clause Optional clause with DIVIDE used to store remainder of division Assume Q and R have PICTUREs of 99 Divide 70 By 15 Giving Q Remainder R Stores quotient 4 in Q and integer remainder 10 in R What can this be used for???

27 ROUNDED Option Arithmetic result truncated if room to store all decimal positions not available Result of 3.89 stored in field with PIC 9V9 as 3^8 Include ROUNDED to round result to PICTURE specification Result of 3.89 stored as 3.9 if ROUNDED option used Actually adding .5 then truncating

28 ROUNDED Option 01 Amt1 Pic 9V99 Value 2.25. 01 Amt2 Pic 999. Examples
Arithmetic statement Result Value Stored Multiply .3 By Amt Amt1 = 0^67 Multiply .3 By Amt1 Rounded Amt1 = 0^68 Divide 150 By 9 Giving Amt2 Rounded … Amt2 = 017

29 Overflow or Size Error Occurs when result value too large to be stored in result field Result of this ADD statement is 1, Add 350 To 725 Giving Num If Num has PICTURE of 999, only 3 digits can be stored High-order digits truncated so 075 stored in Num

30 Checking for Overflow Any arithmetic statement may include one or both size error clauses ON SIZE ERROR statement(s) Specifies one or more statements to be executed if overflow (size error) occurs NOT ON SIZE ERROR statement(s) Specifies one or more statements to be executed if overflow (size error) does not occur

31 SIZE ERROR Clause Example
Add X To Y Giving Z On Size Error Display ' Result too large' Not On Size Error Perform Calc-Para End-Add If sum of X and Y too large to store in Z, Display statement executed If Z large enough for result, Calc-Para is performed When using one or both clauses, use scope terminator to end arithmetic operation END-ADD, END-SUBTRACT END-MULTIPLY, END-DIVIDE

32 Size of Receiving Fields
Ensure receiving field has PICTURE large enough to store result Addition - define resultant field one position larger than largest field added Subtraction - define resultant field as large as number being subtracted from Assumes positive numbers Assumes smaller subtracted from larger number

33 Size of Receiving Fields
Multiplication - define resultant field equal to sum of lengths of operands begin multiplied Division - define resultant field equal to sum of number of digits in divisor and dividend

34 COMPUTE Statement General arithmetic statement using symbols in place of arithmetic verbs Symbol Verb + ADD - SUBTRACT * MULTIPLY / DIVIDE ** exponentiation

35 COMPUTE Statement arithmetic-exp-1 COMPUTE identifier-1 … = literal-1
Identifier to left of equal sign set to value of arithmetic-expression, literal or identifier on right of equal sign Format

36 COMPUTE Examples Assume X, Y and Z are numeric fields X = 9, Y = 4 and Z = 12 COMPUTE Statement Result Compute Z = X * Y Z = 36 Compute X = Z - Y + 2 X = 10 Compute X = Y X = 4 Compute Z = Y ** 2 Z = 16

37 Order of Evaluation 1. ( ) override rules 1-3, all operations
Arithmetic expression may include any combination of symbols +, -, *, / or ** Order of operations 1. ( ) override rules 1-3, all operations in ( ) performed first 2. ** all exponentiation performed first 3. * or / in order or appearance left to right 4. + or - in order or appearance left to right Same rules apply here as standard math

38 COMPUTE Examples Assume X, Y and Z are numeric fields X = 6, Y = 18 and Z = 5 COMPUTE Statement Result Compute Z = Y / X + 3 Z = 6 Compute Z = Y / (X + 3) Z = 2 Compute Y = Z + X * 10 Y = 65 Compute Y = Z * X / 10 Y = 3

39 COMPUTE Statement COMPUTE can include same optional clauses used with other arithmetic verbs ROUNDED follows result field (identifier preceding equal sign) If ON SIZE ERROR or NOT ON SIZE ERROR clauses used, include scope terminator END- COMPUTE

40 Signed Numbers Use S in PIC clause of result field if
Numbers used in calculation may be negative Calculation may produce negative results PIC clause without S assumed to be unsigned If negative result stored in unsigned field, sign not retained We discussed this already

41 Intrinsic Functions Built-in procedures to perform particular task like Find square root of number Convert letters to uppercase Get current date Approved as extensions to COBOL standard in 1989 Now included in many compilers Not sure about ours, we’ll have to test it

42 Intrinsic Functions Find square root of X and place result in Y
Example Find square root of X and place result in Y Compute Y = Function Sqrt(X) Value of X passed to function called Sqrt Code in function finds square root of X Result returned by Sqrt assigned to Y

43 Intrinsic Functions Convert More-Data to uppercase
Example Convert More-Data to uppercase Move Function Upper-Case (More-Data) To Up-More-Data If More-Data = "Yes", function Upper-Case returns value "YES" Value "YES" moved to Up-More-Data

44 Intrinsic Functions FUNCTION function-name (argument)
Argument - input to function May be numeric or alpha-numeric depending on function Functions may have 0, 1 or more arguments Format

45 Intrinsic Functions Output of function - result returned after function performs its task Function returning alphanumeric result used in statements using alphanumeric data-items Function returning numeric result can be used only in arithmetic expressions, of course Page 283 – 288 has a list of several categorized by type

46 COBOL 2008 Changes Spaces around arithmetic operators will no longer be required So we should put spaces around each word and the parenthesis COMPUTE statement will yield same results on all compilers Will make precision or number of decimal places in each intermediate calculation fixed This depends on when the answer is stored.

47 Chapter Summary ADD, SUBTRACT, MULTIPLY, and DIVIDE verbs
format without GIVING Receiving field is part of arithmetic May not be report-item with GIVING format Receiving field is not part of arithmetic May be report-item

48 Chapter Summary COMPUTE used for any combination of arithmetic operations Order of evaluation of operators 1. ** 2. * or / in sequence left to right 3. + or - in sequence left to right 4. ( ) override normal hierarchy rules

49 Chapter Summary ROUNDED can follow receiving field in any arithmetic verb ON SIZE ERROR, NOT ON SIZE ERROR Can be used with any arithmetic verb Include scope terminator (e.g., END-ADD)

50 Chapter Summary Intrinsic functions added as COBOL extensions in 1989
Calendar Numerical analysis Statistical Trigonometric Financial Character and String


Download ppt "Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions"

Similar presentations


Ads by Google