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

Float Point Calculations

Former Member
0 Likes
2,909

Hi,

When I do the following in abap.

-> Snip <-

data: e_faktor type f,

a like drseg-menge value '1.100',

b like drseg-menge value '2.200',

c like drseg-menge value '3.300'.

e_faktor = ( a + b - c ).

write e_faktor.

-> Snip <-

the result is 4,4408920985006262E-16.

I would expect it to be 0.

Changing a = 2.2

b = 2.2

c = 4.4

Gives me 0 as expected

Am I missing something ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,546

Hi Dear,

DATA: e_faktor TYPE p DECIMALS 3, "f,

a TYPE p DECIMALS 3 VALUE '1.100',

b TYPE p DECIMALS 3 VALUE '2.200',

c TYPE p DECIMALS 3 VALUE '3.300'.

e_faktor = ( a + b ) - c .

WRITE e_faktor.

Try this.

I hope this will helpful for you.

Regards,

Vijay

8 REPLIES 8
Read only

Former Member
0 Likes
1,546

data: e_faktor type f,

a type p value '1.100',

b type p value '2.200',

c type p value '3.300'.

break developer.

e_faktor = ( a + b ) - c .

write e_faktor.

Read only

0 Likes
1,546

a type p value '1.100',

is not the same as

a type p decimals 3 value '1.100',

Read only

Former Member
0 Likes
1,547

Hi Dear,

DATA: e_faktor TYPE p DECIMALS 3, "f,

a TYPE p DECIMALS 3 VALUE '1.100',

b TYPE p DECIMALS 3 VALUE '2.200',

c TYPE p DECIMALS 3 VALUE '3.300'.

e_faktor = ( a + b ) - c .

WRITE e_faktor.

Try this.

I hope this will helpful for you.

Regards,

Vijay

Read only

0 Likes
1,546

Vijay,

Thanks, that would work , but I would like to know why my example does not work.

This is an example of code in a standard SAP function that MRM_ORDER_PRICE_QUANTITY_CHECK.

Is there something wrong with the way Float Points work in SAP ?

I expect the following code to give the answer 3.300000000000000E00 not 3.2999999999999998E00 as our is the case in our SAP system.

-> Snip <-

data: e_faktor type f,

a type p decimals 3 value '3.300'.

e_faktor = a.

write e_faktor.

-> Snip <-

Read only

0 Likes
1,546

How SAP works: as target is a float field, packed fields are converted to float before the calculation. So we get the problem of rounding.

This kind of problem is inherent to floating point numbers.

The rounding problem is explained and solved in [Note 361784 - Floating point rounding problems|http://service.sap.com/sap/support/notes/361784]. Use method ROUND_F_TO_15_DECS of class CL_ABAP_MATH.

Read only

Clemenss
Active Contributor
0 Likes
1,546

Hi Adriaan,

?? just copied your code snippet in SAP ECC 6.0 SAP_BASIS 700 SAP_ABA 700 system:


FORM calc .
  DATA:
    e_faktor TYPE f,
    a LIKE drseg-menge VALUE '1.100',
    b LIKE drseg-menge VALUE '2.200',
    c LIKE drseg-menge VALUE '3.300',
    d  LIKE drseg-menge.

  d        = ( a + b - c ).
  e_faktor = ( a + b - c ).
  SET COUNTRY 'DE'.
  WRITE:/ e_faktor, d.
  SET COUNTRY 'US'.
  WRITE:/ e_faktor, d.
ENDFORM.                    " CALC

this results in


  0,0000000000000000E+00            0,000
  0.0000000000000000E+00            0.000

How did you proceed, what system are you working on?

Regards,

Clemens

Read only

Former Member
0 Likes
1,546

Clemens,

Copy and paste your code with the result below:


4,4408920985006262E-16   0,00
4.4408920985006262E-16   0.00

System Information

Kernel Patch number - 247

SAP_BASIS - 620 support pack SAPKB62063

SAP_ABA - 620 support pack SAPKB62063

Read only

0 Likes
1,546

Clemens, I also have the same result as Adriaan.

The difference between kernels/OS is fun, but it's no more an issue than having float = '1.005' resulting in 1.004999999999999E+00.

All this is normal, it's the general issue of rounding with floating point numbers, and it's not related to ABAP.

Adriaan, if you want fixed point precision, you must use packed numbers.

In your example, you must either:

- directly use packed number

- or if you really want a float as result of the calculation, add extra statements transfer float to packed. Remark: the Note 361784 (Floating point rounding problems) I indicated above explains some interesting things but does not solve this "problem" of F which is not exactly zero (this note gives only a solution for transferring a float variable to a packed variable.

If you want more information, check wikipedia documentation about floating point and fixed point arithmetic