SA0057: Possible loss of decimal points

Function

Determines statements with possible loss of decimal points.

Reason

A piece of code of the following type:

nDINT := 1;
fREAL := TO_REAL(nDINT / DINT#2);

can lead to misinterpretation. This line of code can lead to the assumption that the division would be performed as a REAL operation and the result in this case would be REAL#0.5. However, this is not the case, the operation is performed as an integer operation, the result is cast to REAL and fREAL receives the value REAL#0. To avoid this, you should use a cast to ensure that the operation is performed as a REAL operation:

fREAL := TO_REAL(nDINT) / REAL#2;

Importance

Medium

Samples:

PROGRAM MAIN
VAR
    fREAL : REAL;
    nDINT : DINT;
    nLINT : LINT;
END_VAR
nDINT := nDINT + DINT#11;
fREAL := DINT_TO_REAL(nDINT / DINT#3);              // => SA0057
fREAL := DINT_TO_REAL(nDINT) / 3.0;                 // no error
fREAL := DINT_TO_REAL(nDINT) / REAL#3.0;            // no error
 
nLINT := nLINT + LINT#13;
fREAL := LINT_TO_REAL(nLINT / LINT#7);              // => SA0057
fREAL := LINT_TO_REAL(nLINT) / 7.0;                 // no error
fREAL := LINT_TO_REAL(nLINT) / REAL#7.0;            // no error