How accurately can I calculate with REAL numbers that are used in extensive formulas?
Generally speaking, it holds that REAL numbers are specified and calculated with an accuracy of 6 decimal places. With the calculations you should also note that this accuracy holds for every single step of the calculation.
When adding and subtracting, the exponents are aligned, i.e. the basis and the exponents are the same for adding and subtracting, only the mantissas are added.
To be calculated:
y = a + b - c;
z =1 / y;
a = 100 000 000 as REAL value = 1.000000*108
b = 1 as REAL value = 1.000000*100
c = 100 000 000 as REAL value = 1.000000*108
- In the first step of the calculation (a + b) is determined:
(a + b) = 1.000000*108 + 1.000000*100 after alignment of the exponents you get
(a + b) = 1.000000*108 + 0.00000001*108. The last two places are truncated, because they can no longer be displayed (accuracy of 6 decimal places).
- So a zero is added.
- In the second step of the calculation ((a + b)-c), c is subtracted from the result of the previous step of the calculation:
((a + b)-c) = 1.000000*108 - 1.000000*108 = 0.000000e0
- If z is now determined, then you attempt to divide by zero!
The correct solution is obtained in this case by converting the calculation.
y = a - c + b;
z = 1 / y;
Since here, after the calculation (a - c), the result 0.000000e0 is obtained, in the second step of the calculation ((a -c) + b), the addition of the REAL number leads to the correct result.
y = 0.000000*100 + 1.00000*100 = 1.000000*100
Therefore, before programming a calculation, you should always check how the calculation can best be made.
More information is also available in the STEP 7 Online Help under the Topic of "Real: data type".
In this example the REAL number 160.0 is divided by the REAL number 100.0. There are two versions of this calculation: either 160.0 / 100.0 or 160.0 * 0.01, whereby the following must be observed with the second version. According to Table 1 below both versions of the calculation provide the same result, but not the same computational accuracy in the HEX format.
|Version 1:||Version 2:|
|STEP 7||S7-SCL||STEP 7||S7-SCL|
|L 1.6e+002||X1 = 1.6e+002||L 1.6e+002||X3 = 1.6e+002|
|L 1.0e-002||X2 = 1.0e-002||L 1.0e+002||X4 = 1.0e+002|
|*R||Y1 = X1 * X2
= 1.6 * 1.0 e(+002-002)
|/R||Y2 = X3 / X4 |
= (1.6 / 1.0) e(+002-002)
|T MD0||Y1 = 1.6||T MD4||Y2 = 1.6|
Table 1: Two different versions of the division of REAL numbers in the LAD/FBD/STL editor
The reason for this is that the two REAL numbers in version one, 160.0 and 0.01, are further apart on the number scale than the two REAL numbers in version two, 160.0 and 100.0. These effects on the computational accuracy hold for STEP 7 in the LAD/FBD/STL editor and with the S7-SCL options package.
Sample calculation in STEP 7 in the LAD/FBD/STL editor:
In the variables table (Fig. 1) the two marker double-words MD0 and MD4 are displayed once in HEX format and once in floating-point format. The REAL numbers in both versions are the same,1.6, but in the HEX display the last two places differ by one HEX value.
Fig. 1: Program of the two calculation versions in the LAD/FBD/STL editor
Sample calculation with the S7-SCL options package:
The S7-SCL program below contains two functions. With function FC10 division of the REAL numbers is realized with "X1*X2" and with the function FC11 the division is realized with "X3/X4". The results are transferred to the marker double-words MD10 and MD20 when the functions are called in OB1.
Fig. 2: S7-SCL program for calculating the two versions as in Table 1
The results of the functions (MD10 and MD20) are displayed each in HEX and floating-point format in the "Monitor/Control Variables" table. Here too the REAL numbers in both versions are 1.6, but in the HEX display the last two places differ by one HEX value.
Fig. 3: "Monitor/Control Variables" table with display of results in HEX and floating-point format for S7-SCL