Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Numerical Calculations 

arith_exp - Arithmetic Expressions

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... [+|-] operand1 
    [{+|-||/|DIV|MOD|} [+|-] operand2
    [{+|-|
|/|DIV|MOD|**} [+|-] operand3
    ... ]] ... .

Effect

An arithmetic expression expresses a calculation. The result of an arithmetic expression is a numeric value of the calculation type assigned to the arithmetic expression. In an arithmetic expression arith_exp, the arithmetic operators +, -, , /, DIV, MOD, or * can be used to join an operand operand1 with one or more operands operand2, operand3 ... Brackets are also possible. Arithmetic expressions can be used in the statement COMPUTE or in read positions in certain statements.

The operand positions operand are general expression positions, which means that numeric data objects, predefined functions, functional methods, or compound arithmetic expressions can be specified. The arithmetic operators +, -, , /, DIV, MOD, and * join two adjacent operands. When the expression is evaluated, a numeric value is calculated and joined with the next adjacent operand. The priority of this join depends on the operators used.

Each operand can be preceded by the signs + or -, in any order and separated by one or more blanks. The effect of using a sign is the same as specifying the expression +1 * or -1 *, which means that a sign has the same priority as a multiplication.

If functional methods or character-like expressions are specified as operands, they are listed from left to right and from inside to outside before the remainder of the expression is evaluated. The return values are buffered to be used in the corresponding operand positions. Here, character-like processing functions and string expressions are only possible as arguments of description functions.


Notes

  • Note whether the value of a data object that is also used as an operand is changed in a specified functional method. Even if an operand of this type precedes the functional method, its value will always be changed by the method before it is evaluated.
  • Arithmetic expressions and string expressions cannot be mixed. Description functions that are used as operands for arithmetic expressions can, however, contain string expressions as arguments.

Example

The following program excerpt computes the hyperbolic sine with the Eulerian formula in calculation type decfloat34 and with the predefined function sinh in calculation type f and displays the difference.

DATA: x       TYPE i, 
      result1 TYPE decfloat16, 
      result2 TYPE f, 
      diff    TYPE decfloat16. 

DO 2001 TIMES. 
  TRY. 
      x = sy-index - 1001. 
      result1 = ( ( exp( x ) - exp( -1 * x ) ) / 2 ). 
      result2 = sinh( x ). 
      diff    = abs( result1 - result2 ). 
      IF diff <> 0. 
        WRITE: / x, diff STYLE cl_abap_math=>scientific. 
      ENDIF. 
    CATCH cx_sy_conversion_overflow cx_sy_arithmetic_overflow. 
  ENDTRY. 
ENDDO. 

Exceptions


Catchable Exceptions

CX_SY_ARG_OUT_OF_DOMAIN

  • Cause: Invalid call of acos
    Runtime Error: COMPUTE_ACOS_DOMAIN
  • Cause: Invalid call of asin
    Runtime Error: COMPUTE_ASIN_DOMAIN
  • Cause: Invalid call of cos
    Runtime Error: COMPUTE_COS_DOMAIN
  • Cause: Invalid call of log10
    Runtime Error: COMPUTE_LOG10_ERROR
  • Cause: Invalid call of log
    Runtime Error: COMPUTE_LOG_ERROR
  • Cause: Illegal argument in powers
    Runtime Error: COMPUTE_POW_DOMAIN
  • Cause: Invalid call of sin
    Runtime Error: COMPUTE_SIN_DOMAIN
  • Cause: Invalid call of sqrt
    Runtime Error: COMPUTE_SQRT_DOMAIN
  • Cause: Invalid call of tan
    Runtime Error: COMPUTE_TAN_DOMAIN

CX_SY_ARITHMETIC_OVERFLOW

  • Cause: Overflow in arithmetic operation (type p)
    Runtime Error: BCD_OVERFLOW
  • Cause: Overflow in arithmetic operation (all operands type p)
    Runtime Error: COMPUTE_BCD_OVERFLOW
  • Cause: Overflow in cosh
    Runtime Error: COMPUTE_COSH_OVERFLOW
  • Cause: Overflow or underflow in exp
    Runtime Error: COMPUTE_EXP_RANGE
  • Cause: Overflow in addition (type decfloat)
    Runtime Error: COMPUTE_DECFLOAT_ADD_OVERFLOW
  • Cause: Overflow in subtraction (type decfloat)
    Runtime Error: COMPUTE_DECFLOAT_SUB_OVERFLOW
  • Cause: Overflow in multiplication (type decfloat)
    Runtime Error: COMPUTE_DECFLOAT_MUL_OVERFLOW
  • Cause: Overflow in division (type decfloat)
    Runtime Error: COMPUTE_DECFLOAT_DIV_OVERFLOW
  • Cause: Overflow in division (type decfloat)
    Runtime Error: COMPUTE_DECFLOAT_DIV_OVERFLOW
  • Cause: Overflow in division (type f)
    Runtime Error: COMPUTE_FLOAT_DIV_OVERFLOW
  • Cause: Overflow in subtraction (type f)
    Runtime Error: COMPUTE_FLOAT_MINUS_OVERFLOW
  • Cause: Overflow in addition (type f)
    Runtime Error: COMPUTE_FLOAT_PLUS_OVERFLOW
  • Cause: Overflow in multiplication (type f)
    Runtime Error: COMPUTE_FLOAT_TIMES_OVERFLOW
  • Cause: Overflow in |-2147483648|
    Runtime Error: COMPUTE_INT_ABS_OVERFLOW
  • Cause: Integer overflow in division
    Runtime Error: COMPUTE_INT_DIV_OVERFLOW
  • Cause: Integer overflow after subtraction
    Runtime Error: COMPUTE_INT_MINUS_OVERFLOW
  • Cause: Integer overflow while adding
    Runtime Error: COMPUTE_INT_PLUS_OVERFLOW
  • Cause: Integer overflow in multiplication
    Runtime Error: COMPUTE_INT_TIMES_OVERFLOW
  • Cause: Invalid call of log10
    Runtime Error: COMPUTE_LOG10_ERROR
  • Cause: Invalid call of log
    Runtime Error: COMPUTE_LOG_ERROR
  • Cause: Overflow or underflow in powers
    Runtime Error: COMPUTE_POW_RANGE
  • Cause: Overflow in sinh
    Runtime Error: COMPUTE_SINH_OVERFLOW
  • Cause: Overflow in function round or rescale
    Runtime Error: COMPUTE_ROUND_RESCALE_OVERFLOW

CX_SY_CONVERSION_NO_NUMBER

  • Cause: Operand cannot be interpreted as a number
    Runtime Error: CONVT_NO_NUMBER

CX_SY_CONVERSION_OVERFLOW

  • Cause: Overflow in arithmetic operation (type p, with specified length)
    Runtime Error: BCD_FIELD_OVERFLOW
  • Cause: Operand too big or (interim) result too big
    Runtime Error: CONVT_OVERFLOW

CX_SY_PRECISION_LOSS

  • Cause: Result of cos not exact
    Runtime Error: COMPUTE_COS_LOSS
  • Cause: Result of sin not exact
    Runtime Error: COMPUTE_SIN_LOSS
  • Cause: Result of tan not exact
    Runtime Error: COMPUTE_TAN_LOSS

CX_SY_UNSUPPORTED_FUNCTION

  • Cause: Invalid function for calculation type decfloat34
    Runtime Error: COMPUTE_UNSUPPORTED_DECF_FUNC

CX_SY_ZERODIVIDE

  • Cause: Division by 0 (type p)
    Runtime Error: BCD_ZERODIVIDE
  • Cause: Division by 0 (type f)
    Runtime Error: COMPUTE_FLOAT_ZERODIVIDE
  • Cause: Division by 0 (type i)
    Runtime Error: COMPUTE_INT_ZERODIVIDE
  • Cause: Division by 0 (type decfloat)
    Runtime Error: COMPUTE_DECFLOAT_ZERODIVIDE


Non-Catchable Exceptions

  • Cause: p field does not contain the correct BCD format
    Runtime Error: BCD_BADDATA
  • Cause: p field does not contain a correct sign
    Runtime Error: BCD_NO_SIGN

Continue

arith_exp - Arithmetic Operators

arith_exp - ( )

arith_exp - Calculation Type