IEC 1131

C

Thread Starter

Costantino

easy one for you guys. I don't have the text for the standard so I ask here: given 2 integers i1 and i2 and the following operation: i1/i2 for i2>0 what data type is the result expected to be?

If you think like a PLC, you might truncate the result and get an integer. If you think like a compiler you should get a float.
 
P

Peter Nachtwey

The result will be of the same type as the operands. If you want a real result then use the interger_to_real function on both integers
before the divide.
 
According to IEC 61131-3: Programming Industrial Automation Systems, DIV can be 'overloaded', and, 'The data type of the function value is normally the same as the data type of its inputs. Exceptions are functions such as LEN . . .' The result should be an integer.

[email protected]
 
R
> The result will be of the same type as the operands. If you want a real
result then use the interger_to_real function on both integers
> before the divide.

Depends on the behavior of the compiler. Some compilers implicitly typecast all arithmetic operands to float (or double) before doing the operations, others might use the \ to designate integer division and / to designate float division, while others use the type of the resultand and the operands to determine any typecasting. Always best to do explicit typecasting unless you want compiler default.
 
Top