Writing Numeric Expressions
 In this section: How to:Reference: Numeric Operators

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:

• A numeric constant. For example:
`COMPUTE COUNT/I2 = 1 ;`
• A numeric variable. For example:
`COMPUTE RECOUNT/I2 = Count ;`
• Two numeric constants or variables joined by a numeric operator. For example:
```COMPUTE BONUS/D12.2 = CURR_SAL
* 0.05 ;```
• A numeric function. For example:
```COMPUTE LONGEST_SIDE/D12.2 = MAX
(WIDTH, HEIGHT) ;```
• Two or more numeric expressions joined by a numeric operator. For example:
```COMPUTE PROFIT/D12.2 = (RETAIL_PRICE
- UNIT_COST) * UNIT_SOLD ;```
 Top of page
 Top of page
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`
 Top of page
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
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.

 Top of page
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
Order of Evaluation

Maintain performs numeric operations in the following order:

1. Exponentiation.
2. Division and multiplication.

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
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
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

`+`

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.

 Top of page
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
 Top of page
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.