show the entry list
STEP 7 -- Creating S7 programs -- Executing operations
 How in STEP 7 V5.x do you program signal generation (rectangle, sawtooth, triangle, sinus) using the cyclic interrupts? How do you determine the velocity, acceleration and distance? How can you address I/O bytes and individual bits via the I/O? How do you implement numerical differentiation in STEP 7 V5.5? How can you convert a hexadecimal number into ASCII characters with STEP 7 V5.5? What should you watch out for with the display of negative integer values in the LAD/STL/FBD editor when monitoring? Information on the topic of "Executing operations" How do you calculate power, exponential, root, polynomial, logarithm and linear values and how do you convert trigonometric functions into degrees? How can you convert a number value of the data type INT into a string in S5TIME, or into the data types BCD and REAL? How can you delay the output signal in relation to the input signal? How can you accumulate physical flow measurements (e.g. mass flow rate, flow velocity) to an overall value? How do you monitor a 32-bit field, evaluate the position of an active bit and access individual bits of a double-word? How can you link variables and block parameters with each other? How do you check real numbers for parity? How is numerical integration programmed in S7-SCL and STEP 7? When is there a first scan in logic operation sequences? What does the NOP statement in the STL editor mean in STEP 7? How accurately can I calculate with REAL numbers that are used in extensive formulas? Which functions and IEC standard functions exist in STEP 7 for converting and comparing string variables? How do you program a shift register with the LAD/FBD statements in STEP 7 V5.5 and in V11?
How do you check real numbers for parity?
Part number:

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:

1. 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

1. 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.
2. 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

 Entry ID:24000326 Date:2010-04-15