How to use floating - point arithmetic in a general PLC program?

Oct 06, 2025Leave a message

How to use floating - point arithmetic in a general PLC program?

As a supplier of general PLCs, I've witnessed firsthand the importance of floating - point arithmetic in modern industrial automation. Floating - point arithmetic allows PLCs to handle real - world values such as temperature, pressure, and flow rates with high precision. In this blog, I'll share some insights on how to effectively use floating - point arithmetic in a general PLC program.

Understanding Floating - Point Numbers

Before delving into how to use floating - point arithmetic in a PLC program, it's crucial to understand what floating - point numbers are. Floating - point numbers are a way to represent real numbers in a computer system. They consist of a sign bit, an exponent, and a mantissa. In most PLCs, floating - point numbers follow the IEEE 754 standard, which provides a common format for representing these numbers across different platforms.

The advantage of using floating - point numbers is that they can represent a wide range of values, from very small to very large, with a high degree of precision. For example, in a temperature control system, floating - point numbers can accurately represent temperatures ranging from - 273.15°C (absolute zero) to extreme high temperatures, which is not easily achievable with integer numbers.

Configuring the PLC for Floating - Point Arithmetic

Most modern general PLCs support floating - point arithmetic, but you need to ensure that the PLC is properly configured to handle these operations. First, check the PLC's programming manual to confirm that it has the necessary instructions for floating - point arithmetic. Common floating - point instructions include addition, subtraction, multiplication, division, and square root.

Next, allocate appropriate memory locations for floating - point variables. In the PLC's programming environment, you can define variables as floating - point types. For instance, in ladder logic programming, you can use specific data types to represent floating - point values. Make sure to label these variables clearly for easy identification and debugging.

Implementing Floating - Point Arithmetic in the PLC Program

Once the PLC is configured for floating - point arithmetic, you can start implementing it in your program. Let's take a simple example of a temperature control system. Suppose you have a temperature sensor that outputs a voltage proportional to the temperature, and you want to convert this voltage to a temperature value in degrees Celsius.

First, you need to read the analog input from the temperature sensor. The analog input is usually an integer value representing the voltage. You then need to convert this integer value to a floating - point number. Most PLCs provide instructions for converting integer values to floating - point values.

// Assume AIW0 is the analog input word
REAL_Temp := I_TO_R(AIW0); // Convert integer to real

After converting the integer input to a floating - point number, you can perform the necessary calculations to convert the voltage to temperature. The conversion formula depends on the characteristics of the temperature sensor. For example, if the sensor has a linear relationship between voltage and temperature, you can use a simple linear equation:

// Assume a linear conversion formula: Temp = a * Voltage + b
a := 0.1; // Coefficient a
b := 20;  // Coefficient b
Final_Temp := a * REAL_Temp + b;

In this example, we first define the coefficients a and b as floating - point variables. Then we multiply the converted floating - point voltage value by a and add b to get the final temperature value.

Error Handling in Floating - Point Arithmetic

Floating - point arithmetic can introduce errors, especially when dealing with very large or very small numbers, or when performing a series of calculations. One common error is rounding error, which occurs when a number cannot be represented exactly in the floating - point format.

To minimize rounding errors, try to use the appropriate data types and perform calculations in the correct order. For example, if you need to perform multiple multiplications and divisions, group the operations in a way that reduces the number of intermediate results.

Another important aspect of error handling is checking for invalid operations. For example, division by zero is an invalid operation in floating - point arithmetic. In your PLC program, you should include conditional statements to check for such situations and handle them gracefully.

7_2_

IF Divisor <> 0 THEN
    Result := Dividend / Divisor;
ELSE
    // Handle the error, e.g., set a fault flag
    Fault_Flag := 1;
END_IF;

Applications of Floating - Point Arithmetic in General PLCs

Floating - point arithmetic has a wide range of applications in general PLCs. In addition to temperature control, it is also used in pressure control, flow measurement, and speed control systems.

In a pressure control system, floating - point arithmetic can be used to calculate the actual pressure based on the output of a pressure sensor. The calculated pressure value can then be compared with a setpoint, and the PLC can adjust the control valves to maintain the desired pressure.

In a flow measurement system, floating - point arithmetic is used to convert the flow rate sensor's output to an actual flow value. This value can be used for process monitoring and control, such as adjusting the pump speed to maintain a constant flow rate.

When choosing a general PLC for applications that require floating - point arithmetic, you may consider our CAN Bus PLC, Compact Mini PLC, or 485 Pulse PLC. These PLCs are equipped with powerful floating - point arithmetic capabilities and are suitable for a variety of industrial automation applications.

Conclusion

Floating - point arithmetic is an essential feature in modern general PLCs, enabling them to handle real - world values with high precision. By understanding the basics of floating - point numbers, configuring the PLC correctly, implementing the arithmetic operations in the program, and handling errors effectively, you can make the most of this powerful tool in your industrial automation projects.

If you are interested in our general PLC products and need more information on how to use floating - point arithmetic in your specific applications, feel free to contact us for procurement and further technical discussions. We are committed to providing you with the best solutions for your industrial automation needs.

References

  • "Programmable Logic Controllers: Principles and Applications" by William Bolton
  • IEEE 754 Standard for Floating - Point Arithmetic