Skip to content

Commit 907d982

Browse files
third
1 parent 1db81f3 commit 907d982

File tree

1 file changed

+50
-20
lines changed

1 file changed

+50
-20
lines changed

modules/ROOT/pages/expressions/mathematical-operators.adoc

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -168,57 +168,87 @@ RETURN p.discountPrice AS discountPrice
168168

169169
1+d|Rows: 1
170170
|===
171+
171172

172-
[[precedence]]
173+
[[order-of-precedence]]
173174
== Order of precedence
174175

175-
The following list details the order of precedence of the mathematical operators in ascending order:
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.
176179

177-
.Mathematical operators order of precedence
178-
[options="header"]
180+
.Mathematical operators: order of precedence
181+
[options="header"cols="a,2a,2a"]
179182
|===
180-
181-
| Precedence | Operators | Associativity
182-
183183
| Precedence | Operators | Associativity
184184

185-
| 1 | Unary negation (`-`), Unary positive (`+`) | Right to left
185+
| 1 | Unary negation (`-`), unary positive (`+`) | Right to left
186186
| 2 | Exponentiation (`^`) | Right to left
187-
| 3 | Multiplication (`*`), Division (`/`), Modulo (`%`) | Left to right
188-
| 4 | Addition (`+`), Subtraction (`-`) | Left to right
187+
| 3 | Multiplication (`*`), division (`/`), modulo division (`%`) | Left to right
188+
| 4 | Addition (`+`), subtraction (`-`) | Left to right
189189

190190
|===
191191

192192
Operators within the same precedence group are evaluated based on associativity.
193193

194-
.Combining mathematical operators
194+
.Expression with several different mathematical operations
195195
[source, cypher]
196196
----
197-
RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % +12 AS result
197+
RETURN (((-50) + (6 * 3)) - ((100 / (5 ^ 2)) % 12)) AS result
198198
----
199199

200200
.Result
201201
[role="queryresult",options="header,footer",cols="1*<m"]
202202
|===
203203
| result
204204

205-
| -35
205+
| -36
206206

207207
1+d|Rows: 1
208208
|===
209209

210-
.Order of Evaluation
211-
[options="header"]
210+
.Order of evaluation
211+
[options="header", cols="a,2a,a"]
212212
|===
213213
| Precedence | Operation | Result
214214

215215
| 1 | Unary negation (`-50`) | `-50`
216-
| 1 | Unary positive (`+12`) | `12`
217216
| 2 | Exponentiation (`5 ^ 2`) | `25`
218-
| 3 | Division (`100 / 25`) | `4`
219217
| 3 | Multiplication (`6 * 3`) | `18`
220-
| 3 | Multiplication (`4 * 12`) | `48`
221-
| 3 | Modulo (`48 % 5`) | `3`
218+
| 3 | Division (`100 / 25`) | `4`
219+
| 3 | Modulo division (`4 % 12`) | `4`
222220
| 4 | Addition (`-50 + 18`) | `-32`
223-
| 4 | Subtraction (`-32 - 3`) | `-35`
221+
| 4 | Subtraction (`-32 - 4`) | `-36`
222+
223+
|===
224+
225+
Only bracketing some of the operations within parentheses will change the order of precedence and may, therefore, change the result of an expression.
226+
227+
.Parenthesizing single operation
228+
[source, cypher]
229+
----
230+
RETURN (-50 + 6) * 3 - 100 / 5 ^ 2 % 12 AS result
231+
----
232+
233+
.Result
234+
[role="queryresult",options="header,footer",cols="1*<m"]
235+
|===
236+
| result
237+
238+
| -136
239+
240+
1+d|Rows: 1
241+
|===
242+
243+
.Changed order of evaluation
244+
[options="header",cols="a,2a,a"]
245+
|===
246+
| Precedence | Operation | Result
247+
248+
| 1 | Parenthesized operation (`-50 + 6`) | `-44`
249+
| 2 | Exponentiation (`5 ^ 2`) | `25`
250+
| 3 | Multiplication (`-44 * 3`) | `132`
251+
| 3 | Division (`100 / 25`) | `4`
252+
| 3 | Modulo division (`4 % 12`) | `4`
253+
| 4 | Subtraction (`-132 - 4`) | `-136`
224254
|===

0 commit comments

Comments
 (0)