Floating Point Values

A calculation with floating points can lead to the following values:

0 (zero)

If a calculation results in an underrun, the value is set to 0 (result near 0, but not presentable). Depending on the sign bit, it can be a positive zero or a negative zero. The operator “=” of -0 and 0 returns TRUE.

Infinity

If a calculation results in an overrun, the value is set to Infinity (the result is not presentable). Depending on the sign bit, it can be a positive infinity (Infinity) or negative infinity (-Infinity).

If Infinity is converted into another data type it results in the maximum value of the other data type (e.g.. conversion into DWORD with REAL_TO_DWORD: 16#FFFFFFFF, into DINT with REAL_TO_DINT: 16#7FFFFFFF).

If -Infinity is converted into another data type it results in the maximum value of the other data type (e.g.. conversion into DWORD with REAL_TO_DWORD: 16#00000000, into DINT with REAL_TO_DINT: 16#80000000).

Except for:

TRUE := REAL_TO_BOOL(Infinity);

'#Inf' := REAL_TO_STRING(Infinity);

'-#Inf' := REAL_TO_STRING(-Infinity);

Examples:

Infinity -Infinity
Infinity := 1.0 / 0.0 -Infinity := -1.0 / 0.0
Infinity := Infinity + Infinity -Infinity := -Infinity -Infinity
Infinity := Infinity + 1.0 -Infinity := -Infinity + 1.0
Infinity := LREAL_TO_REAL(Infinity) -Infinity := LREAL_TO_REAL(-Infinity)

NaN

If a calculation results in an undefined value the result is set to NaN (Not a Number). The result of each calculation with NaN is NaN. The operators “<”, “<=”, “>” and “>=” return FALSE if either or both operands are NaN.

Operator “=” returns FALSE if one operand is NaN.

Operator “<>” returns TRUE if one operand is NaN.

If NaN is converted into another data type the result is 0.

Except for:

TRUE := REAL_TO_BOOL(NaN);

'#NaN' := REAL_TO_STRING(NaN);

Examples:

NaN := SQRT(-2.0)

NaN := 0.0 / 0.0

NaN := Infinity -Infinity

NaN := 0.0 * Infinity

NaN := Inifnity / Infinity

The result of an operation can be checked with the following program parts:

Check for NaN (REAL): Check for NaN (LREAL):

rX: REAL;

IF (rX <> rX) THEN

(* rX is a NaN *)

...;

END_IF;

lrX: LREAL;

IF (lrX <> lrX) THEN

(* lrX is a NaN *)

...;

END_IF;

Check for Infinity (REAL): Check for Infinity (LREAL):

Infinity is represented with sign bit 0, exponent of all 1s and a fraction of all 0s.

-Infinity is represented with sign bit 1, exponent of all 1s and a fraction of all 0s.

rX:  REAL;

prX: POINTER TO REAL;

pdwX: POINTER TO DWORD;

prX := ADR(rX);

pdwX := prX;

IF (pdwX^ = 16#7F800000) THEN

(* rX is Infinity *)

...;

END_IF;

IF (pdwX^ = 16#FF800000) THEN

(* rX is -Infinity *)

...;

END_IF;

lrX: LREAL;

plrX: POINTER TO LREAL;

plwX: POINTER TO LWORD;

plrX := ADR(lrX);

plwX := plrX;

IF (plwX^ = 16#7FF0000000000000) THEN

(* lrX is Infinity *)

...;

END_IF;

IF (plwX^ = 16#FFF0000000000000) THEN

(* lrX is -Infinity *)

...;

END_IF;