Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Clarify associativity of operators. #9170

Merged
merged 8 commits into from
Apr 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions tutorials/scripting/gdscript/gdscript_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ in case you want to take a look under the hood.
Operators
~~~~~~~~~

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

+---------------------------------------+-----------------------------------------------------------------------------+
| **Operator** | **Description** |
Expand All @@ -251,10 +253,6 @@ The following is the list of supported operators and their precedence.
| | |
| | Multiplies ``x`` by itself ``y`` times, similar to calling |
| | :ref:`pow() <class_@GlobalScope_method_pow>` function. |
| | |
| | **Note:** In GDScript, the ``**`` operator is |
| | `left-associative <https://en.wikipedia.org/wiki/Operator_associativity>`_. |
| | See a detailed note after the table. |
+---------------------------------------+-----------------------------------------------------------------------------+
| ``~x`` | Bitwise NOT |
+---------------------------------------+-----------------------------------------------------------------------------+
Expand Down Expand Up @@ -330,9 +328,7 @@ The following is the list of supported operators and their precedence.
3. For negative values, the ``%`` operator and ``fmod()`` use `truncation <https://en.wikipedia.org/wiki/Truncation>`_ instead of rounding towards negative infinity.
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
:ref:`fposmod() <class_@GlobalScope_method_fposmod>` functions instead.
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``.
Use parentheses to explicitly specify precedence you need, for example ``2 ** (2 ** 3)``.
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
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
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
(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>`
and :ref:`is_zero_approx() <class_@GlobalScope_method_is_zero_approx>` functions instead.
Expand Down