SA0057: Possible loss of decimal places

Detects statements with possible loss of decimal places

Justification: A piece of code of the following type (diTemp2 := 1 rTemp1 := TO_REAL(diTemp2 / DINT#2)) can cause a misinterpretation. The author or reader of this line of code can assume that the division would be performed as a REAL operation, and in this case the result would be REAL#0.5. However, this is not true. It is an integer operation. The result is cast to REAL and rTemp1 gets the value REAL#0.

To avoid this, use a cast to make sure that the operation is performed as a REAL operation: rTemp1 := TO_REAL(diTemp2) / REAL#2.

Importance: Medium

Example

PROGRAM PLC_PRG
VAR
        rTemp1 : REAL;
        diTemp2 : DINT;
        liTemp3 : LINT;
END_VAR

diTemp2 := diTemp2 + DINT#11;
rTemp1 := DINT_TO_REAL(diTemp2 / DINT#3);    // SA0057
rTemp1 := DINT_TO_REAL(diTemp2) / REAL#3.0;
liTemp3 := liTemp3 + LINT#13;
rTemp1 := LINT_TO_REAL(liTemp3 / LINT#7);    // SA0057
rTemp1 := LINT_TO_REAL(liTemp3) / REAL#7.0;


--> SA0057: Möglicherweise Verlust von Nachkommastellen