DIV

ScriptEigener Objekttyp in der Automation Engine Function: It performs a division.

Syntax

DIV(Operand1, Operand2)

Syntax

Description/Format

Operand1

The first expression.
Format: script literal, script variable or number specified without quotation marks.

Operand2

The second expression.
Format: script literal, script variable or number specified without quotation marks.


Return code

Result of the division

Comments

The script function divides Operand1 by Operand2.

Operand1 and  Operand2  must be expressions which correspond to numbers within the allowed value range of the data types. The result must not exceed this range.

The Operand2 must not be zero.You can assign the result to a script variable but it must show the corresponding data type. Any attempt to store a negative number to a variable of the data type "unsigned" results in an error. If the result is a floating-point number and the target variable is of data type "signed" or "unsigned", the decimal positions are rounded up or down to the nearest whole number. The data type "float" supports negative numbers and floating-point numbers. The data type "string" can also be used. In this case, the result is stored as a string instead of a number. The data type of script variables can be defined by using the script element DEFINE

The result's data type is decisive and not the operand's data type. Two negative operands result in a positive number which can then be stored in a target variable of the data type "unsigned".

Note that arithmetic operations using floating-point numbers can supply inexact results.

The result is returned in the 16-digit default format. Floating-point numbers also include 16 decimal places and an algebraic sign (+ or -) is used as the first sign if negative numbers are supported. Formatting can be changed by using the script function  FORMAT.

Examples

The first example passes the division result "5" on to a script variable. Script variables are used as Operand1 and Operand2.

:SET &OP1# = '100'
:
SET &OP2# = '20'
:
SET &RESULT# = DIV(&OP1#,&OP2#)

Decimal places are rounded up or down to the nearest whole number if the target variable's data type is not "float. Therefore, the value "0" is stored to the variable in the following example.

:DEFINE &RESULT#, unsigned
:
SET &RESULT# = DIV(10,30)

The following example shows a division with floating-point numbers. 

:DEFINE &RESULT#,float
:SET &RESULT#DIV(-9,-2.25)
:P &RESULT#

The following result is output in the activation protocol:

U0020408 +0000000000000004.0000000000000000

Another Way to Perform Division

Another way to perform division is shown below. In this example, the value of a script variable is set to the result of 10 divided by 2.

:SET &DIVIDE# = 10 * 2
:
P &DIVIDE#

The following result is output in the activation protocol:

U0020408 0000000000000005

See also:

Script element Description

ADD

Performs an addition.
SUB Performs a subtraction.
MULT Performs a multiplication.
MOD Returns the remainder of a division.
RANDOM Generates random numbers.

Script Elements - Arithmetics

About Scripts
Script Elements - Alphabetical Listing

Script Elements - Ordered by function