CALC


The CALC statement allows you to perform complex calculations in a single statement (refer to the COMPUTE statement for simpler calculations involving only two fields or a field and a constant). The result of the calculation is placed in any of the index predefined fields, A to Z or AI to ZI.

    ••••• CALC     •• = •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
    (1)            (2)  (3)

(1) T/F execution conditions

(2) Receiver index PDF (A to Z, AI to ZI)

(3) Algebraic computation (59 characters max)

Using the Statement

The algebraic computation can contain index predefined fields A to Z and AI to ZI, numeric constants, operators (+, -, *, and /), and any number of levels of parentheses. The CALC statement performs operations according to the standard algebraic rules of precedence:

·    Unary negation (-) (i.e., reversing the signs on numbers),

·    Then, for the innermost pair of parentheses:

·    Multiplication (*) and division (/), which have precedence over

·    Addition (+) and subtraction (-)

·    Then, proceed to each succeeding layer of parentheses, if applicable.

Precision for the floating-point variables A to Z is a maximum of 15 total decimal positions. Precision for the integer variables AI to ZI is a maximum of 10 total decimal positions.

Standard Math Functions

APPX supports the use of several standard math functions in the CALC statement. To use one of these functions, specify the function name, followed by its arguments in parentheses. For instance:

          CALC     A  = @ABS(B)

In this case, the absolute value of B (the argument) is assigned to A.

The function arguments can be expressions that may contain other functions. For instance, the following CALC statements are legal:

          CALC     A  = @POW(B,1/3)

... takes the cube root of B

          CALC     A  = @SQRT(@POW(B-C,2)+@POW(D-E,2))

... computes the absolute distance between two Cartesian points

APPX does not check for illegal argument values (such as taking the square root of a negative number). In general, you should check arguments for validity before using them in certain functions. If an error occurs, standard OVERFLOW processing takes place (you can tell where an error occurred, but not what caused it). The math functions fall into three groups described in corresponding sections: basic functions, exponential functions, and trigonometric functions.

Restrictions

The fields referenced in a CALC statement are restricted to the index predefined fields (A to Z and AI to ZI). Therefore, you frequently precede a CALC statement with one or more SET statements to move values into such fields, as in the following example:

          *
          *        A = Unit price
          *        B = Quantity ordered
          *        C = Extension
          *
          SET      --- A                          =  TOE ORDER UNIT PRICE
          SET      --- B                          =  TOE ORDER QUANTITY

          CALC     C = A*B
          SET      TOE ORDER AMOUNT               =  --- C

Note the use of comments to clarify the variable names used in the calculation. You may find it helpful to include comments whenever predefined variable names are used.

Compilation Errors

If the algebraic expression is invalid, a compilation error occurs. Invalid algebraic expressions include those with too few operands per operator and those with unbalanced parentheses.

Execution Errors

A runtime error can occur if an intermediate or final calculation results in overflow or division by zero.

Related Statements

COMPUTE, SET, OVERFLOW

Example

The following example demonstrates a calculation involving three index variables (B, C, and DI). The results are then placed in the receiver index variable (AI).

          **       The following statements calculate the total estimated monthly
          *        compensation (AI) for a salesperson. This formula includes a
          *        fixed monthly base salary ($1,800), an annual bonus (B), and a
          *        commission based on the number of units sold (DI) over 100
          *        times the average sale (C) less $50 per unit. The bonus and
          *        comission are annualized so they need to be divided by 12
          *        to derive a monthly amount.
          *
          SET      --- B                          =  TSA SALES ANNUAL BONUS
          SET      --- C                          =  TSA SALES AVG UNIT SALE
          SET      --- DI                         =  TSA SALES NO UNITS SOLD
 
         CALC     AI = 1800 + (B + (C - 50) * (DI - 100) ) / 12
          SET      TSA SALES MONTHLY SALARY       =  --- AI

In this example, the constant 50 is subtracted from the predefined field C, and the constant 100 is subtracted from the predefined field DI, before the two intermediate results are multiplied together. Next, B is added to the result of the multiplication, and that result is divided by 12. Finally, that result is added to 1800, rounded to an integer, and stored in AI.

In this example, note that only the predefined field AI is affected. The fields B, C, and DI remain unaltered.