Skip to content

Commit

Permalink
docs: Clarify associativity of operators. (#9170)
Browse files Browse the repository at this point in the history
* docs: Clarify associativity of operators.
  • Loading branch information
elenakrittik committed Apr 6, 2024
1 parent 208bd8f commit 2925c83
Showing 1 changed file with 4 additions and 8 deletions.
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

0 comments on commit 2925c83

Please sign in to comment.