11.3.1 Introduction
The arithmetic functions for numerical values link two values according to the basic arithmetical operations addition, subtraction, multiplication, and division (Fig. 11.9). The arithmetic functions include absolute value generation, changing the sign (negation), and changing the value by 1 (decrementing, incrementing).
LAD and FBD: An arithmetic function is executed if the enable input EN is not con- nected, or if “1” is present at the enable input, or if “current” flows into the enable
Fig. 11.9 Arithmetic functions for numerical values, representation and function
EN EN
OUT OUT
ENO
ENO
Arithmetic functions for numerical values
IN1 IN1
IN2 IN2
Function:
LAD/FBD SCL
ADD + Addition
SUB – Subtraction
MUL * Multiplication
DIV / Division
MOD MOD Division with remainder as result
Data type: USINT, UINT, UDINT, SINT, INT, DINT
REAL, LREAL(not with MOD) Name
EN ENO IN1 IN2 OUT
– – INPUT INPUT OUTPUT
BOOL BOOL Data type Data type Data type SCL OUT := IN1 Function IN2;
Declaration
Enabling input Enabling output Input tag 1 Input tag 2 Result Description Data type
Data type Data type
Function Function
LAD FBD
If different data types are specified, the result adopts the “most powerful”
data type.
input. If an error occurs during calculation, the enable output ENO is set to “0”, oth- erwise to “1”. If the execution of the function is not enabled (EN = “0”), the calcula- tion does not take place and ENO is also “0”.
SCL: The basic arithmetical operations are implemented with an arithmetic expres- sion in which two or more tags are linked. The result of an arithmetic expression can in turn be used in another arithmetic expression. If, during the execution of an arithmetic function, an error such as exceeding a number range occurs, the ENO tag is set to FALSE (signal state “0”).
11.3.2 Addition ADD
The ADD function interprets the values present at the IN1 and IN2 inputs as num- bers with the specified data type. It adds the two numbers, and saves the total at the OUT output. Leaving the permissible range is reported by ENO = “0”.
Floating-point addition: with an impermissible calculation (one of the input values is an invalid floating-point number, or you attempt to add + and -), ADD delivers an invalid value at the OUT output and sets ENO to “0”.
11.3.3 Subtraction SUB
The SUB function interprets the values present at the IN1 and IN2 inputs as num- bers with the specified data type. It subtracts the value at IN2 from the value at IN1, and saves the difference at the OUT output. Leaving the permissible range is report- ed by ENO = “0”.
Floating-point subtraction: with an impermissible calculation (one of the input val- ues is an invalid floating-point number, or you attempt to subtract + from + or -
from - ), SUB delivers an invalid value at the OUT output and sets ENO to “0”.
11.3.4 Multiplication MUL
The MUL function interprets the values present at the IN1 and IN2 inputs as num- bers with the specified data type. It multiplies the two numbers, and saves the prod- uct at the OUT output. Leaving the permissible range is reported by ENO = “0”.
Floating-point multiplication: In an unauthorized calculation (one of the input val- ues is an invalid floating point number, or you try to multiply with 0), MUL sup- plies an invalid value at OUT and sets ENO to “0”.
11.3.5 Division DIV
Fixed-point division: the DIV function interprets the values present at the IN1 and IN2 inputs as numbers with the specified data type. It divides the value at input IN1 (dividend) by the value at input IN2 (divisor) and delivers the quotient at the OUT output. This is the integer result of the division. The quotient is zero if the div- idend is equal to zero and the divisor is not equal to zero, or if the value of the div-
idend is smaller than the value of the divisor. The quotient is negative if the divisor is negative. A division by zero delivers a value of zero as the quotient, and sets ENO to “0”.
Floating-point division: the value at the IN1 parameter is divided by the value at the IN2 parameter, and the result output at the OUT parameter. With an impermissible calculation (one of the input values is an invalid floating-point number, or you at- tempt to divide by or 0 by 0), DIV delivers an invalid value at the OUT output and sets ENO to “0”.
11.3.6 Division with remainder as result MOD
The MOD function interprets the values present at the IN1 and IN2 inputs as numbers with the specified data type. It divides the value at input IN1 (dividend) by the value at input IN2 (divisor) and saves the remainder of the division at the OUT output. The remainder refers to the remaining part of the division, and does not correspond to the decimal positions. With a negative dividend, the remainder is also negative.
A division by zero delivers a value of zero as the remainder, and sets ENO to “0”.
The MOD function does not allow tags of data type REAL or LREAL.
11.3.7 Generation of absolute value ABS
The ABS function generates the absolute value from the number at the IN parame- ter and outputs the result at the OUT parameter. With a floating-point number, the sign of the mantissa is set to “0”, even if the number is invalid.
If the allowed number range is exceeded, for example ABS(–128) for data type SINT, or for a valid floating-point number, the ENO output has signal state “0”
(Fig. 11.10).
Fig. 11.10 Absolute value generation and negation, representation and function
EN EN
OUT
OUT ENO
ENO Generation of absolute value, negation
IN IN
Function:
ABS NEG
Generation of absolute value Negation
Data type:
SINT, INT, DINT, REAL, LREAL Declaration Name
EN ENO IN OUT
– – INPUT OUTPUT
BOOL BOOL Data type Data type
Description Data type
SCL OUT := ABS(IN1); //Generation of absolute value OUT := -IN; //Negation (two's complement)
Enabling input Enabling output Input tag Result
LAD Function FBD
Data type
Function Data type
11.3.8 Negation NEG
The NEG function reverses the sign of the number at the IN parameter and outputs the result at the OUT parameter. The negation is equivalent to a multiplication by – 1. With a floating-point number, the sign of the mantissa is changed, even if the num- ber is invalid (Fig. 11.10).
If the result is out of the valid number range, e.g .NEG(–128) for the data type SINT, the enable output ENO is set to signal state “0”.
11.3.9 Decrement DEC, increment INC
The function DEC (decrement) reduces the value at the IN/OUT parameter by 1 as in a subtraction. The function INC (increment) increases the value at the IN/OUT parameter by 1 as in an addition (Fig. 11.11).
LAD, FBD: When reaching the lowest and highest numerical value for the data type, the enable output ENO is set to signal state “0”.
SCL: If the allowed number range is exceeded, the ENO tag is set to FALSE (signal state “0”).