Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 9760

Round-off errors in ABAP

$
0
0

Hello,

 

I am quite new to ABAP programming and during an exercise I realized that sometimes very exact numbers are necessary.

 

Thought experiment:

You are a programmer in a gold factory which produces spheres of gold. The value of a sphere is calculated and depends on the measurement of its diameter. The measurement is performed by a super precise high tech geek device and not your concern. Develop a program that calculates the value of produced gold spheres in Euros and calculates in the metric system.

 

See the implementation below.

 

Discussion

 

I tried to use different lenghts of decimal places:

  1. 0 EUR -> 2 decimals !!!
  2. 59.111,91 EUR -> 4 decimals !!!
  3. 30.519,51 EUR -> 8 decimals
  4. 30,521,11 EUR -> 10 decimals
  5. 30.521,09 EUR -> 12 decimals
  6. 30.521,09 EUR -> 14 decimals

 

You can see how the result get closer to the true value of 30.620 EUR for 1 kg of gold on the 05/15/2014. For a gold sphere of 1kg weight with a diameter of exactly 0.0462 meters.

 

However the difference is still 98,91 EUR!

 

Is there a way to make such calculations even more precise in ABAP?

 

Implementation:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
*----------------------------------------------------------------------**       CLASS lcl_geo DEFINITION*----------------------------------------------------------------------***----------------------------------------------------------------------*CLASSlcl_geoDEFINITIONPUBLIC SECTION.    CONSTANTSpiTYPEpLENGTH16DECIMALS14VALUE'3.14159265358979323846264'.    CONSTANTSfourthirdTYPEpLENGTH16DECIMALS14VALUE'1.33333333333333333333'.    CONSTANTSvol1kgauTYPEp  LENGTH16DECIMALS8VALUE'0.0000518'.ENDCLASS.                    "lcl_geo DEFINITIONDATAvolumeTYPEpDECIMALS14.DATAerrTYPEpDECIMALS14.DATAkgramsTYPEpDECIMALS14.DATAvaleurTYPEpDECIMALS2.START-OF-SELECTION.*diameter of gold ball in metersPARAMETERSp_diagbTYPEpLENGTH16DECIMALS8.*calculate radiuserr=p_diagb / 2.*volume sphere: (4/3) * pi * r³volume=lcl_geo=>fourthird*lcl_geo=>pi*err*err*err.*calculate masskgrams=volume / lcl_geo=>vol1kgau.*course for 1g of gold was 30.62 EUR on 05/15/2014 source: wolframalpha.comvaleur=kgrams*'30.62'*1000.write: /'Radius (m):', err, /'Volume (m³):', volume, /'Weight (kg):', kgrams.ULINE.WRITE: 'Your gold ball had on the 15. Mai 2014 a value of', valeur, 'EUR'.

Viewing all articles
Browse latest Browse all 9760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>