Transcript Chapter 7

Chapter 7
7-1





Basic Arithmetic Verbs
Options Available with Arithmetic Verbs
COMPUTE Statement
Signed Numbers in Arithmetic Operations
Intrinsic Functions
7-2


ADD, SUBTRACT, MULTIPLY, DIVIDE
All require fields operated on to
◦ Have numeric PICTURE clauses
◦ Contain numeric data when statements executed
7-3
Format 1
ADD


identifier-1
literal-1
… TO identifier-2 ...
identifier-1 or literal-1 added to identifier-2
Result stored in identifier-2
7-4
Assume X, Y and Z are numeric fields
X = 5, Y = 3 and Z = 7
ADD Statement
Add X To Y
Add X, 9 To Y
Add X, 6, Y To Z


Result
Y=8
Y = 17
Z = 21
Identifiers preceding TO are unchanged
Value of identifier after TO
◦ Used in ADD operation
◦ Original value replaced with ADD result
7-5
Format 2
ADD


identifier-1
literal-1
… GIVING identifier-2 ...
Identifiers and literals preceding GIVING
added together
Result stored in identifier-2
7-6
Assume X, Y and Z are numeric fields
X = 5, Y = 3 and Z = 7
ADD Statement
Add X, Y Giving Z
Add X, 10 Giving Y
Add X, 4, Y Giving Z


Result
Z=8
Y = 15
Z = 12
Identifiers preceding GIVING are unchanged
Value of identifier after GIVING
◦ Original value replaced with ADD result
◦ May be report-item with edit symbols
7-7


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
Add X To Y, Z
Add X, 6 Giving Y, Z
Result
Y = 8, Z = 12
Y = 11, Z = 11
7-8

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
7-9
Format 1
SUBTRACT
...


identifier-1
… FROM identifier-2
literal-1
identifier-1 or literal-1 subtracted from
identifier-2
Result stored in identifier-2
710
Assume A, B and C are numeric fields
A = 6, B = 2 and C = 18
SUBTRACT Statement
Subtract A From C
Subtract B, 5 From C
Subtract B From A, C
Result
C = 12
C = 11
A = 4, C = 16
711
Format 2
SUBTRACT
identifier-1
… FROM
literal-1
identifier-2
literal-2
GIVING identifier-3 ...


identifier-1 or literal-1 subtracted from
identifier-2 or literal-2
Result stored in identifier-3
712
Assume A, B and C are numeric fields
A = 6, B = 2 and C = 18
SUBTRACT Statement
Subtract B From A Giving C
Subtract A From 15 Giving C
Subtract A, 4 From C Giving B
Result
C=4
C=9
B=8
713
Format 1
MULTIPLY


identifier-1
literal-1
BY identifier-2 ...
identifier-1 or literal-1 multiplied by
identifier-2
Result stored in identifier-2
714
Assume Q, R and S are numeric fields
Q = 4, R = 7 and S = 5
MULTIPLY Statement
Multiply Q By R
Multiply 10 By S
Multiply 2 By R, S
Result
R = 28
S = 50
R = 14, S = 10
715
Format 2
MULTIPLY
identifier-1
literal-1
BY
identifier-2
literal-2
GIVING identifier-3 ...


identifier-1 or literal-1 multiplied by
identifier-2 or literal-2
Result stored in identifier-3
716
Assume Q, R and S are numeric fields
Q = 4, R = 7 and S = 5
MULTIPLY Statement
Multiply Q By R Giving S
Multiply Q By 3 Giving S
Multiply 6 By Q Giving R, S
Result
S = 28
S = 12
R = 24
S = 24
717


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
718
Format 1
DIVIDE


identifier-1
literal-1
INTO identifier-2 ...
identifier-1 or literal-1 divided into identifier-2
Result stored in identifier-2
719
Assume X, Y and Z are numeric fields
X = 2, Y = 12 and Z = 8
DIVIDE Statement
Divide X Into Y
Divide 3 Into Y
Divide 2 Into Y, Z
Result
Y=6
Y=4
Y = 6, Z = 4
720
Format 2
DIVIDE
identifier-1
literal-1
INTO
BY
identifier-2
literal-2
GIVING identifier-3 ...


identifier-1 or literal-1 divided into or by
identifier-2 or literal-2
Result stored in identifier-3
721
Assume X, Y and Z are numeric fields
X = 2, Y = 12 and Z = 8
DIVIDE Statement
Divide X Into Y Giving Z
Divide Y By X Giving Z
Divide 16 By Z Giving X, Y
Result
Z=6
Z=6
X=2
Y=2
722


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???
Same as MOD
723

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
◦ Its actually adding .5 then truncating
724
Examples
01
01
Amt1Pic 9V99 Value 2.25.
Amt2Pic 999.
Arithmetic statement
Multiply .3 By Amt1
Multiply .3 By Amt1 Rounded
Divide 150 By 9
Giving Amt2 Rounded
Result
.675
.675
Value Stored
Amt1 = 0^67
Amt1 = 0^68
16.66… Amt2 = 017
725




Occurs when result value too large to be
stored in result field
Result of this ADD statement is 1,075
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
726


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
727
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
728



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
729


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
730

General arithmetic statement using symbols
in place of arithmetic verbs
Symbol
Verb
+
ADD
SUBTRACT
*
MULTIPLY
/
DIVIDE
**
exponentiation
731
Format
arithmetic-exp-1
COMPUTE identifier-1 … = literal-1
identifier-2

Identifier to left of equal sign set to value of
arithmetic-expression, literal or identifier on
right of equal sign
732
Assume X, Y and Z are numeric fields
X = 9, Y = 4 and Z = 12
COMPUTE Statement
Compute Z = X * Y
Compute X = Z - Y + 2
Compute X = Y
Compute Z = Y ** 2
Result
Z = 36
X = 10
X=4
Z = 16
733


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
734
Assume X, Y and Z are numeric fields
X = 6, Y = 18 and Z = 5
COMPUTE Statement
Compute Z = Y / X + 3
Compute Z = Y / (X + 3)
Compute Y = Z + X * 10
Compute Y = Z * X / 10
Result
Z=6
Z=2
Y = 65
Y=3
735



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 ENDCOMPUTE
736

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
737

Built-in procedures to perform particular task
like
◦ Find square root of number
◦ Convert letters to uppercase
◦ Get current date

Looks like our compiler handles these
because of Lab 2 with the NUMVAL function
738
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
739
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
740




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
741

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
742


COMPUTE used for any combination of
arithmetic operations
Order of evaluation of operators
1.
2.
3.
4.
**
* or / in sequence left to right
+ or - in sequence left to right
( ) override normal hierarchy rules
743


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)
744

Intrinsic functions added as COBOL
extensions in 1989
◦
◦
◦
◦
◦
◦
Calendar
Numerical analysis
Statistical
Trigonometric
Financial
Character and String
745