ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Numerical Calculations → Statements for Numerical Calculations
COMPUTE
Other versions: 7.31 | 7.40 | 7.54
Syntax
[COMPUTE] [EXACT] result = arith_exp |
string_exp | bit_exp.
Addition
Effect
This statement assigns the result of a
calculation expression to the variable result
. The keyword COMPUTE
can be omitted. A calculation expression is:
The data type of result
must be either a
numeric data object, a
character-like data object, or a
byte-like data object.
If required, the result of the expression is converted to the data type of result
.
The following variants exist in addition to the main variant above:
-
[COMPUTE] result = dobj.
-
[COMPUTE] result ?= dobj.
If, instead of a calculation expression on the right, a single data object has been specified without a prefixed sign or bit operator BIT-NOT this is not a complete arithmetic expression or bit expression. The calculation with a subsequent assignment becomes a simple assignment. These two variants then function just likeMOVE dobj {TO|?TO} result
. There is then no shared calculation type into which the operands are converted before the assignment; instead, the source field is converted into the data type of the target field, if required.EXACT
, however, always functions like a calculation. -
[COMPUTE] destination1 = destination2 = ... = result = arith_exp|string_exp|bit_exp|dobj.
In this variant, the variableresult
is assigned a value in accordance with the rules of the statementCOMPUTE
;destination1
,destination2
, ... are given values in accordance with multiple assignment rules.
Programming Guideline
Notes
-
Without the
EXACT
addition, the right side of the statementCOMPUTE
is a general expression position. -
In addition to numeric calculations with an
arithmetic expression, this means that the statement
COMPUTE
also permits character string and bit operations with a string expression or a bit expression, which is why they are grouped together under calculation expression. -
If, on the right side, only a predefined function with a single data object is specified, the
COMPUTE statement without the
EXACT
addition behaves like theMOVE
statement. If the argument in the function is a calculation expression, the entire right-hand side is a calculation expression.
Example
The first assignment is the same as a COMPUTE
statement and the value returned
in result
is "731036", or the number of days since 01.01.0001. The second
assignment, however, is the same as a MOVE
statement and the value returned in result
is "20020704".
DATA: result TYPE string,
date TYPE d VALUE '20020704'.
result = + date.
result = date.
Addition
... EXACT
Effect
The EXACT
addition, which cannot currently be specified without COMPUTE
, checks whether a
lossless calculation (in which no rounding is permitted) takes place. A simplified
arithmetic expression can be specified on the right side. The
calculation type is
always decfloat34
. If rounding is required by the statement, the exception
CX_SY_CONVERSION_ROUNDING is raised. Rounding can take place when operands are converted to the calculation type, during the calculation, or when the result is converted to the target field.
The exception is raised when the first subexpression that requires a rounding is calculated. The target
variable result
remains unchanged when the exception is raised. When the
exception is handled, the VALUE attribute in the exception object contains the result that a calculation
returns without the EXACT
addition. The attribute OPERATION contains the first operation that had to be rounded; conversions are represented by ":=".
The following restrictions apply to the simplified arithmetic expression on the right, as opposed to general arithmetic expressions:
-
The permitted arithmetic
operators are
+
,-
,*
, and/
. The operatorsDIV
,MOD
, and**
cannot be used. -
The permitted predefined
functions are
abs
,sign
,ceil
,floor
,trunc
,frac
,round
, andrescale
. These functions do not raise the exception CX_SY_CONVERSION_ROUNDING. No other predefined functions can be specified because their results are not lossless (in principle). -
No operands of data type
x
andxstring
can be specified. - No functional methods can be specified, since the losslessness requirements cannot be applied to the method execution.
-
Operands of the type
f
(binary floating point numbers) are not permitted because they cannot represent every decimal number precisely, due to their internal representation.
The target variable result
can have any elementary data type except f
.
Note
The first calculation in lossless calculations that requires rounding raises an exception. This means that arithmetic expressions that are mathematically identical but constructed in different ways can display different behavior (see example).
Example
Example
Refer to Decimal Floating Point Numbers, Arithmetic Calculations.
Exceptions
Catchable Exceptions
CX_SY_CONVERSION_ROUNDING
-
Cause: Rounding in a lossless calculation
Runtime Error:UNCAUGHT_EXCEPTION