Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

data type F

former_member48736
Participant
0 Likes
5,354

Hi,

Can some explain to me why below result is 9.2249999999999999E-01, not 9.225E-01?

DATA: l_calc_base TYPE f.

l_calc_base = ( 2008.00 - 163.00) / 2000.00.

2 REPLIES 2
Read only

philipdavy
Contributor
0 Likes
2,834

Hi

Here is what it says in ABAP documentation.

  • Floating point numbers - type F

The value range of type F numbers is 1x10**-307 to 1x10**308 for positive and negative numbers, including 0 (zero). The accuracy range is approximately 15 decimals, depending on the floating point arithmetic of the hardware platform. Since type F data is internally converted to a binary system, rounding errors can occur. Although the ABAP processor tries to minimize these effects, you should not use type F data if high accuracy is required. Instead, use type P data.

It clearly mentions that the accuracy when using type F is appoximately 15 decimals.

Regards,

Philip.

Read only

former_member209120
Active Contributor
0 Likes
2,834

Hi Jiangang You,

DATA: l_calc_base TYPE f.

l_calc_base = ( 2008 - 163 ) / 2000.

Result is 9.2249999999999999E-01

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).

See this link http://help.sap.com/abapdocu_70/en/ABENNUMBER_TYPES.htm

This is the reason for getting 9.2249999999999999E-01, not 9.225E-01.

Regards,

Ramesh.T