Mathematical operators
Cypher® contains the following mathematical operators:
-
Addition or unary addition:
+
-
Subtraction or unary minus:
-
-
Multiplication:
*
-
Division:
/
-
Modulo division:
%
-
Exponentiation:
^
For additional mathematical expressions, see:
Examples
+
)RETURN 10 + 5 AS result
result |
---|
|
Rows: 1 |
+
)RETURN + 5 AS result
result |
---|
|
Rows: 1 |
-
)RETURN 10 - 5 AS result
result |
---|
|
Rows: 1 |
-
)RETURN - 5 AS result
result |
---|
|
Rows: 1 |
*
)RETURN 10 * 5 AS result
result |
---|
|
Rows: 1 |
/
)RETURN 10 / 5 AS result
result |
---|
|
Rows: 1 |
%
)RETURN 10 % 3 AS result
result |
---|
|
Rows: 1 |
^
)RETURN 10 ^ 5 AS result
result |
---|
|
Rows: 1 |
CREATE (p:Product {price: 10})
SET p.discountPrice = p.price * (1 - 0.15)
RETURN p.discountPrice AS discountPrice
discountPrice |
---|
|
Rows: 1 |
Order of precedence
The following table details the order of precedence of the mathematical operators in ascending order.
Note, the lower the precedence level, the higher the binding power of the operands.
Also note, if parentheses ()
are used, any operation within them takes precedence, overriding the default order of precedence.
Precedence | Operators | Associativity |
---|---|---|
1 |
Unary negation ( |
Right to left |
2 |
Exponentiation ( |
Right to left |
3 |
Multiplication ( |
Left to right |
4 |
Addition ( |
Left to right |
Operators within the same precedence group are evaluated based on associativity.
The order of precedence ensures that the following two expressions return the same result.
RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % 12 AS result1,
(((-50) + (6 * 3)) - ((100 / (5 ^ 2)) % 12)) AS result2
result1 | result2 |
---|---|
|
|
Rows: 1 |
Precedence | Operation | Result |
---|---|---|
1 |
Unary negation ( |
|
2 |
Exponentiation ( |
|
3 |
Multiplication ( |
|
3 |
Division ( |
|
3 |
Modulo division ( |
|
4 |
Addition ( |
|
4 |
Subtraction ( |
|
Only bracketing some of the operations within parentheses will change the order of precedence and may, therefore, change the result of an expression.
RETURN (-50 + 6) * 3 - 100 / 5 ^ 2 % 12 AS result
result |
---|
|
Rows: 1 |
Precedence | Operation | Result |
---|---|---|
1 |
Parenthesized operation ( |
|
2 |
Exponentiation ( |
|
3 |
Multiplication ( |
|
3 |
Division ( |
|
3 |
Modulo division ( |
|
4 |
Subtraction ( |
|