In this section: |
How to: |
Reference: |
A numeric expression performs a calculation that uses numeric constants, variables, operators, or functions to return a numeric value. A numeric expression can consist of the following components, as highlighted below:
COMPUTE COUNT/I2 = 1 ;
COMPUTE RECOUNT/I2 = Count ;
COMPUTE BONUS/D12.2 = CURR_SAL * 0.05 ;
COMPUTE LONGEST_SIDE/D12.2 = MAX(WIDTH, HEIGHT) ;
COMPUTE PROFIT/D12.2 = (RETAIL_PRICE - UNIT_COST) * UNIT_SOLD ;
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.
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
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;
The MOD operator can be used in any valid 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.
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
Maintain Data performs numeric operations in the following order:
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.
Maintain Data 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 Data 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.
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-precision 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. |
Because the following variables are defined as integers,
COMPUTE OperandOne/I4; OperandTwo/I4; Result/I4;
Maintain Data does the following multiplication in native-mode arithmetic (integer arithmetic):
COMPUTE Result = OperandOne * OperandTwo;
If operands of a numeric operator have different formats, you can use the following table to determine what the common format is after Maintain Data converts them. Maintain Data 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 Data 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 Data truncates the fractional value.
By default, you must use a decimal point (.) to indicate a decimal position when writing a value in a Maintain Data procedure (for example, a COMPUTE statement), and a comma (,) 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 quotation marks (') or double quotation marks (") when CDN=ON or SPACE. However, you must use double quotation marks (") when CDN=QUOTE or QUOTEP. The MNTCON CDN_FEXINPUT command does not apply to values entered in Maintain Data 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.