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

bpo-26680: Incorporate is_integer in all built-in and standard library numeric types #6121

Merged
merged 10 commits into from
Oct 1, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bpo-26680: Updates documentation for Real.is_integer and built-ins in…
…t 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.
rob-smallshire committed Sep 18, 2020
commit 3ae9866a51d7d40d6a69308ad211aada8711949b
26 changes: 19 additions & 7 deletions Doc/library/numbers.rst
Original file line number Diff line number Diff line change
@@ -49,19 +49,30 @@ The numeric tower
numbers.

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

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

.. method:: is_integer()

Returns :const:`True` if this number has a finite and integral value,
otherwise :const:`False`. This is a default implementation which
relies on successful conversion to :class:`int`. It may be overridden
in subclasses (such as it is in :class:`float`) for better performance,
or to handle special values such as NaN which are not
convertible to :class:`int`.

.. versionadded:: 3.10


.. class:: Rational

Subtypes :class:`Real` and adds
:attr:`~Rational.numerator` and :attr:`~Rational.denominator` properties, which
should be in lowest terms. With these, it provides a default for
:func:`float`.
should be in lowest terms. With these, it provides defaults for
:func:`float` and :func:`~Real.is_integer`.

.. attribute:: numerator

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

Subtypes :class:`Rational` and adds a conversion to :class:`int`. Provides
defaults for :func:`float`, :attr:`~Rational.numerator`, and
:attr:`~Rational.denominator`. Adds abstract methods for ``**`` and
bit-string operations: ``<<``, ``>>``, ``&``, ``^``, ``|``, ``~``.
defaults for :func:`float`, :attr:`~Rational.numerator`,
:attr:`~Rational.denominator`, and :func:`~Real.is_integer`. Adds abstract
methods for ``**`` and bit-string operations: ``<<``, ``>>``, ``&``, ``^``,
``|``, ``~``.


Notes for type implementors
14 changes: 4 additions & 10 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
@@ -310,6 +310,10 @@ the operations, see :ref:`operator-summary`):
+---------------------+---------------------------------+---------+--------------------+
| ``x ** y`` | *x* to the power *y* | \(5) | |
+---------------------+---------------------------------+---------+--------------------+
| ``x.is_integer()`` | ``True`` if *x* has a finite | | :func:`~numbers\ |
| | and integral value, otherwise | | .Real.is_integer` |
| | ``False``. | | |
+---------------------+---------------------------------+---------+--------------------+

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

.. method:: float.is_integer()

Return ``True`` if the float instance is finite with integral
value, and ``False`` otherwise::

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False

Two methods support conversion to
and from hexadecimal strings. Since Python's floats are stored
internally as binary numbers, converting a float to or from a