Arithmetic expressions compute numbers: gross plus bonus, tax as a percentage of pay, annual factor raised to a power, net after deductions. Easytrieve uses familiar infix operators—plus, minus, asterisk for multiply, slash for divide, double asterisk for exponentiation—between fields and numeric literals. Results land in a target variable on assignment, optionally modified by ROUNDED or TRUNCATED keywords that control how extra decimal positions fit the receiving field. Mainframe payroll and billing logic runs on packed decimal P fields with implied decimals; beginners must respect decimal alignment when adding GROSS P 2 to BONUS P 2 and storing into TOTAL P 2. Invalid packed data from a bad FILE READ still parses as an expression at compile time but explodes at runtime with data exception. This page teaches each operator, precedence with other operators, assignment keywords, remainder patterns, mixed-type behavior, and defensive coding before arithmetic on production extract fields.
| Operator | Name | Example | Notes |
|---|---|---|---|
| + | Addition | TOTAL = A + B | Sum operands; align implied decimals on P fields |
| - | Subtraction | NET = GROSS - TAX | Can yield negative if target field supports sign |
| * | Multiplication | BONUS = GROSS * 0.10 | Common for percentages; watch result field size |
| / | Division | AVG = TOTAL / COUNT | Guard divisor zero; result precision may exceed target |
| ** | Exponentiation | PWR = BASE ** EXP | Less common in batch; verify range fits target |
Arithmetic expressions appear on the right side of assignment: TARGET = expression. The target must be a defined field—usually W, S, or FILE output buffer—large enough to hold the result range. Easytrieve evaluates the expression, applies conversion rules, then stores into the target bytes. You can chain assignments but not chain arithmetic without intermediate targets: NET = GROSS - TAX = BONUS is invalid; compute stepwise or use parentheses in one expression when grammar allows.
123NET-PAY = GROSS - FED-TAX - STATE-TAX - FICA BONUS = GROSS * 0.05 HOLD = (REG-HRS + OT-HRS) * RATE
Plus and minus combine two numeric operands. Adding packed amounts with two decimal positions keeps hundredths aligned internally—GROSS P 2 plus BONUS P 2 yields sum with two implied decimals when stored into TOTAL P 2. Subtracting larger from smaller on unsigned field definitions may produce unexpected sign nibble patterns—use signed quantitative fields when negative results are valid business outcomes. FILE fields participate in arithmetic after READ loads bytes; validate NUMERIC before trusting FILE packed content.
12345IF GROSS NUMERIC AND BONUS NUMERIC TOTAL = GROSS + BONUS ELSE DISPLAY 'INVALID AMOUNT' ACCT END-IF
Multiplication scales values—percent of gross, hours times rate, quantity times unit price. Literal 0.05 represents five percent when multiplying gross pay. Result precision grows: multiplying two P 2 fields can need P 4 intermediate range before rounding into final P 2 target. If WS-HOLD is too short, overflow or truncation occurs per product rules. Use adequately sized WORK field for intermediate products in complex tax calculations.
123DEFINE WS-PROD W 11 P 4 WS-PROD = GROSS * TAX-RATE TAX-AMT = WS-PROD ROUNDED
Division computes quotient of dividend and divisor. Average pay TOTAL / COUNT requires COUNT non-zero—test IF COUNT GT 0 before assignment. Integer-style division behavior depends on field types and decimal positions; financial averages usually need ROUNDED into P 2 target. Division is the operator most likely to cause runtime abend when divisor is zero or when packed divisor holds invalid sign nibble from corrupt input file.
12345IF REC-COUNT GT 0 AVG-GROSS = GRAND-TOTAL / REC-COUNT ROUNDED ELSE AVG-GROSS = 0 END-IF
Double asterisk raises left operand to right operand power. BASE ** 2 squares a value; FACTOR ** 3 cubes it. Exponentiation appears rarely in classic batch reports compared to add and multiply tax logic, but appears in interest or statistical jobs. Ensure target field accommodates result magnitude—large exponents overflow fixed decimal fields quickly. See dedicated exponentiation operator page for edge cases.
ROUNDED keyword on assignment applies rounding to fit target decimal positions—typical for currency to two decimal places. TRUNCATED discards excess fraction without rounding up—some legacy billing rules mirror COBOL truncation behavior. Omitting both stores per default conversion rules which may differ from explicit ROUNDED. Payroll shops often standardize ROUNDED on all currency assignment for auditor consistency.
12TAX-AMT = GROSS * TAX-RATE ROUNDED FEE-AMT = AMOUNT * FEE-PCT TRUNCATED
Easytrieve documents MOD operator for remainder after division in operator reference. When MOD is unavailable in your syntax subset, remainder can be computed with arithmetic identity: REMAINDER = DIVIDEND - (DIVIDEND / DIVISOR) * DIVISOR with appropriate rounding discipline—see MOD operator page. Modulo logic supports leap-year checks, batch slot assignment, and cyclic counters in maintenance utilities.
Unary minus negates a numeric value: BALANCE = -ADJUST when adjustment direction is stored positive. Unary plus is rarely written explicitly. Sign handling on packed fields uses hardware decimal instructions—invalid sign on FILE data causes S0C7 during unary or binary operations on that field.
Fields must be numeric types for quantitative arithmetic—N, P, B, U, I with appropriate definitions. Numeric literals up to 18 digits participate without DEFINE. Alphabetic literals do not belong in arithmetic except in specialized conversion routines. System constant ZERO acts as numeric zero operand in expressions and MOVE contexts.
Zoned N and packed P convert per assignment rules when combined—the product promotes to representation suitable for computation then stores in target type. Binary B fields participate in integer-style arithmetic. Floating U and I appear in scientific or interface programs less common in report generator batch. Mismatched implied decimals between operands still compute but storing into wrong-sized target loses precision—size WORK fields generously for intermediates.
Mixed expressions compare arithmetic results: IF GROSS * 0.10 GT LIMIT. Multiplication binds before comparison GT per precedence rules. Parentheses clarify: IF (GROSS - DEDUCT) LT 0 detects negative net. See mixed expressions and order of evaluation pages for connector precedence with logical operators.
123IF (GROSS - TOTAL-DEDUCT) LT 0 DISPLAY 'NEGATIVE NET' EMPL-NUM END-IF
SUM on REPORT totals numeric fields automatically when fields are quantitative. Arithmetic in JOB assigns values before PRINT; SUM accumulates during report processing. Do not confuse assignment expression TOTAL = A + B with report SUM statement—they serve different phases. MASK on LINE formats display of numeric results after arithmetic storage.
Arithmetic expressions are math problems inside your instructions. Plus puts marbles together. Minus takes marbles away. Times makes a pile bigger by a number of copies. Divide shares marbles into equal groups— but you cannot divide by zero groups. Double-star power means multiply a number by itself several times. ROUNDED is like asking the teacher to round your answer to two decimal places. TRUNCATED is chopping off extra digits without rounding up.
1. Arithmetic expression BONUS = GROSS * 0.05 uses operator:
2. ROUNDED on assignment affects:
3. Exponentiation in Easytrieve uses operator:
4. Packed decimal fields in arithmetic require:
5. TRUNCATED on assignment: