docs: Clarify associativity of operators. (#9170)

* docs: Clarify associativity of operators.

(cherry picked from commit 2925c83390)
This commit is contained in:
lena
2024-04-06 20:28:25 +03:00
committed by Max Hilbrunner
parent 509095d65f
commit b8e65373cd

View File

@@ -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** |
@@ -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 |
+---------------------------------------+-----------------------------------------------------------------------------+
@@ -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.