SA0020: Possibly assignment of truncated value to REAL variable
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 value range 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
PROGRAM PLC_PRG VAR rX : LREAL; dI : DINT; END_VAR
rX := dI * dI // SA0020 rX := TO_LREAL(dI) * TO_LREAL(dI) //no message
Output in the Messages view:
SA0020: Possibly assignment of truncated value to REAL variable