Arithmetic operation supplies incorrect result

Question:

Why is the result of an arithmetic operation wrong, although it is within the permissible range of values of the variables at the output?

Answer:

When arithmetic calculations are made, the corresponding range of values is decisive for obtaining a correct result. The Servo PLC / Drive PLC determines this range of values on the basis of the variables' data types, which are situated at the inputs of the operation. If the result of the calculation is not within this range of values, a data type overflow will occur, this means, the operation will show a calculation result deviating from the actual result.

Example:
dnOut : DINT;
nIn : INT;

dnOut := nIn + 1;
With nIn = 32767 (maximum positive value of the INT data type) the addition supplies the result dnOut = -32768, as due to the variables' data type nIn the calculation is made within the range of values of INT.

Variables of different data types
Concerning arithmetic calculations with variables of different data types, Lenze recommends to always apply the corresponding functions for the type conversion in such a way that at the operation only similar data types are applied. Thus the range of values to be applied can be clearly determined by the PLC.

Example:
sIn1 : SINT;
unIn2 : UINT;
dnOut : DINT;

dnOut := SINT_TO_DINT( sIn1 ) + UINT_TO_DINT( unIn2 );
This calculation is effected in the range of values of DINT.

URL for linking this AKB article: https://www.lenze.com/en-de/go/akb/20010150/1/
Formulario de contacto