Automation Engine Script Guide > Introduction > Advanced Users > Calculations

 Calculations

This document describes the arithmetic operations which can be used with AE Script.

Script Elements

AE Script provides four basic arithmetic operations in the form of script elements:

Additionally, the script function MOD can be used to retrieve the rest of a division.

If required, the script functionRANDOM can be used to generate random numbers.

The results of arithmetic operations are stored in script variables.

Example:

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

Negative numbers (data type "signed") and decimal numbers (data type "float") are supported arithmetic operations. The target variable that contains the result must be of an adequate data type. The data type "string" is not allowed for arithmetic operations.

Decimal places of results are truncated if the target variable's data type does not support decimal numbers. 

A runtime error occurs if an arithmetic operation supplies a negative result and the target variable's data type does not support algebraic signs.

Automic recommends using the operation's highest data type as the target variable's data type (see table). The operation's result must not exceed the value "9 999 999 999 999 999".

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

The highest data type is "float" (negative and positive decimal numbers). It is followed by "signed" (negative and positive integers). The lowest data type is "unsigned" which only supports positive integers.

Example: The data type of the result variable "&SUM#" must at least be "signed". Data type "float" can also be used.

: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#)

Note that arithmetic operations that include floating point numbers can cause inaccurate results.

Solving an Arithmetic Expression

In AE, you can also solve an arithmetic expression using the script element :SET and store the result in a variable. Doing so reduces and simplifies a script's length significantly.

An error occurs if the target variable's data type is "unsigned" and the operation results in a negative number. If the target variable's data type does not support decimal places, they are truncated.

An operation can include the four basic arithmetic operations,parentheses and signs.

A line that includes arithmetic operators (+,-,*,/) is handled as an expression. Do not use single or double quotation marks for the expression. Otherwise, the expression cannot be interpreted as a string.

Attention: You must not use script functions within arithmetic expressions.

Note that multiplicative operations (multiplication and division) take precedence over additive operations (addition and subtraction).

Example:

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

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

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

 

See also:

Script element Description

ADD

Performs an addition.

SUB

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

GET_BIT

Checks if a bit is set in a bit field.

Script Elements - Arithmetics

About Scripts
Script Elements - Alphabetical Listing

Script Elements - Ordered by Function