ILE COBOL Programmer's Guide

Numeric Intrinsic Functions

Intrinsic functions can return an alphanumeric, DBCS, numeric, boolean or date-time value.

Numeric intrinsic functions:

Types of Numeric Functions

Numeric functions are classified into these categories:

Integer
Those that return an integer.

Floating-Point
Those that return a long floating-point value.

Argument Dependent
Return type depends on the arguments specified

The numeric functions available in ILE COBOL under these categories are listed in Table 8

Table 8. Types of Data that Numeric Functions Return

Integer Floating-point Argument Dependent
DATE-OF-INTEGER ACOS MAX *
DATE-TO-YYYYMMDD ANNUITY MIN *
DAY-OF-INTEGER ASIN RANGE
DAY-TO-YYYYDDD ATAN SUM
EXTRACT-DATE-TIME COS
FACTORIAL LOG
FIND-DURATION LOG10
INTEGER MEAN
INTEGER-OF-DATE MEDIAN
INTEGER-OF-DAY MIDRANGE
INTEGER-PART NUMVAL
LENGTH NUMVAL-C
MOD PRESENT-VALUE
ORD RANDOM
ORD-MAX REM
ORD-MIN SIN
YEAR-TO-YYYY SQRT

STANDARD-DEVIATION

TAN

VARIANCE
Note:
* MAX and MIN can be alphanumeric.

Nesting Functions and Arithmetic Expressions

Numeric functions can be nested; you can reference one function as the argument of another. A nested function is evaluated independently of the outer function.

Because numeric functions and arithmetic expressions hold similar syntactic status, you can also nest an arithmetic expression as an argument to a numeric function:

COMPUTE X = FUNCTION MEAN (A, B, C / D).

In this example, there are only three function arguments: A, B and the arithmetic expression (C / D).

All Subscripting and Special Registers

Two other useful features of Intrinsic Functions are the ALL subscript and special registers.

You can reference all the elements of an array as function arguments by using the ALL subscript. This feature is used with tables.

The integer-type special registers are allowed as arguments wherever integer arguments are allowed.

Intrinsic Function Examples

You can use Intrinsic Functions to perform several different kinds of arithmetic as outlined in the table below:

Table 9. Types of Arithmetic that Numeric Functions Handle

Number Handling Date/Time Finance Mathematics Statistics
LENGTH
MAX
MIN
NUMVAL
NUMVAL-C
ORD-MAX
ORD-MIN
CURRENT-DATE
DATE-OF-INTEGER
DAY-TO-YYYYDDD
DATE-TO-YYYYMM
DD
DAY-OF-INTEGER
EXTRACT-DATE-
TIME
FIND-DURATION
INTEGER-OF-DATE
INTEGER-OF-DAY
WHEN-COMPILED
YEAR-TO-YYYY
ANNUITY
PRESENT
-VALUE
ACOS
ASIN
ATAN
COS
FACTORIAL
INTEGER
INTEGER-PART
LOG
LOG10
MOD
REM
SIN
SQRT
SUM
TAN
MEAN
MEDIAN
MIDRANGE
RANDOM
RANGE
STANDARD
-DEVIATION
VARIANCE

The following examples and accompanying explanations show intrinsic functions in each of the categories listed in the preceding table.

General Number-Handling

Suppose you want to find the mean value of three prices (represented as alphanumeric items with dollar signs), put this value into a numeric field in an output record, and determine the length of the output record. You could use NUMVAL-C (a function that returns the numeric value of an alphanumeric string) and the MEAN function to do this:

01 X                 PIC 9(2).
01 PRICE1            PIC X(8)     VALUE "$8000".
01 PRICE2            PIC X(8)     VALUE "$4000".
01 PRICE3            PIC X(8)     VALUE "$6000".
01 OUTPUT-RECORD.
   05 PRODUCT-NAME   PIC X(20).
   05 PRODUCT-NUMBER PIC 9(9).
   05 PRODUCT-PRICE  PIC 9(6).
.
.
.
PROCEDURE DIVISION.
   COMPUTE PRODUCT-PRICE =
       FUNCTION MEAN (FUNCTION NUMVAL-C(PRICE1)
                      FUNCTION NUMVAL-C(PRICE2)
                      FUNCTION NUMVAL-C(PRICE3)).
       COMPUTE X = FUNCTION LENGTH(OUTPUT-RECORD).

Additionally, to ensure that the contents in PRODUCT-NAME are in uppercase letters, you could use the following statement:

    MOVE FUNCTION UPPER-CASE(PRODUCT-NAME) TO PRODUCT-NAME.

Date and Time

The following example shows how to calculate a due date that is 90 days from today. The first eight characters returned by the CURRENT-DATE function represent the date in a 4-digit year, 2-digit month, and 2-digit day format (YYYYMMDD). In the example, this date is converted to its integer value. Then 90 is added to this value, and the integer is converted back to the YYYYMMDD format.

01 YYYYMMDD              PIC 9(8).
01 INTEGER-FORM          PIC S9(9).
   .
   .
   .
   MOVE FUNCTION CURRENT-DATE(1:8) TO YYYYMMDD.
   COMPUTE INTEGER-FORM = FUNCTION INTEGER-OF-DATE(YYYYMMDD).
   ADD 90 TO INTEGER-FORM.
   COMPUTE YYYYMMDD = FUNCTION DATE-OF-INTEGER(INTEGER-FORM).
   DISPLAY 'Due Date: ' YYYYMMDD. 

You can also calculate a due date as a category date-time data item. For an example of this type of calculation, refer to Example of Calculating a Due Date.

Finance

Business investment decisions frequently require computing the present value of expected future cash inflows to evaluate the profitability of a planned investment. The present value of money is its value today. The present value of an amount that you expect to receive at a given time in the future is that amount which, if invested today at a given interest rate, would accumulate to that future amount.

For example, assume a proposed investment of $1, 000 produces a payment stream of $100, $200, and $300 over the next three years, one payment per year respectively. The following ILE COBOL statements show how to calculate the present value of those cash inflows at a 10% interest rate.

01 SERIES-AMT1               PIC 9(9)V99     VALUE 100.
01 SERIES-AMT2               PIC 9(9)V99     VALUE 200.
01 SERIES-AMT3               PIC 9(9)V99     VALUE 300.
01 DISCOUNT-RATE             PIC S9(2)V9(6)  VALUE .10.
01 TODAYS-VALUE              PIC 9(9)V99.
.
.
.
COMPUTE TODAYS-VALUE =
FUNCTION
PRESENT-VALUE(DISCOUNT-RATE SERIES-AMT1 SERIES-AMT2
SERIES-AMT3).

The ANNUITY function can be used in business problems that require you to determine the amount of an installment payment (annuity) necessary to repay the principal and interest of a loan. The series of payments is characterized by an equal amount each period, periods of equal length, and an equal interest rate each period. The following example shows how you could calculate the monthly payment required to repay a $15,000 loan at 12% annual interest in three years (36 monthly payments, interest per month = .12/12):

01 LOAN                PIC 9(9)V99.
01 PAYMENT             PIC 9(9)V99.
01 INTEREST            PIC 9(9)V99.
01 NUMBER-PERIODS      PIC 99.
.
.
.
COMPUTE LOAN = 15000.
COMPUTE INTEREST = .12
COMPUTE NUMBER-PERIODS = 36.
COMPUTE PAYMENT =
LOAN * FUNCTION ANNUITY((INTEREST / 12) NUMBER-PERIODS).

Mathematics

The following ILE COBOL statement demonstrates how intrinsic functions can be nested, how arguments can be arithmetic expressions, and how previously complex mathematical calculations can be simply performed:

COMPUTE Z = FUNCTION LOG(FUNCTION SQRT (2 * X + 1))
+ FUNCTION REM(X 2)

Here, the remainder of dividing X by 2 is found with an intrinsic function, instead of using a DIVIDE statement with a REMAINDER clause.

Statistics

Intrinsic Functions also make calculating statistical information on data easier. Assume you are analyzing various city taxes and want to calculate the mean, median, and range (the difference between the maximum and minimum taxes):

O1 TAX-S               PIC 99V999 VALUE .045.
01 TAX-T               PIC 99V999 VALUE .02.
01 TAX-W               PIC 99V999 VALUE .035.
01 TAX-B               PIC 99V999 VALUE .03.
01 AVE-TAX             PIC 99V999.
01 MEAN-TAX            PIC 99V999.
01 TAX-RANGE           PIC 99V999.
   .
   .
   .
COMPUTE AVE-TAX = FUNCTION MEAN(TAX-S TAX-W TAX-B)
COMPUTE MEDIAN-TAX = FUNCTION MEDIAN(TAX-S TAX-W TAX-B)
COMPUTE TAX-RANGE = FUNCTION RANGE(TAX-S TAX-W TAX-B)


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]