|
| 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 | +[[precedence]] |
| 173 | +== Order of precedence |
| 174 | + |
| 175 | +The following list details the order of precedence of the mathematical operators in ascending order: |
| 176 | + |
| 177 | +.Mathematical operators order of precedence |
| 178 | +[options="header"] |
| 179 | +|=== |
| 180 | + |
| 181 | +| Precedence | Operators | Associativity |
| 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 (`%`) | 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 | +.Combining mathematical operators |
| 195 | +[source, cypher] |
| 196 | +---- |
| 197 | +RETURN -50 + 6 * 3 - 100 / 5 ^ 2 % +12 AS result |
| 198 | +---- |
| 199 | + |
| 200 | +.Result |
| 201 | +[role="queryresult",options="header,footer",cols="1*<m"] |
| 202 | +|=== |
| 203 | +| result |
| 204 | + |
| 205 | +| -35 |
| 206 | + |
| 207 | +1+d|Rows: 1 |
| 208 | +|=== |
| 209 | + |
| 210 | +.Order of Evaluation |
| 211 | +[options="header"] |
| 212 | +|=== |
| 213 | +| Precedence | Operation | Result |
| 214 | + |
| 215 | +| 1 | Unary negation (`-50`) | `-50` |
| 216 | +| 1 | Unary positive (`+12`) | `12` |
| 217 | +| 2 | Exponentiation (`5 ^ 2`) | `25` |
| 218 | +| 3 | Division (`100 / 25`) | `4` |
| 219 | +| 3 | Multiplication (`6 * 3`) | `18` |
| 220 | +| 3 | Multiplication (`4 * 12`) | `48` |
| 221 | +| 3 | Modulo (`48 % 5`) | `3` |
| 222 | +| 4 | Addition (`-50 + 18`) | `-32` |
| 223 | +| 4 | Subtraction (`-32 - 3`) | `-35` |
| 224 | +|=== |
0 commit comments