Arithmetic Calculations in Scripts

The Automation Engine scripting language provides functions for calculating values in your scripts. Arithmetic functions store values in script variables. Use calculated values to automate various steps in your processes. For an overview of available script functions for performing calculations, see Script Functions for Calculations.

Example

The following script defines a variable as the difference between two values.

:SET &DIFFERENCE# = SUB(100,50)

Note: The usual order of operations applies: multiplication and division take precedence over additional and subtraction

Warnings:

  • Do not use arithmetic script functions within arithmetic expressions.
  • The result of the operation must not exceed the value 9 999 999 999 999 999.

Data Types in Arithmetic Calculations

Keep in mind that the declared data types of variables place restrictions on the values in your arithmetic calculations:

  • You cannot use variables that are declared as strings in arithmetic operations.
  • Negative numbers are supported if the variable is declared as a data type that allows negative numbers. If the data type does not support algebraic signs, a negative result will cause a runtime error .
  • Decimals are supported if the variable is declared as a data type that allows decimals. If the data type of the target variable does not support decimals, the decimal places of results are truncated.

For more information, see Script Variable Data Types

Tip: Use the highest data type in the operation as the data type of the target variable.

Data types of operands Allowed data types for the target variable
unsigned, unsigned unsigned, signed, float
unsigned, signed signed, float
unsigned, float float
signed, signed signed, float
signed, float float
float, float float

Example

The data type of the result variable &SUM# must be signed or float:

:DEFINE &UNSIGNED#,unsigned
:DEFINE &SIGNED#,signed
:DEFINE &SUM#,signed

:SET &UNSIGNED# = 12
:SET &SIGNED# = -5

:SET &SUM# = ADD(&SIGNED#,&UNSIGNED#)

The following line would result in a scripting error:

:SET &UNSIGNED# = ADD(&SIGNED#,&UNSIGNED#)

Solving Arithmetic Expressions

You can also solve arithmetic expressions by using the :SET script statement and storing the result in a variable. Doing so reduces and simplifies a script's length significantly.

You can use the four basic arithmetic operations, parentheses and signs in arithmetic expressions:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Parentheses
  • Signs

Lines that include arithmetic operators (+,-,*,/) are handled as expressions. Do not use single or double quotation marks for such expressions as the system does not interpret the expression as a string otherwise.

Examples

:DEFINE &UNSIGNED#,unsigned
:DEFINE &FLOAT#,float
:DEFINE &RES#,float

:SET &UNSIGNED# = 12
:SET &FLOAT# = -0.50

:SET &RES# = &FLOAT#*3 + (-&UNSIGNED#) - 3

See also:

seealso

Writing Scripts