ABAP Keyword Documentation → ABAP - Reference → Declarations → Declaration Statements → Data Types and Data Objects → Types and Objects - Overview → Details of Special Data Types or Data Objects
Numeric Data Types
ABAP supports the numerical data types: i
,
p
,
decfloat16
,
decfloat34, and f
,
plus the internal types b
and s
. The latter
cannot be specified directly in programs but are created when the predefined types INT1 or INT2 from
ABAP Dictionary are referenced. They are generally used in the same way as the type i
and are often converted to i
internally.
The numeric data types are intended for calculations. Calculations involving fields of type i
and type f
correspond more or less directly to the operating system's machine
commands for the current application server. In contrast, calculations involving packed numbers of type
p
are programmed in the ABAP runtime environment's kernel and are therefore
somewhat slower. Operations using the decimal floating point numbers decfloat16
and decfloat34
run using a library integrated into the ABAP kernel, until they are supported by the hardware of the application server.
The common generic type of the numeric data types is numeric
.
Other versions: 7.31 | 7.40 | 7.54
Programming Guideline
Notes
- To a great extent,
decimal floating point numbers of the types
decfloat16
anddecfloat34
replace the binary floating point numbers with typef
.
- The predefined type
n
(numeric text field) is not a numeric number type, although its values are purely strings of digits; instead it is a character-like type, not generally to be used for calculations. Typical examples of numeric text fields are account numbers and article numbers, postal codes, and so on.
Integers
The data types of integers i
have a
value range of -2147483648 through +2147483647 and contain integers only. Integers can be specified as
numeric literals directly in the program.
Intermediate results in arithmetic expressions of
calculation type
i are stored in type i
auxiliary fields. Otherwise, type i
arithmetic is similar to performing calculations with type p
without
decimal places.
In particular, division rounds numbers rather than truncating them and an overflow raises an exception.
Data type i
is typically used for counters, quantities, indexes, and offsets, as well as time periods.
Packed Numbers
The data type p
for
packed numbers has a
value range that depends on their length and the number of
decimal places. Data objects of type p
can be 1 to 16 bytes long, with two
decimal places packed
into each byte, and one decimal digit and the sign packed into the last byte. A packed number consists of the length x 2 minus 1 digits and can have a maximum of 14 decimal places. Packed numbers are used to implement
fixed point numbers. The decimal places in a packed number are an attribute of its data type and are fixed for this type.
Packed numbers with decimal places cannot be specified directly in the program. Instead, character literals must be used whose content can be interpreted as a packed number, that is, it represents a number in mathematical or commercial notation. Scientific notation is not permitted unless it can be interpreted as a mathematical notation.
Auxiliary fields for intermediate results in arithmetic expressions of calculation type p are always 16 bytes long and can thus hold up to 31 decimal places. Before an overflow, an arithmetic expression is calculated again with auxiliary fields that are twice as large or 63 decimal places. In the case of comparisons between packed numbers, the operand with fewer decimal places is also converted into an auxiliary field of this type, though an overflow occurs if the sum of the whole number places and decimal places exceeds 31.
If packed numbers are used, the
program attribute must always be set to fixed point arithmetic since
only this setting ensures that the decimal point is correctly calculated. Otherwise, all numbers are
specified as integers and all intermediate results are rounded up to the next integer. If fixed
point arithmetic is not configured, the decimal places defined for the number only appear in dynpro output or when formatting with WRITE [TO]
.
Calculations using calculation type p
are performed using
fixed point arithmetic.
In other words, a calculation is performed "commercially", similarly to using a pocket calculator or
paper and pencil. Type p
is typically used for values such as lengths, weights, and sums of money.
Note
The number of decimal places in a packed number should be no greater than the number of digits; otherwise the decimal separator might be outside the sequence of digits.
Decimal Floating Point Numbers
The data types fordecimal floating
point numbers are decfloat16
and decfloat34
.
The value range is 1E385(1E-16 - 1) through -1E-383, 0, +1E-383 through 1E385(1 - 1E-16) for decfloat16
and 1E6145(1E-34 - 1) through -1E-6143, 0, +1E-6143 through 1E6145(1 - 1E-34) for decfloat34
.
The maximum precision is 16 decimal places or 34 decimal places, respectively. As well as its value, a decimal floating point number has a
scale and a
precision. These properties are not relevant for calculations and comparisons of values, but are used in
conversion rules and for formatting output.
Decimal floating point numbers with decimal places or exponents cannot be specified directly in the program. Instead, character literals must be used whose content can be interpreted as a packed number, that is, it represents a number in mathematical, scientific, or commercial notation.
Arithmetic expressions with decimal floating point numbers always have the
calculation type decfloat34
. Each calculation is made with
decimal floating point arithmetic.
We recommend decimal floating point numbers if precision and a large range of values are of importance.
They do not have the disadvantages of binary floating point numbers described below. These binary floating
point numbers cannot represent each decimal number in their value range exactly. Decimal floating point numbers have a much large value range and a higher level of precision than packed numbers.
By using the lossless operator EXACT
,
it is possible to force a lossless calculation for decimal floating point numbers under certain circumstances. No rounding is permitted in lossless calculations and raises an exception.
Internally, decimal floating point numbers are represented by a 16-digit or 34-digit decimal mantissa and a decimal exponent. The exponent is between -383 and +384 or -6143 and + 6144, respectively. Apart from potential roundings in assignments and calculations, the effects discussed below for binary floating point numbers are not observed. This is because every 16-digit or 34-digit decimal number can be represented exactly.
Binary Floating Point Numbers
The data type for
binary floating point numbers, f
, has a
value range of 2.2250738585072014E-308
through 1.7976931348623157E+308, positive as well as negative, and the number 0, with an accuracy of
at least 15 decimal places. 17 decimal places are represented in ABAP. Whole numbers can be represented
exactly up to an absolute value of 2**53, which is equivalent to 9,007,199,254,740,992. Any larger numbers are rounded.
Binary floating point numbers cannot be specified directly in the program. Instead, character literals must be used whose content can be interpreted as floating point numbers, that is, it represents a number in scientific notation. Mathematical or commercial notation is not permitted unless it can be interpreted as scientific notation.
Arithmetic expressions with
calculation type f
are performed using
binary floating point arithmetic. Be aware of the following features of binary floating point arithmetic.
Internally, binary floating point numbers are stored separately, each in two parts. This can lead to
unexpected results despite the high degree of intrinsic accuracy. These occur mainly when performing conversions from and to type f
.
- For example, the number 1.5 can be represented exactly in this notation since 1.5 = 1*2**0 + 1*2**(-1), but the number 0.15 can only be represented approximately by the number 0.14999999999999999. If 0.15 is rounded up to 1 valid digit, the result is 0.1 rather than 0.2 as might me expected. On the other hand, the number 1.5E-12 is represented by the number 1.5000000000000001E-12, which would be rounded up to 2E-12.
- A further real-life example: 7.27% of 73050 is to be calculated and rounded to 2 decimal places. The intermediate result is 5.3107349999999997E+03, since the correct result, 5310.735, cannot be represented exactly in two parts with 53 bits. (If the hardware cannot represent a real number exactly, it uses the next representable binary floating point number). After rounding, the result is 5310.73, rather than 5310.74 as might be expected.
The ABAP runtime environment always calculates commercially and not numerically like the underlying machine arithmetic. According to the rounding algorithm of the latter, the end digit 5 must always be rounded to the nearest even number (not the next largest number), that is, from 2.5 to 2 and from 3.5 to 4.
Note also that multiplication using powers of 10 (positive or negative) is not an exact operation.
- Example: Although it can be represented exactly in two parts, a binary floating point number
f
of value 100.5, after the operation
f = f / 100 * 100.
has the value 100.49999999999999.
As well as rounding errors, the restricted number of decimal places for the mantissa can lead to the loss of trailing digits.
- Example:
1 - 1.0000000000000001
produces zero.
This means that the last digits in binary floating point arithmetic are not reliable. In particular,
it is not usually worth testing two binary floating point numbers a
and
b for equality; instead, it is best to check whether the relative difference abs((a - b)/a)
is less than a predefined limit, such as 10**(-7).
Ultimately, the display and therefore the value of a binary floating point number stored in a database can be platform-dependent.
Note
To assign numeric values to text fields and text strings, instead of using a
conversion it is often better to use the statement WRITE ... TO
or
embedded expressions in string templates with the associated formatting options.
Example
Refer to Decimal Floating Point Numbers, Arithmetic Calculations.