IsFinite

IsFinite 1:

The function IsFinite() returns TRUE, if its argument has a finite value (INF < x < +INF). The function returns FALSE, if the argument is infinite or NaN (NaN = Not a number). IsFinite() checks whether the formatting of an LREAL or REAL variable complies with IEEE.

 

INF numbers may occur in a runtime system if the result of a mathematical operation falls outside the range that can be represented. E.g.:

PROGRAM MAIN
VAR
    fSingle : REAL := 12.34;
END_VAR
(*Cyclic called program code*)
fSingle := fSingle*2;

 

NaN numbers may occur in the runtime system if their actual formatting (memory content) was overwritten through illegal access (e.g. by using the MEMCPY of MEMSET functions). E.g.:

PROGRAM MAIN
VAR
    fSingle : REAL := 12.34;
END_VAR
(*Cyclic called program code*)
MEMSET( ADR( fSingle ), 16#FF, SIZEOF( fSingle ) ); (* Invalid initialization of REAL variable *)

 

Calling a conversion function with an NaN or INF number as parameter causes an FPU exception on a PC system (i368). This exception subsequently leads to the PLC being stopped. The function IsFinite() enables the value of the variables to be checked, and therefore the FPU exception to be avoided and program execution to be continued.

 

FUNCTION IsFinite : BOOL

VAR_INPUT
    x :T_Arg;
END_VAR

x: an auxiliary structure with information about the REAL or LREAL variables to be checked. The structure parameters have to be generated when IsFinite() is called from helper functions F_REAL or F_LREAL and transferred as parameters.

 

Sample of a call in ST:

In the following sample, the formatting of a REAL and an LREAL variable is checked, and an FPU exception is avoided.

PROGRAM MAIN
VAR
    fSingle         : REAL := 12.34;
    fDouble         : LREAL := 56.78;
    singleAsString  : STRING;
    doubleAsString  : STRING;
END_VAR
fSingle := fSingle*2;
IF IsFinite( F_REAL( fSingle ) ) THEN
    singleAsString := REAL_TO_STRING( fSingle );
ELSE
  (* report error !*)
 fSingle := 12.34;
END_IF

fDouble := fDouble*2;
IF IsFinite( F_LREAL( fDouble ) ) THEN
    doubleAsString := LREAL_TO_STRING( fDouble );
ELSE
  (* report error !*)
 fDouble := 56.78;
END_IF

In the following case, an FPU exception cannot be avoided through checking with IsFinite():

PROGRAM MAIN
VAR
    bigFloat  : LREAL := 3.0E100;
    smallDigit: INT;
END_VAR
IF IsFinite( F_LREAL( bigFloat ) ) THEN
    smallDigit := LREAL_TO_INT( bigFloat );
END_IF

While the bigFloat variable has the right formatting, the variable value is too large for conversion into an INT type. An exception is triggered on a PC system (i368), and the runtime system is stopped.

 

Requirements

Development environment

Target system type

PLC libraries to include

TwinCAT v2.8.0 Build > 747

TwinCAT v2.9.0 Build > 947

PC or CX (x86)

TcUtilities.Lib

( Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically)

TwinCAT v2.10.0 Build >= 1301

CX (ARM)