POU ‘CheckDivReal’

Functions for preventing division by zero: 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.

Caution

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

The default implementation of CheckDivReal:

Declaration section:

// This is automatically generated code: DO NOT EDIT
FUNCTION CheckDivReal : REAL
VAR_INPUT
\ divisor:REAL;
END_VAR

Implementation section:

// This automatically generated code is a suggested implementation.
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;

See also