sapdev logo background
sapdev logo sapdev logo
Comments

SAP ARITH OPERATORS documentation, setup help and example usage



Return to SAP documentation index


ARTICLE

arith_exp - Arithmetic Operators
The table below shows the possible arithmetic operators for arithmetic expressions , their priority, and the order in which the calculation is performed. Within one parenthesis level , calculations with operators of higher priority are performed before calculations with operators of lower priority. For consecutive operators of the same priority, the calculation is performed in the order specified. In the third column of the table below, 3 indicates the highest priority and 1 the lowest. With the exception of ** , the operators have no effect on the calculation type. If the operator ** does not appear in an arithmetic expression, then the calculation type is determined only by the data types involved. When ** is used, the calculation type is either decfloat34 or f . The calculation type is decfloat34 if one of the operands used is a decimal floating point number , otherwise it is f .

Operator Calculation Priority Order
ABAP_KEY + Addition of the operands1From left to right
ABAP_KEY - Subtraction of the right operand from the left1
From left to right
ABAP_KEY * Multiplication of the operands2From left to right
ABAP_KEY / Division of the left operand by the right2From left to right
ABAP_KEY DIV Integer part of the division of the left operand by the right, with positive remainder2From left to right
ABAP_KEY MOD Positive remainder of the division of the left operand by the right; a remainder other than zero is always between zero and the size of the right operand2From left to right
ABAP_KEY ** Left operand raised to the power of the right3
From right to left

ABAP_PGL Preventing Division by Zero

Notes
  • Division by the value 0 is undefined and raises a handleable exception. The only situation where division by 0 does not raise an exception is if the dividend is also 0. Here, the result is set to 0.

  • The result of div multiplied by operand2 plus the result of mod always results in operand1 . Therefore, the rule that the result of mod is always positive also has an impact on the result of div . The result of an integer division of two positive numbers with a remainder that is not equal to zero differs from the result of an integer division of two negative numbers with the same amounts. Likewise, for operands with different signs, which operand is positive and which is negative is of significance.

  • If, when raising to a power, the left operand is 0, the right operand must be greater than or equal to 0. If the left operand is negative, the right operand must be an integer. Otherwise, both cases raise a handleable exception.

  • To stop the operator ** producing the calculation type f , the predefined function ipow can be used for integer exponents. Here, the calculation type is determined by the argument.


  • Example
    The following table shows the results of integer divisions and their remainders.
    operand1 operand2 div mod
    7321
    -73-32
    7-3-21
    -7-332

    Example
    See Lossless Calculations
    Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved




    ARITH_BRACKETS
    ARITH_TYPE




    comments powered by Disqus