SA0020: Possibly assignment of truncated value to REAL variablePermalink

Detects operations on integer variables for which a truncated value could be assigned to a ‎REAL‎ data type variable

Justification: Static analysis issues an error when the result of an integer calculation is assigned to a ‎REAL‎ or ‎LREAL‎ variable. The programmer should be alerted to a possible incorrect interpretation of this kind of assignment: ‎lrealvar := dintvar1 * dintvar2‎. Because the range of values of ‎LREAL‎ is greater than that of ‎DINT‎, one could assume that the result of the calculation could always be represented in ‎LREAL‎. But that is not the case. The processor calculates the result of the multiplication as an integer and then casts the result to ‎LREAL‎. An overflow in the integer calculation would be lost. To work around the problem, the calculation has to be done as a ‎REAL‎ operation: ‎lreal_var := TO_LREAL(dintvar1) * TO_LREAL(dintvar2)‎.

Importance: High

Example

PROGRAM PLC_PRG
VAR
 rx : LREAL;
 di : DINT;
END_VAR
rx := di * di  			                // SA0020
rx := TO_LREAL(di) * TO_LREAL(di) // No message

--> SA0020: Possibly assignment of truncated value to REAL variable