Cookies are important for this site to function properly, to guarantee your safety, and to provide you with the best experience. By clicking OK, you accept all cookies. For more information, please access our Privacy Policy.
Table of Contents
Tutorials / 
Floating-Point Value Calculation (REAL and LREAL) in SCL
Advanced

Floating-Point Value Calculation (REAL and LREAL) in SCL

PLC Programming
Siemens
TIA Portal
Structured Control Language (SCL)

Introduction

Becoming familiar with different data types is essential for writing PLC programs. One of the data types widely used in programming is REAL. Most PLC programmers ignore that the TIA Portal software specifies and calculates the data type REAL with a precision of six decimal places, which might lead to unexpected results. This issue, alongside possible solutions, will be discussed in detail in this tutorial.

Prerequisites

What you'll need to follow this tutorial:

Floating-Point Numbers Accuracy Representation

A PLC program specifies and computes, the data type REAL with a precision of six decimal places. It should be emphasized that this precision generally applies to each step of the computation for the floating-point numbers (REAL and LREAL) calculation.

When you add and subtract floating-point numbers, the exponents are adjusted. During the calculation of adding or subtracting, both the base and the exponents are equal, and only the mantissa are added.

As shown in figure 1.1, two operands of the data type REAL (a and b) must be added, and one REAL operand (c) must be subtracted to complete the first calculation line. In the second calculation line, the constant "one" is divided by the previous outcome (y), and the result is stored in the operand z.

To accomplish this task, you need to make a global data block where you define your operands and create a function where you program the calculation processes.

Figure 1.1: Calculation formulas example

The following values are assigned to the operands:

Table 1.1: Values of operands

First, the data block “DB_GlobalData” should be created. To do this, click twice on the Add new block item on the left pane, under the Project tree, and the dialog box called Add new block appears. Select the Data block (DB) button and put DB_GlobalData in the name field. Then, choose Global DB for the data block type and press the OK button.

Figure 1.2: Creating global data block

Create a, b, and c tags as statics under the name column, select Real for their data types, and type the relevant start values as shown in figure 1.3. Both a and c tags have a starting value of 100000000.0. By the data type REAL, this initial value is converted into 1.0E+08.

Figure 1.3: Creating tags in the data block

Now, it’s time to create an SCL function. Double-click Add new block item on the left pane, and the Add new block window opens. Select the Function (FC) button and enter FC_Calculate as the name. Then, choose SCL from the language drop-down menu and press the OK button.

Figure 1.4: Creating an SCL block

When the created SCL block opens, click on the down arrow to open the block interface. Define z and y tags as temporary variables under the name column and select REAL for their data types. Next, start typing calculation formulas in the SCL editor window, as shown in figure 1.5.

Figure 1.5: Creating temporary variables and typing the application code

For the function to be executed in each PLC cycle scan, it must be called by the Main OB. To do this, double-click the Main OB on the left pane and when the main PLC programming window opens, drag and drop the created SCL block on network 1, as shown in figure 1.6.

Figure 1.6: Calling the FC in the Main OB

It’s time to view the outcome in the function block by establishing an online connection to the PLC. To do this, left-click over the Start simulation icon on the top toolbar, and the Load preview window opens along with the S7-PLCSIM Advanced (PLC simulator).

As you can see at the bottom of the TIA Portal software in the inspector window, the whole PLC program is successfully compiled, and no errors or warnings are found. To continue, press the Load button.

Figure 1.7: Downloading the program to the simulator - The first action

When the Load results window pops up, press the Finish button, and the program starts downloading to the PLC simulator. Finally, by clicking the RUN button over the S7-PLCSIM Advanced interface, bring the PLC simulator to run mode.

Figure 1.8: Downloading the program to the simulator - The second action 

Now, left-click the eyeglasses icon (Monitoring on/off) in the toolbar of the SCL editor to show the application code in the online mode.

Figure 1.9: Monitoring the program in online mode

As you can see, the result at the operand is #y = 0, even though a value of 1 is intended. Why is that?

Figure 1.10: Software result vs expected result

The following is how the incorrect outcome is reached. The operands a and b are added in the first stage of the computation. After adjusting the exponents, the REAL values of the two operands (a = 1.000000*108 and b = 1.000000*100) are as follows: a = 1.000000*108 and b = 0.00000001*108. The final two digits of the second number (operand b) are truncated since the precision of its six decimal digits prevents them from being displayed. As a result, the operand is added with a 0 rather than a 1.

Figure 1.11: Addition of operands a and b

In the second stage of the computation, operand c is deducted from the result of the previous computation step (intermediate outcome = a + b – c = 1.000000*108 - 1.000000*108 = 0.000000e0).

Figure 1.12: Subtracting operand c from the previous result

In the next computation step, you attempt to divide one by zero when you compute the operand z, and as a result, the value of operand z becomes a large number.

Figure 1.13: Calculating operand z

There are two ways to fix this issue. First solution: To deal with such situations, you can modify your calculating formula. Instead of having y = a + b – c in the first computation line, form the formula as y = a – c + b. Since the equation includes adding and subtracting, you were able to change the positions of operands b and c. So, you should first bring the program into offline mode by clicking on the Go offline button on the top toolbar and then start editing the calculation formula in the SCL editor window, as shown in figure 1.14.

Figure 1.14: Modifying the calculation formula

You need to download the modified program to the simulator to view the result. To do this, left-click the "Download to device" button on the top toolbar to open the Load preview window and press the Load button to bring the program into online mode.

Figure 1.15: Downloading the Modified program to the simulator

Since the outcome 0.000000e0 is accessible in this scenario, after the initial computation step (a – c = 1.000000*108 - 1.000000*108 = 0.000000*100), the adding of the REAL number (operand b) in the second computation step (0.000000*100 + 1.000000*100) results in the desired outcome, which is 1.000000*100.

Figure 1.16: Result of modifying the calculation formula

The second solution is if you don’t want to modify your calculation formulas, you can substitute the LREAL data type for the REAL data type. It is possible because the LREAL data type is handled with a precision of 15 decimal digits, and the previous issue does not even occur.

To apply this solution, click the “Go offline” button in the top toolbar to bring the program into offline mode. Then double-click the data block DB_GlobalData on the left pane and start creating three new tags called a_LREAL, b_LREAL, and c_LREAL with the previous values, each having the data type LREAL.

Figure 1.17: Creating LREAL tags in data block

Return to the SCL block interface by double-clicking the function FC_Calculate, under the Program blocks folder in the Project tree, and pressing the down arrow on the top of the block editor. Declare two new tags called z_LREAL and y_LREAL as temporary variables containing LREAL data types. You can start editing your application code based on the new LREAL tags you created for the calculation formulas. Then to view the result, left-click the "Download to device" button in the top toolbar to open the Load preview window.

Figure 1.18: Creating LREAL temporary variables and modifying the application code

In the Load preview dialog box, for the data block, choose Re-initialize from the Action drop-down menu and then press the Load button to download the program into the PLC simulator.

Figure 1.19: Downloading the modified program (in LREAL) to the simulator

After pressing the eyeglasses icon in the toolbar of the SCL editor, the application program will be displayed in the online mode. As you can see, the expected outcomes for operands y and z are achieved without confronting the previous issue discussed at the beginning of this tutorial.

Figure 1.20: Result of modifying the calculation formula (in LREAL)

Conclusion

This tutorial discussed REAL and LREAL floating-point calculations in Structured Control Language (SCL). You learned the REAL data type is processed with a precision of six decimal digits and generally applies to each calculation step through the PLC program. Also, you understood the base and exponents are equal while dealing with adding or subtracting floating-point values.

Finally, you learned there are two ways to handle this issue. First, you can modify your calculating formula by changing the position of operands under the rules of mathematics. And secondly, you can replace the REAL data type with the LREAL data type since the LREAL data type processes with a precision of 15 decimal digits.