Description: When calculating with real numbers, rounding errors typically occur through the structure of real numbers (exponential view) and the high value range, so that parity (all bits are the same) hardly ever occurs. Comparing real numbers for "parity" thus does not usually produce the desired results, so other options are provided here. One of these options would be to employ an approximation. This entry presents an approximation which you can use to check real numbers for "parity". A sample program for applying the approximation is also included. The entry also includes a brief presentation of the real number format.
Approximation for comparing real numbers for parity The approximation described here for comparing real numbers is based on the formula:
|(a -b)/(|a| + |b|)| < eps
The approximation expressed in words:
2 numbers "a" and "b" are considered as having parity when the ratio of "Difference(a,b) : Sum(amount(a), amount(b)) of the two numbers exceeds the "eps" value.
The following properties of the formula are to be taken into account:
- If the values of the numbers "a" and "b" are very large, the numbers are considered as having parity already at a higher difference. The value of "eps" might then have to be reduced according to the requirements of your system.
If the values of the numbers "a" and "b" are very small, the numbers are considered as having parity only at a small difference.
Note:
Please also note here that only a maximum of 6 consecutive hexadecimal digits can be displayed in the mantissa of a real number.
Examples for 1
| |
Example 1 values |
Example 2 values |
| (|a| + |b|) |
5 |
5000000 |
| "eps" |
10-6 [0,000001] |
| Difference of "a" and "b" in which "a" and "b" can be evaluated as having parity. |
0,00000499... |
4,999... |
Table 01
- If both values "a" and "b" contain the value 0, then the sum of the amounts of "a" and "b" is also 0. Dividing values by 0 is not permissible and leads to a false result. Therefore, you cannot use the approximation to make a comparison of 2 real numbers "a" and "b", which both contain the exact value 0.
- If one of the two REAL numbers to be compared has the value 0, the approximation described in this entry no longer works either. The result of the approximation in this case is always 1. The comparison is therefore always "not having parity", unless "eps" is set as greater than 1.
Notes:
- If a division by 0 has been made, the CPU indicates this error by setting the status bits OS, OV, A0 and A1.
- If the result of mathematical functions is 0, then the two status bits A0 and A1 do not remain set.
The attached download contains the archived STEP 7 project in which the approximation is implemented. Here "eps" is preset with the value of 10-6.
The block is written in STL and is contained in the project once with English comments (S7 Program_English) and once with German comments (S7 Programm_German).
Attachment 1: S7_Gleichheit.zip ( 43 KB )
Structure of real numbers A number of the real data type is 32 bits long in STEP 7. The individual bits have the following meanings (bit counting from right to left):
- Bit 0..22: mantissa of the real number (in the binary system).
- Bit 23..30: exponent of the real number (e = exponent -127).
- Bit 31: sign of the real number (0 -> positive, 1 -> negative)
You can calculate the real number using the following formula:
Sign * Mantissa * 2 (Exponent -127)
Fig. 01 below shows the graphical representation of the structure of real numbers with 32 bits as used in STEP 7.
More information on the structure of the REAL data type is available in the S7 Online Help under "Format of the REAL data type (floating-point numbers)".
Keywords:
Float, ==R, Floating-point number
|