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

Addition operator (+)
RETURN 10 + 5 AS result
Result
result

15

Rows: 1

Unary addition operator (+)
RETURN + 5 AS result
Result
result

5

Rows: 1

Subtraction operator (-)
RETURN 10 - 5 AS result
Result
result

5

Rows: 1

Unary subtraction operator (-)
RETURN - 5 AS result
Result
result

-5

Rows: 1

Multiplication operator (*)
RETURN 10 * 5 AS result
Result
result

50

Rows: 1

Division operator (/)
RETURN 10 / 5 AS result
Result
result

2

Rows: 1

Modulo division operator (%)
RETURN 10 % 3 AS result
Result
result

1

Rows: 1

Exponentiation operator (^)
RETURN 10 ^ 5 AS result
Result
result

100000.0

Rows: 1

Modifying properties using mathematical operators
CREATE (p:Product {price: 10})
SET p.discountPrice = p.price * (1 - 0.15)
RETURN p.discountPrice AS discountPrice
Result
discountPrice

8.5

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.

Mathematical operators: order of precedence
Precedence Operators Associativity

1

Unary negation (-), unary positive (+)

Right to left

2

Exponentiation (^)

Right to left

3

Multiplication (*), division (/), modulo division (%)

Left to right

4

Addition (+), subtraction (-)

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.

Expression with several different mathematical operations
RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % 12 AS result1,
       (((-50) + (6 * 3)) - ((100 / (5 ^ 2)) % 12)) AS result2
Result
result1 result2

-36

-36

Rows: 1

Order of evaluation
Precedence Operation Result

1

Unary negation (-50)

-50

2

Exponentiation (5 ^ 2)

25

3

Multiplication (6 * 3)

18

3

Division (100 / 25)

4

3

Modulo division (4 % 12)

4

4

Addition (-50 + 18)

-32

4

Subtraction (-32 - 4)

-36

Only bracketing some of the operations within parentheses will change the order of precedence and may, therefore, change the result of an expression.

Parenthesizing single operation
RETURN (-50 + 6) * 3 - 100 / 5 ^ 2 % 12 AS result
Result
result

-136

Rows: 1

Changed order of evaluation
Precedence Operation Result

1

Parenthesized operation (-50 + 6)

-44

2

Exponentiation (5 ^ 2)

25

3

Multiplication (-44 * 3)

132

3

Division (100 / 25)

4

3

Modulo division (4 % 12)

4

4

Subtraction (-132 - 4)

-136