Compute

Syntax

COMPUTE variable expression [ ROUNDED ] [ GS status ]

Parameters

Compute can be abbreviated as COMP.

Description

The Compute logic command calculates a free-form expression involving arithmetic operations and variables. Calculations are performed in standard mathematical precedence for arithmetic operators, and from left to right.

The behavior of the Compute logic command can be replicated using arithmetic operators. Use of arithmetic operators, where possible, rather than arithmetic logic commands, is the LDL+ preferred format.

Failure behavior

The Glb.Status built-in segment attribute is not set. Use the GS command option to test for division by zero or arithmetic overflow. If either occurs, the result variable is set to zero. Include logic to test that the value of the divisor is not zero before the Compute logic command is executed.

Restrictions

If the result variable does not allow sufficient decimal places for precision, use the Rounded command option. Otherwise, the result is truncated rather than rounded. If the Rounded command option is not used and truncation occurs, the variable specified by the GS command option, if invoked, is set to "*****".

Example

This example calculates the total price and tax price.

Amount := 19.95
Quantity := 51
TaxRate := 0.35
Compute SD_Total (Amount * Quantity)
     : SD_Total is now 1017.45
Compute SD_Tax ((Amount * Quantity) * TaxRate) GS SD_Status
     : SD_Tax is now 356.10 (356.1075 truncated to 2 decimal places)
Compute SD_Tax ((Amount * Quantity) * TaxRate) Rounded
     : SD_Tax is now 356.11 (356.1075 rounded to 2 decimal places)
If SD_Status = "*****"
     Message SD_Tax "arithmetic overflow"
EndExit