Skip to main content

POU: CheckDivReal

Functions for preventing division by "0": CheckDivInt, CheckDivLint, CheckDivReal, and CheckDivLReal

To prevent division by zero, you can use the functions CheckDivInt, CheckDivLint, CheckDivReal, and CheckDivLReal. If you include these functions in the application, then they are called before each division operation in the code.

For more information, see: Using POUs for Implicit Checks, POUs for Implicit Checks

Caution

To get the feature for monitoring functions, do not edit the declaration part. However, you are permitted to add local variables.

Example 531. The default implementation of CheckDivReal:

Declaration part:

// Automatisch erzeugter Code : NICHT EDITIEREN
FUNCTION CheckDivReal : REAL
VAR_INPUT
        divisor:REAL;
END_VAR

Implementation part:

// Automatisch erzeugter Code: Es handelt sich hierbei um einen Implementierungsvorschlag.
IF divisor = 0 THEN
        CheckDivReal:=1;
ELSE
        CheckDivReal:=divisor;
END_IF;

The DIV operator uses the output of the CheckDivReal function as a divisor. In the sample program below, CheckDivReal prevents division by 0 by changing the implicit value of the divisor d from "0" to 1 before the division operation is executed. Therefore, the division result is 799.

PROGRAM PLC_PRG
VAR
 erg:REAL;
 v1:REAL:=799;
 d:REAL:=0;
END_VAR
erg:= v1 / d;