Skip to content

Commit 3ae9866

Browse files
bpo-26680: Updates documentation for Real.is_integer and built-ins int and float.
The call x.is_integer() is now listed in the table of operations which apply to all numeric types except complex, with a reference to the full documentation for Real.is_integer(). Mention of is_integer() has been removed from the section 'Additional Methods on Float'. The documentation for Real.is_integer() describes its purpose, and mentions that it should be overridden for performance reasons, or to handle special values like NaN.
1 parent cc07ee5 commit 3ae9866

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

Doc/library/numbers.rst

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,30 @@ The numeric tower
4949
numbers.
5050

5151
In short, those are: a conversion to :class:`float`, :func:`math.trunc`,
52-
:func:`round`, :func:`math.floor`, :func:`math.ceil`, :func:`divmod`, ``//``,
53-
``%``, ``<``, ``<=``, ``>``, and ``>=``.
52+
:func:`round`, :func:`math.floor`, :func:`math.ceil`, :func:`divmod`,
53+
:func:`~Real.is_integer`, ``//``, ``%``, ``<``, ``<=``, ``>``, and ``>=``.
5454

5555
Real also provides defaults for :func:`complex`, :attr:`~Complex.real`,
5656
:attr:`~Complex.imag`, and :meth:`~Complex.conjugate`.
5757

58+
.. method:: is_integer()
59+
60+
Returns :const:`True` if this number has a finite and integral value,
61+
otherwise :const:`False`. This is a default implementation which
62+
relies on successful conversion to :class:`int`. It may be overridden
63+
in subclasses (such as it is in :class:`float`) for better performance,
64+
or to handle special values such as NaN which are not
65+
convertible to :class:`int`.
66+
67+
.. versionadded:: 3.10
68+
5869

5970
.. class:: Rational
6071

6172
Subtypes :class:`Real` and adds
6273
:attr:`~Rational.numerator` and :attr:`~Rational.denominator` properties, which
63-
should be in lowest terms. With these, it provides a default for
64-
:func:`float`.
74+
should be in lowest terms. With these, it provides defaults for
75+
:func:`float` and :func:`~Real.is_integer`.
6576

6677
.. attribute:: numerator
6778

@@ -75,9 +86,10 @@ The numeric tower
7586
.. class:: Integral
7687

7788
Subtypes :class:`Rational` and adds a conversion to :class:`int`. Provides
78-
defaults for :func:`float`, :attr:`~Rational.numerator`, and
79-
:attr:`~Rational.denominator`. Adds abstract methods for ``**`` and
80-
bit-string operations: ``<<``, ``>>``, ``&``, ``^``, ``|``, ``~``.
89+
defaults for :func:`float`, :attr:`~Rational.numerator`,
90+
:attr:`~Rational.denominator`, and :func:`~Real.is_integer`. Adds abstract
91+
methods for ``**`` and bit-string operations: ``<<``, ``>>``, ``&``, ``^``,
92+
``|``, ``~``.
8193

8294

8395
Notes for type implementors

Doc/library/stdtypes.rst

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ the operations, see :ref:`operator-summary`):
310310
+---------------------+---------------------------------+---------+--------------------+
311311
| ``x ** y`` | *x* to the power *y* | \(5) | |
312312
+---------------------+---------------------------------+---------+--------------------+
313+
| ``x.is_integer()`` | ``True`` if *x* has a finite | | :func:`~numbers\ |
314+
| | and integral value, otherwise | | .Real.is_integer` |
315+
| | ``False``. | | |
316+
+---------------------+---------------------------------+---------+--------------------+
313317

314318
.. index::
315319
triple: operations on; numeric; types
@@ -583,16 +587,6 @@ class`. float also has the following additional methods.
583587
:exc:`OverflowError` on infinities and a :exc:`ValueError` on
584588
NaNs.
585589

586-
.. method:: float.is_integer()
587-
588-
Return ``True`` if the float instance is finite with integral
589-
value, and ``False`` otherwise::
590-
591-
>>> (-2.0).is_integer()
592-
True
593-
>>> (3.2).is_integer()
594-
False
595-
596590
Two methods support conversion to
597591
and from hexadecimal strings. Since Python's floats are stored
598592
internally as binary numbers, converting a float to or from a

0 commit comments

Comments
 (0)