Skip to Content
0
Apr 18, 2018 at 02:59 AM

A calculation precision issue with ABAP.

301 Views

Hi guys,

I've met a question about division and multiply. Please see the following java codes.

double a = 154.8, b = 37, c = 144;

double d = b/c;

d = a * d;

The result of 'd' is 39.775

However, I've made a similar program in ABAP, see below.

data lv_decfloat34_1 type decfloat34.
data lv_decfloat34_2 type decfloat34.
data lv_decfloat34_3 type decfloat34.
data lv_decfloat34_4 type decfloat34.

lv_decfloat34_1 = '154.8'.
lv_decfloat34_2 = 37.
lv_decfloat34_3 = 144.

lv_decfloat34_4 = lv_decfloat34_2 / lv_decfloat34_3.
lv_decfloat34_4 = lv_decfloat34_1 * lv_decfloat34_4.
* result is 39,77499999999999999999999999999999

write: lv_decfloat34_4.

data lv_f_1 type f.
data lv_f_2 type f.
data lv_f_3 type f.
data lv_f_4 type f.

lv_f_1 = '154.8'.
lv_f_2 = 37.
lv_f_3 = 144.

lv_f_4 = lv_f_2 / lv_f_3.
lv_f_4 = lv_f_4 * lv_f_1.

* result is 3,9774999999999999E+01

write: lv_f_4.

data lv_p_1(16) type p DECIMALS 14.
data lv_p_2(16) type p DECIMALS 14.
data lv_p_3(16) type p DECIMALS 14.
data lv_p_4(16) type p DECIMALS 14.

lv_p_1 = '154.8'.
lv_p_2 = 37.
lv_p_3 = 144.

lv_p_4 = lv_p_2 / lv_p_3.
lv_p_4 = lv_p_4 * lv_p_1.

* result is 39,77499999999931

write: lv_p_4.

I've also made a similar experiment with C++. The result is the same as JAVA.

As customer is verifying result with Excel, which gives 39.775, not equal to our result. So we meet the problem. Can anybody help with this issue?

Thanks,

Aicro