Arithmetic operators
- Last UpdatedSep 05, 2024
- 5 minute read
Arithmetical operators perform arithmetic operations with numerical operands.
Arithmetic operations are evaluated from left to right and parenthesis can be used to prioritize. Numerical values are internally treated as double.
int a = 13 / 5 / 2; //a = 1;
int b = 13 / (5 / 2); //b = 5; b should be 6 but will be 5 because numbers are internally double and cast to int only when they get written to the variable
Runtime::Environment.Print(": " + 13/(5/2)); //5.2; this returns a double number as numbers are all double by default
int e = 13;
int f = 5 / 2; //f = 2
int g = e / f; //g = 6; this is correct because both are variables and so they are typed
Arithmetical operators support complex arithmetic operations with operands of different types. The following table shows the combinations supported. Since some combinations are not commutative, first column corresponds to left-hand operands and first row corresponds to right-hand operand.
|
|
int |
double |
vector2 |
vector3 |
vector4 |
quaternion |
matrix |
|
int |
+ - * / % |
+ - * / % |
* |
* |
* |
|
|
|
double |
+ - * / % |
+ - * / % |
* |
* |
* |
|
|
|
vector2 |
* / |
* / |
+ - * |
|
|
|
|
|
vector3 |
* / |
* / |
|
+ - * |
|
|
* |
|
vector4 |
* / |
* / |
|
|
+ - |
|
|
|
quaternion |
|
|
|
|
|
|
|
|
matrix |
|
|
|
* |
|
|
* |
Numerical types
Int and double types support all arithmetic operators:
-
+ operator, which calculates sum of the right-hand operand to the left-hand operand.
-
- operator, which subtracts the right-hand operand from the left-hand operand.
-
* operator, which multiplies the right-hand operand by the left-hand operand.
-
/ operator, which divides the left-hand operand by the right-hand operand.
A numerical operand can be multiplied by a vector2, vector3 or vector4 type value. This is equivalent to a scalar multiplication, where each component of the vector is multiplied by the scalar value. The resulting vector has a magnitude adjusted by the numerical value and a direction determined by its sign.
In addition, numerical types support:
-
++ operator, which computes a postfix increment operation.
-
-- operator, which computes a postfix decrement operation
-
unary – operator, which returns the numeric negation of its operand.
-
% operator, which returns the remainder of the division.
string
String type only supports operator +, which is used to perform a string concatenation.
vector2
Arithmetic operations among vector2 include:
-
+ operator, which returns a vector2 whose components are calculated by summing the respective components of the right-hand operand to the left-hand operand.
-
- operator, which returns a vector2 whose components are calculated by subtracting the corresponding components of right-hand operand from the left-hand operand.
-
* operator, which performs the dot product of the right-hand operand by the left-hand operand and returns a scalar value.
A vector2 value can be also multiplied or divided by a numerical operand. This is equivalent to a scalar multiplication, where each component of the vector2 is multiplied or divided by the scalar value. The resulting vector2 has a magnitude adjusted by the numerical value and a direction determined by its sign.
In addition, vector2 type supports unary – operator, which returns the numeric negation of its operand.
vector3
Arithmetic operations among vector3 include:
-
+ operator, which returns a vector3 whose components are calculated by summing the corresponding components of the right-hand operand to the left-hand operand.
-
- operator, which returns a vector3 whose components are calculated by subtracting the corresponding components of right-hand operand from the left-hand operand.
-
* operator, which performs the dot product of the right-hand operand by the left-hand operand and returns a scalar value.
-
^ operator, which performs the cross product of the right-hand operand by the left-hand operand and returns a vector3.
A vector3 value can be also multiplied or divided by a numerical operand. This is equivalent to a scalar multiplication, where each component of the vector3 is multiplied or divided by the scalar value. The resulting vector3 has a magnitude adjusted by the numerical value and a direction determined by its sign.
Moreover, a vector3 value can be multiplied by a matrix. For dimension compatibility, vector3 is considered as a vector4 with w=1, in the vector-matrix product. The result is a vector3, where each component is the dot product of the vector3 with each column of the matrix.
It is worth noting that the vector3-matrix product is not commutative. In fact, the result of a matrix-vector product is a vector3, where each component is the dot product of the each row of the matrix with the vector3. Also, in this case vector3 is considered as a vector 4 with w=1, for dimension compatibility.
In addition, vector3 type supports unary – operator, which returns the numeric negation of its operand.
vector4
Arithmetic operations among vector4 include:
-
+ operator, which returns a vector4 whose components are calculated by summing the corresponding components of the right-hand operand to the left-hand operand.
-
- operator, which returns a vector3 whose components are calculated by subtracting the corresponding components of right-hand operand from the left-hand operand.
A vector4 value can be also multiplied or divided by a numerical operand. This is equivalent to a scalar multiplication, where each component of the vector4 is multiplied or divided by the scalar value. The resulting vector4 has a magnitude adjusted by the numerical value and a direction determined by its sign.
In addition, vector4 type supports unary – operator, which returns the numeric negation of its operand.
quaternion
Quaternion type only supports unary – operator, which returns the numeric negation of its operand.
matrix
Arithmetic operations among matrix values only include the * operator, which performs the dot product of the right-hand operand by the left-hand operand and returns a matrix. Each component of the resulting matrix is the dot product of the each row of the left-hand operand with with each column of the right-hand operand.
A matrix can also be multiplied by a vector3. For dimension compatibility, vector3 is considered as a vector 4 with w=1. , the result is a vector3, where each component is the dot product of the each row of the matrix with the vector3.
It is worth noting that the matrix-vector product is not commutative. In fact, the result of a vector-matrix product is a vector3, where each component is the dot product of the vector3 with each column of the matrix. Also in this case vector3 is considered as a vector 4 with w=1, for dimension compatibility.
In addition, matrix type supports unary – operator, which returns the numeric negation of its operand.