# Numeric Data Types

ABAP supports three numeric data types: i, p and f. Their shared generic type is numeric.

Type n (numeric text field) is a text type, not a numeric data type (although its values are strings of digits), because the strings are not used for calculation purposes. Typical examples of numeric text fields are account and article numbers, postal codes and so on. Real numeric data types are intended for calculations.

Calculations involving fields of type i or 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 noticeably slower.

## Integers

The data type of integers, i, has a value range of -2147483648 to +2147483647 and contains integers only. You can specify integers 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 results in an exception. Data type i is typically used for counters, quantities, indexes and offsets as well as time periods.

## Packed Numbers

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. There can be up to 14 decimal places. You cannot specify packed numbers with decimal places directly in the program. Instead, you have to use text literals whose contents can be interpreted as a packed number, that is, contains a number in mathematical or commercial notation. The 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.

If you are using packed numbers, you should always set the program attribute fixed point arithmetic as only this ensures that the decimal point is correctly calculated. Otherwise, all numbers are specified as integers and all intermediate results for the next integer are rounded. If fixed point arithmetic is not set, the decimal places defined for the number only appear when outputting with WRITE.

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 sizes such as lengths, weights and sums of money.

## Floating Point Numbers

The data type for floating point numbers f has a value range of2,2250738585072014E-308 to 1,7976931348623157E+308, positive as well as negative, and the number 0, with an accuracy of at least 15 decimal places.

You cannot enter floating point numbers directly in your program. Instead, you have to use text literals that can be interpreted as floating point numbers, that is, contains a number in scientific notation. Mathematical or commercial notation are not permitted unless they can be interpreted as scientific notation.

Arithmetic expressions with calculation type f are performed using floating point arithmetic. Use this if you need a very large value range or you are making decimal calculations, but be aware of the following features of floating point arithmetic.

Internally, the exponent and the mantissa of 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 you round 0.15 up to 1 valid digit, the result is 0.1 rather than 0.2 you would expect. 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.
• Another example that actually occurred is the calculation of 7.27% of 73050 to an accuracy of 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 floating point number). After rounding, you therefore get 5310.73, rather than 5310.74 as you would expect.

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, 3.5 to 4.

You should also note that multiplication using powers of 10 (positive or negative) is not an exact operation.

• For example, although it can be represented exactly in two parts, a floating point number F of value 100.5, after the operation
F = F / 100 * 100.
it 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.

• For example, 1 - 1,0000000000000001 results in zero.

This means you cannot rely on the last digits in floating point arithmetic. In particular, you should not usually test two floating point numbers a and b for equality; instead, you should check whether the relative difference abs((a - b)/a) is less than a predefined limit, such as 10**(-7).