Writing Numeric Expressions

In this section:

How to:

Reference:

A numeric expression performs a calculation that uses numeric constants, variables, operators, or functions to return a number. A numeric expression can consist of the following components, as highlighted below:


Top of page

x
Reference: Numeric Operators

The following list shows the numeric operators you can use in an expression:

Operation

Operator

Addition

+

Subtraction

-

Multiplication

*

Division

/

Integer division

DIV

Remainder division

MOD

Exponentiation

**

Note: Multiplication, DIV, MOD, and exponentiation are not supported for date expressions of any type. To isolate part of a date, use a simple assignment command.



x
Syntax: How to Use DIV: Integer Division

The DIV operator can be used in any valid expression to perform integer division. The result is an integer value and the remainder is truncated.

The syntax is:

expression DIV expression


Example: Using DIV to Perform Integer Division

In this example, the DIV operator is used to calculate the number of whole days that are equivalent to a number of hours:

COMPUTE Days/I4 = Hours DIV 24;

Top of page

x
Syntax: How to Use MOD: Calculating the Remainder

The MOD operator can be used in any valid Maintain expression to calculate the remainder when division is performed.

The syntax is:

expression MOD divisor

The MOD operator always returns an integer value, and all decimal places are truncated.



Example: Using MOD to Calculate a Remainder

In the following example, the divisor is 10. The variables IntMod and DblMod contain the result.

MAINTAIN FILE Car
FOR 4 NEXT Country MPG INTO StkCar
REPEAT StkCar.FocCount Cnt/I4=1;
   COMPUTE IntMod/I4=StkCar(Cnt).MPG MOD 10;
             DblMod/D4.1=StkCar(Cnt).MPG MOD 10;
   TYPE "MPG=<<StkCar(Cnt).MPG" " IntMod=<<IntMod   DblMod=<<DblMod"
ENDREPEAT Cnt=Cnt+1;
END

The decimal place in the variable DblMod is truncated, even though the format is D4.1.

MPG=     16 INTMOD=   6 DBLMOD= 6.0
MPG=      9 INTMOD=   9 DBLMOD= 9.0
MPG=     11 INTMOD=   1 DBLMOD= 1.0
MPG=     25 INTMOD=   5 DBLMOD= 5.0

Top of page

x
Order of Evaluation

Maintain performs numeric operations in the following order:

  1. Exponentiation.
  2. Division and multiplication.
  3. Addition and subtraction.

When operators are at the same level, they are evaluated from left to right. Because expressions in parentheses are evaluated before any other expression, you can use parentheses to change this predefined order. For example, the following expressions yield different results because of parentheses:

COMPUTE PROFIT  =  RETAIL_PRICE  -  UNIT_COST  *  UNIT_SOLD ;
COMPUTE PROFIT  =  (RETAIL_PRICE  -  UNIT_COST)  *  UNIT_SOLD ;

In the first expression, UNIT_SOLD is first multiplied by UNIT_COST, and the result is subtracted from RETAIL_PRICE. In the second expression, UNIT_COST is first subtracted from RETAIL_PRICE, and that result is multiplied by UNIT_SOLD.


Top of page

x
Evaluating Numeric Expressions

Maintain follows a specific evaluation path for each numeric expression based on the format of the operands and the operators. If the operands all have the same format, most operations are carried out in that format. This is known as native-mode arithmetic. If the operands have different formats, Maintain converts the operands to a common format in a specific order of format precedence. Regardless of operand formats, some operators require conversion to specific formats so that all operands are in the appropriate format.


Top of page

x
Identical Operand Formats

If all operands of a numeric operator are of the same format, you can use the following table to determine whether or not the operations are performed in that native format or if the operands are converted before and after executing the operation. In each case requiring conversion, operands are converted to the operational format and the intermediate result is returned in the operational format. If the format of the result differs from the format of the target variable, the result is converted to the format of the target variable.

Operation

Operational Format

Addition

+

Native.

Subtraction

-

Native.

Multiplication

*

Native.

Full Division

/

Accepts single or double-precision floating point, converts all others to double-precision floating point.

Integer Division

DIV

Native, except converts packed decimal to double-precision floating point.

Remainder Division

MOD

Native, except converts packed decimal to double-precision floating point.

Exponentiation

**

Double-precision floating point.



Example: Identical Operand Formats

Because the following variables are defined as integers,

COMPUTE OperandOne/I4;
        OperandTwo/I4;
        Result/I4;

Maintain does the following multiplication in native-mode arithmetic (integer arithmetic):

COMPUTE Result = OperandOne * OperandTwo;

Top of page

x
Different Operand Formats

If operands of a numeric operator have different formats, you can use the following table to determine what the common format is after Maintain converts them. Maintain converts the lower operand to the format of the higher operand before performing the operation.

Order

Format

1

16-byte packed decimal

2

Double-precision floating point

3

8-byte packed decimal

4

Single-precision floating point

5

Integer

6

Character (alphanumeric and text)

For example, if a 16-byte packed-decimal operand is used in an expression, all other operands are converted to 16-byte packed-decimal format for evaluation. However, if an expression includes only integer and alphanumeric operands, all alphanumeric operands are converted to integer format.

Maintain converts the alphanumeric to a numeric. If the alphanumeric is not a number, it is converted to 0 (zero), and 0 (zero) gets substituted into the equation.

If you assign a decimal value to an integer, Maintain truncates the fractional value.



x
Continental Decimal Notation

By default, you must use a decimal point to indicate a decimal position when writing a value in a Maintain procedure (for example, a COMPUTE statement), and a comma if you wish to demarcate thousands, regardless of the CDN setting.

To write the value in a procedure using the format matching the CDN setting for a value other than OFF (for example, ON, QUOTE, QUOTEP, SPACE), use MNTCON CDN_FEXINPUT ON in the EDASPROF file or user profile, and use quotation marks to delimit the value. You can use single or double quotes when CDN=ON or SPACE. However, you must use double quotes when CDN=QUOTE or QUOTEP. The MNTCON CDN_FEXINPUT command does not apply to values entered in Maintain forms at run time.

For more information on setting MNTCON CDN_FEXINPUT, see MNTCON CDN_FEXINPUT.

When entering values in forms at run time, observe the following rule:

For more information on the SET CDN command, see the Developing Reporting Applications manual.


WebFOCUS