4.7 Arithmetic, relational and logic operators

The following lists the operator symbols that are used for actions such as add, subtract, multiply, and others. Note: it makes a difference if these operators are used with single floatpoint values or with vectors.

Arithmetic operators

+ // = addition

- // = subtraction or negation of a value

* // = multiplication, dot product for vectors

/ // = division

% // = remainder of division (works only with integers)

Arithmetic operators and single values

For single floatpoint values these operators are used according to the basic rules of math.

1 + 1 // returns 2

1 - 1 // returns 0

2 * 2 // returns 4

4 / 2 // returns 2

Arithmetic operators and vectors

For operations between vector channels, vector variables and vector constants, the operators are treated like this:

Addition of components with +

vec( a, b, c ) + vec( d, e, f ) // returns a vector with the components ( a + d ), ( b + e ) and ( c + f )

Example:

vec( 2, 5, 90 ) + vec( 9, 89, 5 ) // returns a vector with the components 11, 94 and 95

Subtraction of components with -

vec( a, b, c ) - vec( d, e, f ) // returns a vector with the components ( a - d ), ( b - e ) and ( c - f )

Example:

vec( 2, 5, 90 ) - vec( 9, 89, 5 ) // returns a vector with the components -7, -84 and 85

Dot product of vectors with *

The dot product multiplies corresponding components of each vector, then adds the components to create a single floating point number result.

vec( a, b, c ) * vec( d, e, f ) // returns a single value which is the result of ( a * d ) + ( b * e ) + ( c * f )

Example

vec( 1, 3, 5 ) * vec( 2, 4, 6 ) // returns a single value which is ( 1 * 2 ) + ( 3 * 4 ) + ( 5 * 6 ) = 44

For operations between a vector and a single floating point value, each component of the vector is operated by the single floating point value:

Addition

vec( a, b, c ) + x // returns a vector with the components ( a + x ), ( b + x ) and ( c + x )

Example:

vec( 1, 3, 5 ) + 5 // returns a vector with the components 6, 8 and 10

Subtraction

vec( a, b, c ) - x // returns a vector with the components ( a - x ), ( b - x ) and ( c - x )

Example:

vec( 1, 3, 5 ) -2 // returns a vector with the components -1, 1 and 3

Multiplication

vec( a, b, c ) * x // returns a vector with the components ( a * x ), ( b * x ) and ( c * x )

Example:

vec( 1, 3, 5 )* 2 // returns a vector with the components 2, 6 and 10

division of vectors with /

Division of vectors with / is uncommon in programming syntax since a term like x / 4 could also be written as x * 0.25. But even though it may be uncommon, the / operator can also be used for multiplications within Xpressionist.

Relational operators

< // = less than

> // = greater than

== // = equal to

!= // = not equal to

<= // = less than or equal to

>= // = greater than or equal to

Relational operators and single values

For operations in conditional statements you often use relational operators. For single floatpoint values the relational operators are used according to the rules of algebra.

Example

if ( time > 5 )

Light_1.Intensity = 1;

When the time is greater than 5 seconds the light's intensity will be set to 1.

Relational operators and vectors

If you use the == or != operators between two vector channels, vector variables or vector constants, Xpressionist compares the corresponding components of each vector.

Example:

if ( vec( a, b, c ) == vec( d, e, f ) )

attribute = x;

This expression sets "attribute" to x, if a is equal to d, b is equal to e and c is equal to f.

In contrast, the >, >=, <, and <= operators compare the length of two vectors and not its single components.

The length of a vector vec( a, b, c ) is the square root of a^2 + b^2 + c^2. This value can also be expressed with the vector function length( vector ).

Example:

if ( vec( a, b, c ) >= vec( d, e, f ) )

property = x;

This expression sets "property" to x, if the length of vec( a, b, c ) is greater or equal to the length of vec( d, e, f )

Logical operators

&& // = and

|| // = or

These operators are often used in conditional statements together with the relational operators

Example for &&:

if ( ( time > 3 ) && ( time < 5 ) )

Light1.Intensity = 1;

This switches the light intensity to 1 when time is greater than three seconds and smaller than five seconds. Note that every condition in the statement is enclosed in brackets.

Example for ||:

if ( ( time < 3 ) || ( time > 5 ) )

Light1.Intensity = 1;

This switches the light intensity to 1 when time is smaller than three seconds or larger than 5 seconds.

String operators

$ // = variable channelname

+ // = concatenation

Strings are mostly used to create flexible expressions. You can define for example strings that contain objectnames at the top of the expression, so you do not have to change the entire expression if you change the name of that object in the Animator.

Example for $:

$left_arm = "Marys_left_arm";
$left_arm.Position = vec( 0, 0, 0 );

Together with the + operator for strings we can address many objects in a loop:

Example for $ and +

for ( i = 0; i <= 1000; i = i + 1 )
{
$obj = "cube" + i;
$obj.Opacity = i / 1000;
}

This is the same as:

cube0.Opacity = 0 / 1000;
cube1.Opacity = 1 / 1000;
cube2.Opacity = 2 / 1000;
...
cube1000.Opacity = 1000 / 1000;

Note: String variables leading with $ will not work if you want to compile your expression.

Xpressionist 3.5