Math Operators
Use math operators to perform basic arithmetic calculations involving numbers. Almost all of these operators accept real numbers, complex numbers, and matrices as operands.
Following is a list of the function of each operator:
Math Operators
| Operator | Description |
|---|---|
| Add | x+y Calculates the sum of x and y. The plus symbol can also be used to indicate the sign of a numeric literal (e.g., +5). |
| Subtract | x-y Subtracts y from x. The minus symbol is also used to enter negative numbers (e.g., -5). |
| Multiply | x*y Calculates the product of x and y. |
| Implied Multiply | kx The absence of an operator. For example, the expression 5x is treated the same as 5*x. In order to use the Implied Multiply function, k must be a numeric literal and x must be a variable, primitive, or node name. |
| Divide | x/y Divides x by y. |
| Power | x^y Returns the value of x raised to the power y. |
| Percent | x% Divides x by 100. For example, 25% is the same as 0.25. |
| Remainder | x%y Returns the remainder of dividing x by y. For example, 11%3 = 2, that is, 11 divided by 3 equals 3 with a remainder of 2. Although this operator is typically used with integer operands, it accepts floating-point values as well. |
| Increment | x++, ++xAdds one to the variable x. If the ++ symbol appears before the variable (i.e., ++x), then Increment returns the value of x after being incremented. If the ++ symbol appears after the variable (i.e., x++) then x is still incremented, but Increment returns the value of x before adding one. For example: ``` var x=0,y; |
| y=x++; | |
| watch(x,y); // x = 1 and y = 0 | |
| var x=0,y; | |
| y=++x; | |
| watch(x,y); // x = 1 and y = 1 ``` The expression x must be a variable name, list element, or object property. | |
| Decrement | x--, --x Works exactly like Increment, except Decrement subtracts one from x instead of adding one. |
| Factorial | x! Calculates the value of x*(x-1)*(x-2)*...*1. For example 5! is the same as 5*4*3*2*1, which is equal to 120. The operand x must be an integer between 0 and 171. |