Skip to content

Commit f961232

Browse files
Expressions/Mathematical operators (#1211)
1 parent 32a19b9 commit f961232

File tree

8 files changed

+263
-58
lines changed

8 files changed

+263
-58
lines changed

modules/ROOT/content-nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
*** xref:expressions/predicates/string-operators.adoc[]
7474
*** xref:expressions/predicates/path-pattern-expressions.adoc[]
7575
*** xref:expressions/predicates/type-predicate-expressions.adoc[]
76+
** xref:expressions/mathematical-operators.adoc[]
7677
** xref:expressions/conditional-expressions.adoc[]
7778
7879
* xref:functions/index.adoc[]

modules/ROOT/pages/appendix/gql-conformance/supported-mandatory.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ For example, `RETURN sum(<expr>)` on an empty table returns `NULL` in GQL, but i
185185

186186
| 20.21
187187
| <numeric value expression>
188-
| xref:syntax/operators.adoc#query-operators-mathematical[Mathematical operators]
188+
| xref:expressions/mathematical-operators.adoc[Mathematical operators]
189189
|
190190

191191
| 20.22

modules/ROOT/pages/expressions/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ For details and examples of specific expressions, see the following sections:
1111
** xref:expressions/predicates/string-operators.adoc[]: `STARTS WITH`, `ENDS WITH`, `CONTAINS`, `IS NORMALIZED`, `IS NOT NORMALIZED`, `=~`
1212
** xref:expressions/predicates/path-pattern-expressions.adoc[]: information about filtering queries with path pattern expressions.
1313
** xref:expressions/predicates/type-predicate-expressions.adoc[]: information about how to verify the value type of a Cypher expression.
14+
* xref:expressions/mathematical-operators.adoc[]: `+`, `-`, `*`, `/`, `%`, `^`.
1415
* xref:expressions/conditional-expressions.adoc[]
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
= Mathematical operators
2+
:description: Information about Cypher's mathematical operators.
3+
:table-caption!:
4+
5+
Cypher contains the following mathematical operators:
6+
7+
* Addition or unary addition: `+`
8+
* Subtraction or unary minus: `-`
9+
* Multiplication: `*`
10+
* Division: `/`
11+
* Modulo division: `%`
12+
* Exponentiation: `^`
13+
14+
For additional mathematical expressions, see:
15+
16+
* xref:functions/mathematical-logarithmic.adoc[Logarithmic functions]
17+
* xref:functions/mathematical-numeric.adoc[Numeric functions]
18+
* xref:functions/mathematical-trigonometric.adoc[Trigonometric functions]
19+
20+
[[examples]]
21+
== Examples
22+
23+
.Addition operator (`+`)
24+
[source, cypher]
25+
----
26+
RETURN 10 + 5 AS result
27+
----
28+
29+
.Result
30+
[role="queryresult",options="header,footer",cols="1*<m"]
31+
|===
32+
| result
33+
34+
| 15
35+
36+
1+d|Rows: 1
37+
|===
38+
39+
.Unary addition operator (`+`)
40+
[source, cypher]
41+
----
42+
RETURN + 5 AS result
43+
----
44+
45+
.Result
46+
[role="queryresult",options="header,footer",cols="1*<m"]
47+
|===
48+
| result
49+
50+
| 5
51+
52+
1+d|Rows: 1
53+
|===
54+
55+
56+
.Subtraction operator (`-`)
57+
[source, cypher]
58+
----
59+
RETURN 10 - 5 AS result
60+
----
61+
62+
.Result
63+
[role="queryresult",options="header,footer",cols="1*<m"]
64+
|===
65+
| result
66+
67+
| 5
68+
69+
1+d|Rows: 1
70+
|===
71+
72+
.Unary subtraction operator (`-`)
73+
[source, cypher]
74+
----
75+
RETURN - 5 AS result
76+
----
77+
78+
.Result
79+
[role="queryresult",options="header,footer",cols="1*<m"]
80+
|===
81+
| result
82+
83+
| -5
84+
85+
1+d|Rows: 1
86+
|===
87+
88+
.Multiplication operator (`*`)
89+
[source, cypher]
90+
----
91+
RETURN 10 * 5 AS result
92+
----
93+
94+
.Result
95+
[role="queryresult",options="header,footer",cols="1*<m"]
96+
|===
97+
| result
98+
99+
| 50
100+
101+
1+d|Rows: 1
102+
|===
103+
104+
.Division operator (`/`)
105+
[source, cypher]
106+
----
107+
RETURN 10 / 5 AS result
108+
----
109+
110+
.Result
111+
[role="queryresult",options="header,footer",cols="1*<m"]
112+
|===
113+
| result
114+
115+
| 2
116+
117+
1+d|Rows: 1
118+
|===
119+
120+
.Modulo division operator (`%`)
121+
[source, cypher]
122+
----
123+
RETURN 10 % 3 AS result
124+
----
125+
126+
.Result
127+
[role="queryresult",options="header,footer",cols="1*<m"]
128+
|===
129+
| result
130+
131+
| 1
132+
133+
1+d|Rows: 1
134+
|===
135+
136+
137+
.Exponentiation operator (`^`)
138+
[source, cypher]
139+
----
140+
RETURN 10 ^ 5 AS result
141+
----
142+
143+
.Result
144+
[role="queryresult",options="header,footer",cols="1*<m"]
145+
|===
146+
| result
147+
148+
| 100000.0
149+
150+
1+d|Rows: 1
151+
|===
152+
153+
154+
.Modifying properties using mathematical operators
155+
[source, cypher]
156+
----
157+
CREATE (p:Product {price: 10})
158+
SET p.discountPrice = p.price * (1 - 0.15)
159+
RETURN p.discountPrice AS discountPrice
160+
----
161+
162+
.Result
163+
[role="queryresult",options="header,footer",cols="1*<m"]
164+
|===
165+
| discountPrice
166+
167+
| 8.5
168+
169+
1+d|Rows: 1
170+
|===
171+
172+
173+
[[order-of-precedence]]
174+
== Order of precedence
175+
176+
The following table details the order of precedence of the mathematical operators in ascending order.
177+
Note, the lower the precedence level, the higher the binding power of the operands.
178+
Also note, if parentheses `()` are used, any operation within them takes precedence, overriding the default order of precedence.
179+
180+
.Mathematical operators: order of precedence
181+
[options="header"cols="a,2a,2a"]
182+
|===
183+
| Precedence | Operators | Associativity
184+
185+
| 1 | Unary negation (`-`), unary positive (`+`) | Right to left
186+
| 2 | Exponentiation (`^`) | Right to left
187+
| 3 | Multiplication (`*`), division (`/`), modulo division (`%`) | Left to right
188+
| 4 | Addition (`+`), subtraction (`-`) | Left to right
189+
190+
|===
191+
192+
Operators within the same precedence group are evaluated based on associativity.
193+
194+
The order of precedence ensures that the following two expressions return the same result.
195+
196+
.Expression with several different mathematical operations
197+
[source, cypher]
198+
----
199+
RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % 12 AS result1,
200+
(((-50) + (6 * 3)) - ((100 / (5 ^ 2)) % 12)) AS result2
201+
----
202+
203+
.Result
204+
[role="queryresult",options="header,footer",cols="2*<m"]
205+
|===
206+
| result1 | result2
207+
208+
| -36 | -36
209+
210+
1+d|Rows: 1
211+
|===
212+
213+
.Order of evaluation
214+
[options="header", cols="a,2a,a"]
215+
|===
216+
| Precedence | Operation | Result
217+
218+
| 1 | Unary negation (`-50`) | `-50`
219+
| 2 | Exponentiation (`5 ^ 2`) | `25`
220+
| 3 | Multiplication (`6 * 3`) | `18`
221+
| 3 | Division (`100 / 25`) | `4`
222+
| 3 | Modulo division (`4 % 12`) | `4`
223+
| 4 | Addition (`-50 + 18`) | `-32`
224+
| 4 | Subtraction (`-32 - 4`) | `-36`
225+
226+
|===
227+
228+
Only bracketing some of the operations within parentheses will change the order of precedence and may, therefore, change the result of an expression.
229+
230+
.Parenthesizing single operation
231+
[source, cypher]
232+
----
233+
RETURN (-50 + 6) * 3 - 100 / 5 ^ 2 % 12 AS result
234+
----
235+
236+
.Result
237+
[role="queryresult",options="header,footer",cols="1*<m"]
238+
|===
239+
| result
240+
241+
| -136
242+
243+
1+d|Rows: 1
244+
|===
245+
246+
.Changed order of evaluation
247+
[options="header",cols="a,2a,a"]
248+
|===
249+
| Precedence | Operation | Result
250+
251+
| 1 | Parenthesized operation (`-50 + 6`) | `-44`
252+
| 2 | Exponentiation (`5 ^ 2`) | `25`
253+
| 3 | Multiplication (`-44 * 3`) | `132`
254+
| 3 | Division (`100 / 25`) | `4`
255+
| 3 | Modulo division (`4 % 12`) | `4`
256+
| 4 | Subtraction (`-132 - 4`) | `-136`
257+
|===

modules/ROOT/pages/functions/mathematical-logarithmic.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[[query-functions-logarithmic]]
55
= Mathematical functions - logarithmic
66

7-
Logarithmic mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators].
7+
Logarithmic mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref:expressions/mathematical-operators.adoc[Mathematical operators].
88

99

1010
[[functions-e]]

modules/ROOT/pages/functions/mathematical-numeric.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
= Mathematical functions - numeric
66

77
Numeric mathematical functions operate on numeric expressions only, and will return an error if used on any other values.
8-
See also xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators].
8+
See also xref:expressions/mathematical-operators.adoc[Mathematical operators].
99

1010
[[example-graph]]
1111
== Example graph

modules/ROOT/pages/functions/mathematical-trigonometric.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[[query-functions-trigonometric]]
55
= Mathematical functions - trigonometric
66

7-
Trigonometric mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators].
7+
Trigonometric mathematical functions operate on numeric expressions only, and will return an error if used on any other values. See also xref:expressions/mathematical-operators.adoc[Mathematical operators].
88

99
[[functions-acos]]
1010
== acos()

modules/ROOT/pages/syntax/operators.adoc

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ This page contains an overview of the available Cypher operators.
1212
|===
1313
| xref::syntax/operators.adoc#query-operators-aggregation[Aggregation operators] | `DISTINCT`
1414
| xref::syntax/operators.adoc#query-operators-property[Property operators] | `.` for static property access, `[]` for dynamic property access, `=` for replacing all properties, `+=` for mutating specific properties
15-
| xref::syntax/operators.adoc#query-operators-mathematical[Mathematical operators] | `+`, `-`, `*`, `/`, `%`, `^`
1615
| xref::syntax/operators.adoc#query-operators-string[String operators] | `+` and `\|\|` (string concatenation), `IS NORMALIZED`
1716
| xref::syntax/operators.adoc#query-operators-temporal[Temporal operators] | `+` and `-` for operations between durations and temporal instants/durations, `*` and `/` for operations between durations and numbers
1817
| xref::syntax/operators.adoc#query-operators-map[Map operators] | `.` for static value access by key, `[]` for dynamic value access by key
@@ -189,59 +188,6 @@ The properties on the node are updated as follows by those provided in the map:
189188

190189
See xref::clauses/set.adoc#set-setting-properties-using-map[Mutate specific properties using a map and `+=`] for more details on using the property mutation operator `+=`.
191190

192-
193-
[[query-operators-mathematical]]
194-
== Mathematical operators
195-
196-
The mathematical operators comprise:
197-
198-
* addition: `+`
199-
* subtraction or unary minus: `-`
200-
* multiplication: `*`
201-
* division: `/`
202-
* modulo division: `%`
203-
* exponentiation: `^`
204-
205-
206-
[[syntax-using-the-exponentiation-operator]]
207-
=== Using the exponentiation operator `^`
208-
209-
.Query
210-
[source, cypher]
211-
----
212-
WITH 2 AS number, 3 AS exponent
213-
RETURN number ^ exponent AS result
214-
----
215-
216-
.Result
217-
[role="queryresult",options="header,footer",cols="1*<m"]
218-
|===
219-
| +result+
220-
| +8.0+
221-
1+d|Rows: 1
222-
|===
223-
224-
225-
[[syntax-using-the-unary-minus-operator]]
226-
=== Using the unary minus operator `-`
227-
228-
.Query
229-
[source, cypher]
230-
----
231-
WITH -3 AS a, 4 AS b
232-
RETURN b - a AS result
233-
----
234-
235-
.Result
236-
[role="queryresult",options="header,footer",cols="1*<m"]
237-
|===
238-
| +result+
239-
| +7+
240-
241-
1+d|Rows: 1
242-
|===
243-
244-
245191
[[query-operators-string]]
246192
== String operators
247193

0 commit comments

Comments
 (0)