Skip to content

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

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

Merged
merged 10 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Doc/library/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,13 @@ Decimal objects
Return :const:`True` if the argument is either positive or negative
infinity and :const:`False` otherwise.

.. method:: is_integer()

Return :const:`True` if the argument is a finite integral value and
:const:`False` otherwise.

Copy link
Member

@mdickinson mdickinson May 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a .. versionadded:: 3.10 note here and in the other relevant bits of documentation.

EDIT: edited the comment to update the version; the original suggestion of 3.8 is obviously out of date

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

.. versionadded:: 3.10

.. method:: is_nan()

Return :const:`True` if the argument is a (quiet or signaling) NaN and
Expand Down Expand Up @@ -1215,6 +1222,13 @@ In addition to the three supplied contexts, new contexts can be created with the
Returns ``True`` if *x* is infinite; otherwise returns ``False``.


.. method:: is_integer(x)

Returns ``True`` if *x* is finite and integral; otherwise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One day it would be nice to fix all these docstrings for consistency (both with one another and with PEP 257). But not today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, when you hop around the code you notice how different they all are. I tried to go for local consistency rather than global consistency.

returns ``False``.

.. versionadded:: 3.10

.. method:: is_nan(x)

Returns ``True`` if *x* is a qNaN or sNaN; otherwise returns ``False``.
Expand Down
26 changes: 19 additions & 7 deletions Doc/library/numbers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
14 changes: 4 additions & 10 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,12 @@ def is_zero(self):
"""Return True if self is a zero; otherwise return False."""
return not self._is_special and self._int == '0'

def is_integer(self):
"""Return True is self is finite and integral; otherwise False."""
if self._is_special:
return False
return self.to_integral_value(rounding=ROUND_FLOOR) == self

def _ln_exp_bound(self):
"""Compute a lower bound for the adjusted exponent of self.ln().
In other words, compute r such that self.ln() >= 10**r. Assumes
Expand Down Expand Up @@ -4659,6 +4665,25 @@ def is_zero(self, a):
a = _convert_other(a, raiseit=True)
return a.is_zero()

def is_integer(self, a):
"""Return True if the operand is integral; otherwise return False.

>>> ExtendedContext.is_integer(Decimal('0'))
True
>>> ExtendedContext.is_integer(Decimal('2.50'))
False
>>> ExtendedContext.is_integer(Decimal('-0E+2'))
True
>>> ExtendedContext.is_integer(Decimal('-0.5'))
False
>>> ExtendedContext.is_integer(Decimal('NaN'))
False
>>> ExtendedContext.is_integer(10)
True
"""
a = _convert_other(a, raiseit=True)
return a.is_integer()

def ln(self, a):
"""Returns the natural (base e) logarithm of the operand.

Expand Down
21 changes: 20 additions & 1 deletion Lib/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Real(Complex):
"""To Complex, Real adds the operations that work on real numbers.

In short, those are: a conversion to float, trunc(), divmod,
%, <, <=, >, and >=.
is_integer, %, <, <=, >, and >=.

Real also provides defaults for the derived operations.
"""
Expand Down Expand Up @@ -242,6 +242,17 @@ def __le__(self, other):
"""self <= other"""
raise NotImplementedError

def is_integer(self):
"""Return True if the Real is integral; otherwise return False.

This default implementation can be overridden in subclasses
for performance reasons or to deal with values such as NaN,
which would otherwise cause an exception to be raised.
"""
# Although __int__ is not defined at this level, the int
# constructor falls back to __trunc__, which we do have.
return self == int(self)

# Concrete implementations of Complex abstract methods.
def __complex__(self):
"""complex(self) == complex(float(self), 0)"""
Expand Down Expand Up @@ -290,6 +301,10 @@ def __float__(self):
"""
return self.numerator / self.denominator

def is_integer(self):
"""Return True if the Rational is integral; otherwise return False."""
return self.denominator == 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must denominator and numerator always be fully reduced?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not,

Suggested change
return self.denominator == 1
return self.numerator % self.denominator == 0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, self.numerator and self.denominator will always be relatively prime in normal use (and denominator will always be positive). Other parts of the fractions module assume this, so it's safe to just check self.denominator == 1 here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't about Fraction and its implementation of the Rational API though - this is about whether the API mandates the implementation behaves this way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I see the doc is """.numerator and .denominator should be in lowest terms.""" - so perhaps worth clarifying that denominator should always be positive too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that docstring could definitely be improved. Apart from that clarification, we could do with a line or two describing what the Rational class is actually for, rather than launching straight into a detail. But that's a job for a separate PR.



class Integral(Rational):
"""Integral adds a conversion to int and the bit-string operations."""
Expand Down Expand Up @@ -386,4 +401,8 @@ def denominator(self):
"""Integers have a denominator of 1."""
return 1

def is_integer(self):
"""Return True; all Integrals represent an integral value."""
return True

Integral.register(int)
18 changes: 18 additions & 0 deletions Lib/test/decimaltestdata/extra.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,24 @@ bool2096 iszero sNaN -> 0
bool2097 iszero -sNaN -> 0
bool2098 iszero sNaN123 -> 0
bool2099 iszero -sNaN123 -> 0
bool2100 is_integer -1.0 -> 1
bool2101 is_integer 0.0 -> 1
bool2102 is_integer 1.0 -> 1
bool2103 is_integer 42 -> 1
bool2104 is_integer 1e2 -> 1
bool2105 is_integer 1.5 -> 0
bool2106 is_integer 1e-2 -> 0
bool2107 is_integer NaN -> 0
bool2109 is_integer -NaN -> 0
bool2110 is_integer NaN123 -> 0
bool2111 is_integer -NaN123 -> 0
bool2112 is_integer sNaN -> 0
bool2113 is_integer -sNaN -> 0
bool2114 is_integer sNaN123 -> 0
bool2115 is_integer -sNaN123 -> 0
bool2116 is_integer Infinity -> 0
bool2117 is_integer -Infinity -> 0


------------------------------------------------------------------------
-- The following tests (pwmx0 through pwmx440) are for the --
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ def test_real_and_imag(self):
self.assertIs(type(False.real), int)
self.assertIs(type(False.imag), int)

def test_always_is_integer(self):
# Issue #26680: Incorporating number.is_integer into bool
self.assertTrue(all(b.is_integer() for b in (False, True)))


def test_main():
support.run_unittest(BoolTest)

Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def setUp(self):
'is_snan',
'is_subnormal',
'is_zero',
'is_integer',
'same_quantum')

def read_unlimited(self, v, context):
Expand Down Expand Up @@ -2726,6 +2727,7 @@ def test_named_parameters(self):
self.assertRaises(TypeError, D(1).is_snan, context=xc)
self.assertRaises(TypeError, D(1).is_signed, context=xc)
self.assertRaises(TypeError, D(1).is_zero, context=xc)
self.assertRaises(TypeError, D(1).is_integer, context=xc)

self.assertFalse(D("0.01").is_normal(context=xc))
self.assertTrue(D("0.01").is_subnormal(context=xc))
Expand Down Expand Up @@ -3197,6 +3199,15 @@ def test_is_zero(self):
self.assertEqual(c.is_zero(10), d)
self.assertRaises(TypeError, c.is_zero, '10')

def test_is_integer(self):
Decimal = self.decimal.Decimal
Context = self.decimal.Context

c = Context()
b = c.is_integer(Decimal(10))
self.assertEqual(c.is_integer(10), b)
self.assertRaises(TypeError, c.is_integer, '10')

def test_ln(self):
Decimal = self.decimal.Decimal
Context = self.decimal.Context
Expand Down Expand Up @@ -4360,6 +4371,19 @@ def test_implicit_context(self):
self.assertTrue(Decimal("-1").is_signed())
self.assertTrue(Decimal("0").is_zero())
self.assertTrue(Decimal("0").is_zero())
self.assertTrue(Decimal("-1").is_integer())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth adding a couple of tests for cases where the exponent isn't 0, e.g. Decimal("1e2") and Decimal("100e-2")? We should also test the behaviour for signalling NaNs

Ideally, we'd also test that no Decimal floating-point flags are ever raised. An easy way to do this would be to add some testcases to Lib/test/decimaltestdata/extra.decTest, which will check that exactly the flags mentioned are raised for each given testcase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

self.assertTrue(Decimal("0").is_integer())
self.assertTrue(Decimal("1").is_integer())
self.assertTrue(Decimal("42").is_integer())
self.assertTrue(Decimal("1e2").is_integer())
self.assertFalse(Decimal("1.5").is_integer())
self.assertFalse(Decimal("1e-2").is_integer())
self.assertFalse(Decimal("NaN").is_integer())
self.assertFalse(Decimal("-NaN").is_integer())
self.assertFalse(Decimal("sNaN").is_integer())
self.assertFalse(Decimal("-sNaN").is_integer())
self.assertFalse(Decimal("Inf").is_integer())
self.assertFalse(Decimal("-Inf").is_integer())

# Copy
with localcontext() as c:
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,17 @@ def denominator(self):
self.assertEqual(type(f.numerator), myint)
self.assertEqual(type(f.denominator), myint)

def test_is_integer(self):
# Issue #26680: Incorporating number.is_integer into Fraction
self.assertTrue(F(-1, 1).is_integer())
self.assertTrue(F(0, 1).is_integer())
self.assertTrue(F(1, 1).is_integer())
self.assertTrue(F(42, 1).is_integer())
self.assertTrue(F(2, 2).is_integer())
self.assertTrue(F(8, 4).is_integer())
self.assertFalse(F(1, 2).is_integer())
self.assertFalse(F(1, 3).is_integer())
self.assertFalse(F(2, 3).is_integer())

if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,10 @@ class myint(int):
self.assertEqual(type(numerator), int)
self.assertEqual(type(denominator), int)

def test_int_always_is_integer(self):
# Issue #26680: Incorporating number.is_integer into int
self.assertTrue(all(x.is_integer() for x in (-1, 0, 1, 42)))


if __name__ == "__main__":
unittest.main()
31 changes: 31 additions & 0 deletions Lib/test/test_numeric_tower.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
import operator

from numbers import Real, Rational, Integral
from decimal import Decimal as D
from fractions import Fraction as F

Expand Down Expand Up @@ -198,5 +199,35 @@ def test_complex(self):
self.assertRaises(TypeError, op, v, z)


class IsIntegerTest(unittest.TestCase):

def test_real_is_integer(self):
self.assertTrue(Real.is_integer(-1.0))
self.assertTrue(Real.is_integer(0.0))
self.assertTrue(Real.is_integer(1.0))
self.assertTrue(Real.is_integer(42.0))

self.assertFalse(Real.is_integer(-0.5))
self.assertFalse(Real.is_integer(4.2))

def test_rational_is_integer(self):
self.assertTrue(Rational.is_integer(F(-1, 1)))
self.assertTrue(Rational.is_integer(F(0, 1)))
self.assertTrue(Rational.is_integer(F(1, 1)))
self.assertTrue(Rational.is_integer(F(42, 1)))
self.assertTrue(Rational.is_integer(F(2, 2)))
self.assertTrue(Rational.is_integer(F(8, 4)))

self.assertFalse(Rational.is_integer(F(1, 2)))
self.assertFalse(Rational.is_integer(F(1, 3)))
self.assertFalse(Rational.is_integer(F(2, 3)))

def test_integral_is_integer(self):
self.assertTrue(Integral.is_integer(-1))
self.assertTrue(Integral.is_integer(0))
self.assertTrue(Integral.is_integer(1))
self.assertTrue(Integral.is_integer(1729))


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,7 @@ Roman Skurikhin
Ville Skyttä
Michael Sloan
Nick Sloan
Robert Smallshire
Václav Šmilauer
Allen W. Smith
Christopher Smith
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The int type now supports the x.is_integer() method for compatibility with
float.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The x.is_integer() method is incorporated into the abstract types of the
numeric tower, Real, Rational and Integral, with appropriate default
implementations.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The d.is_integer() method is added to the Decimal type, for compatibility
with other number types.
Loading