Skip to content

Commit 2925c83

Browse files
authored
docs: Clarify associativity of operators. (godotengine#9170)
* docs: Clarify associativity of operators.
1 parent 208bd8f commit 2925c83

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ in case you want to take a look under the hood.
224224
Operators
225225
~~~~~~~~~
226226

227-
The following is the list of supported operators and their precedence.
227+
The following is the list of supported operators and their precedence. All binary operators are `left-associative <https://en.wikipedia.org/wiki/Operator_associativity>`_,
228+
including the ``**`` operator. This means that ``2 ** 2 ** 3`` is equal to ``(2 ** 2) ** 3``. Use parentheses to explicitly specify precedence you need, for
229+
example ``2 ** (2 ** 3)``. The ternary ``if/else`` operator is right-associative.
228230

229231
+---------------------------------------+-----------------------------------------------------------------------------+
230232
| **Operator** | **Description** |
@@ -251,10 +253,6 @@ The following is the list of supported operators and their precedence.
251253
| | |
252254
| | Multiplies ``x`` by itself ``y`` times, similar to calling |
253255
| | :ref:`pow() <class_@GlobalScope_method_pow>` function. |
254-
| | |
255-
| | **Note:** In GDScript, the ``**`` operator is |
256-
| | `left-associative <https://en.wikipedia.org/wiki/Operator_associativity>`_. |
257-
| | See a detailed note after the table. |
258256
+---------------------------------------+-----------------------------------------------------------------------------+
259257
| ``~x`` | Bitwise NOT |
260258
+---------------------------------------+-----------------------------------------------------------------------------+
@@ -330,9 +328,7 @@ The following is the list of supported operators and their precedence.
330328
3. For negative values, the ``%`` operator and ``fmod()`` use `truncation <https://en.wikipedia.org/wiki/Truncation>`_ instead of rounding towards negative infinity.
331329
This means that the remainder has a sign. If you need the remainder in a mathematical sense, use the :ref:`posmod() <class_@GlobalScope_method_posmod>` and
332330
:ref:`fposmod() <class_@GlobalScope_method_fposmod>` functions instead.
333-
4. The ``**`` operator is `left-associative <https://en.wikipedia.org/wiki/Operator_associativity>`_. This means that ``2 ** 2 ** 3`` is equal to ``(2 ** 2) ** 3``.
334-
Use parentheses to explicitly specify precedence you need, for example ``2 ** (2 ** 3)``.
335-
5. The ``==`` and ``!=`` operators sometimes allow you to compare values of different types (for example, ``1 == 1.0`` is true), but in other cases it can cause
331+
4. The ``==`` and ``!=`` operators sometimes allow you to compare values of different types (for example, ``1 == 1.0`` is true), but in other cases it can cause
336332
a runtime error. If you're not sure about the types of the operands, you can safely use the :ref:`is_same() <class_@GlobalScope_method_is_same>` function
337333
(but note that it is more strict about types and references). To compare floats, use the :ref:`is_equal_approx() <class_@GlobalScope_method_is_equal_approx>`
338334
and :ref:`is_zero_approx() <class_@GlobalScope_method_is_zero_approx>` functions instead.

0 commit comments

Comments
 (0)