‎2009 Oct 08 12:20 PM
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 ?
‎2009 Oct 08 1:00 PM
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
‎2009 Oct 08 12:38 PM
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.
‎2009 Oct 08 1:17 PM
a type p value '1.100',
is not the same as
a type p decimals 3 value '1.100',
‎2009 Oct 08 1:00 PM
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
‎2009 Oct 08 1:29 PM
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 <-
‎2009 Oct 08 10:41 PM
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.
‎2009 Oct 08 11:30 PM
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
‎2009 Oct 09 7:55 AM
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
‎2009 Oct 09 9:49 AM
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