SA0020: Possibly assignment of truncated value to REAL variable
Function | Determines operations on integer variables, during which a truncated value may be assigned to a variable of data type REAL. |
Reason | The static code analysis returns an error when the result of an integer calculation is assigned to a REAL or LREAL variable. The programmer should be made aware of a possibly incorrect interpretation of such an assignment:
Since the value range of LREAL is greater than that of DINT, it could be assumed that the result of the calculation is always displayed in LREAL. But this 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 avoid this problem, the calculation should be performed as a REAL operation:
|
Importance | High |
Sample:
PROGRAM MAIN
VAR
nVar1 : DWORD;
nVar2 : DWORD;
fVar : REAL;
END_VARnVar1 := nVar1 + DWORD#1;
nVar2 := nVar2 + DWORD#2;
fVar := nVar1 * nVar2; // => SA0020