Product help with community knowledge

### Page statistics

884 views1 edit(s)15008 characters(s) Page last modified 18:12, 12 Apr 2012 by contentconnector

# Arithmetic Operators

The arithmetic operators include the *, /, ^, +, and - operators.

## Unary Negation Operator

The unary negation operator (-) is defined for Integer, Number, and Vector types. All other operand types will generate an error. For numeric types, the result is obtained by subtracting the operand from zero. For Boolean, the operand is compared to False. For Vector, the operand is reversed (unary negation applied to all coordinates).

The following table shows examples of the unary negation operator.

Operand Type Example Result
Numeric -3.0 -3.0
Vector -vector(0,0,1) Vector_(0.0, 0.0, -1.0, WorldFrame())

The addition operator (+) computes the combination of two operands. The addition operator is defined for the following pairs of types (output type is also shown):

• Integer + Integer => Integer
• Integer + Number => Number
• Number + Number => Number
• List + List => List
• Point + Vector => Point
• Vector + Vector => Vector
• Name + Name => Name
• Name + String => String

Order is not significant.

For numeric arguments, numeric addition is performed. For List arguments, the lists are concatenated, in order. For Name and String arguments, any Name values are converted to String, and the strings are concatenated in order.

The following table shows examples of the addition operator.

Operand Type Example Result
Names :My + :Name :MyName
Name and String :My + "Name" "MyName"
Mixed Numeric 12 + 4.5 + 3.1 19.6
Lists {:A, :B, :C} + {1, 2.0} {:a, :b, :c, 1, 2.0}
Point and Vector Point(0, 0, 0) + Vector(0, 0, 1) Point_(0.0, 0.0, 1.0, WorldFrame())

## Subtraction Operator

The subtraction operator (-) computes the difference of two operands. The subtraction operator is defined for the following pairs of types (output type is also shown):

• Integer - Integer => Integer
• Integer - Number => Number
• Number - Number => Number
• Number - Integer => Number
• Point - Point => Vector
• Point - Vector => Point
• Vector - Vector => Vector

Because order does matter, no other combinations are permitted, particularly Vector-Point.

For numeric arguments, numeric subtraction is performed. For geometric arguments, vector operations are performed.

The following table shows examples of the subtraction operator.

Expression Example Result
Point - Point Point(0, 0, 0) - Point(10, 0, 0) Vector_(-10.0, 0.0, 0.0, WorldFrame())
Point - Vector Point(0, 0, 0) - Vector(1, 0, 0) Point_(-1.0, 0.0, 0.0, WorldFrame())
Vector - Vector   Vector_(1.0, -1.0, 0.0, WorldFrame())

## Multiplication Operator

The multiplication operator (*) computes the product of two operands. The multiplication operator is defined for the following types (output type is also shown):

• Frame * Frame => Frame
• Integer * Integer => Integer
• Integer * Number => Number
• Number * Number => Number
• Integer * Vector => Vector
• Number * Vector => Vector
• Vector * Vector => Vector

For some types of operands, the order changes the result, but not the type of the result.

For numeric operands, numeric multiplication is performed. For Scalar*Vector, the vector is scaled. For Vector*Vector, the vector cross product is performed. For Frame*Frame, a combination analogous to matrix multiplication is performed.

The following table shows examples of the multiplication operator.

Operand Type Example Result
Numeric and Vector 3.0 * Vector(1,0,0) Vector_(3.0, 0.0, 0.0, WorldFrame())
Vector and Vector Vector(1,0,0) * Vector(0,1,0) Vector_(0.0, 0.0, 1.0, WorldFrame())

## Division Operator

The division operator (/) computes the quotient of two operands. The division operator is defined for the following types (output type is also shown):

• Integer / Integer => Number
• Integer / Number => Number
• Number / Number => Number
• Number / Integer => Number
• Vector / Integer => Vector
• Vector / Number => Vector

Since order matters, the Vector operands are not defined in the reverse order.

Note that numeric division always results in a Number, even when the number represents an integer value. The Vector operations perform scaling of the Vector.

The following table shows examples of the division operator.

Operand Type Example Result
Number and Integer 42.0 / 6 7.0
Vector and Integer Vector(1,0,0)/3 vector(0.33333, 0.0, 0.0, WorldFrame())

## Exponentiation Operator

The exponentiation operator (^) computes the first operand raised to the power of the second operand. The exponentiation operator is defined for the following types (output type is also shown):

• Integer ^ Integer => Number
• Integer ^ Number => Number
• Number ^ Number => Number

Order can affect the result, but not the type of the result.

The result is always of type Number.

The following table shows examples of the exponentiation operator.

Operand Type Example
Integer and Integer 6^2 36.0
Integer and Number 6^2.0 36.0
Number and Number 6.0^2.0 36.0