RPG/400 Help
The arithmetic operations are:
For examples of arithmetic operations, see Figure
"Summary of Arithmetic Operations".
Remember the following when specifying arithmetic operations:
- Arithmetic operations can be done only on numeric fields (including
numeric subfields, numeric arrays, numeric array elements, numeric table
elements, numeric named constants, numeric figurative constants, and numeric
literals).
- Arithmetic operations are done on data in packed decimal format.
Data maintained in other formats is converted to or from packed decimal
format.
- Decimal alignment is done for all arithmetic operations. Even
though truncation can occur, the position of the decimal point in the result
field is not affected.
- An arithmetic operation does not change factor 1 and factor 2 unless they
are the same as the result field.
- Any data placed in the result field replaces the data that was
there.
- The result field must be large enough to accommodate the results of the
arithmetic operation because an RPG/400 program does not cause an error on an
arithmetic overflow. If the result field is not large enough to
accommodate the results, digits are dropped from either or both ends,
depending on the location of the decimal point.
- If you use conditioning indicators, it is your responsibility to ensure
that the DIV operation occurs immediately before the MVR operation. If
the MVR operation occurs before the DIV operation, undesirable results
occur. An error message may be issued.
- Half adjust (position 53) can be specified for all the
arithmetic operations except for the MVR operation or for a DIV operation
immediately followed by an MVR operation.
For arithmetic operations in which all three fields are used:
- Factor 1, factor 2, and the result field can be three different
fields.
- Factor 1, factor 2, and the result field can all be the same field.
- Factor 1 and factor 2 can be the same field but different from the result
field.
- Either factor 1 or factor 2 can be the same as the result field.
The length of any field specified in an arithmetic operation cannot exceed
30 digits. If the result exceeds 30 digits, digits are dropped from
either or both ends, depending on the location of the decimal point.
All arithmetic operations are done algebraically.
The results of all operations are signed (a plus sign is a hexadecimal F
and a minus sign is a hexadecimal D) according to the following rules.
The sign is in the zone portion of the low-order byte.
For information on using arrays with arithmetic operations, see Specifying an Array in Calculations.
Addition: If factor 1 and factor 2 have like signs, the
result field sign is the same. If factor 1 and factor 2 have unlike
signs, the result field sign is the same as the sign of the factor with the
larger absolute value.
Subtraction: Change the sign of factor 2, and apply the
rules for addition.
Multiplication: If factor 1 and factor 2 have like signs,
the result field sign is plus (+). If factor 1 and factor 2 have
unlike signs, the result field sign is minus (-).
Division: If factor 1 and factor 2 have like signs, the
result field sign is plus (+). If factor 1 and factor 2 have
unlike signs, the result field sign is minus (-). The sign of the
remainder is the same as the factor 1 sign.
For the ADD, SUB, MULT, and DIV operations, factor 1 is not
required. If factor 1 is not specified, the operation is done as though
factor 1 and the result field were the same field.
[ Top of Page | Previous Page | Next Page | Table of Contents ]
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.