Skip to content

Commit 58a7da9

Browse files
bpo-26680: Incorporate is_integer in all built-in and standard library numeric types (GH-6121)
* bpo-26680: Adds support for int.is_integer() for compatibility with float.is_integer(). The int.is_integer() method always returns True. * bpo-26680: Adds a test to ensure that False.is_integer() and True.is_integer() are always True. * bpo-26680: Adds Real.is_integer() with a trivial implementation using conversion to int. This default implementation is intended to reduce the workload for subclass implementers. It is not robust in the presence of infinities or NaNs and may have suboptimal performance for other types. * bpo-26680: Adds Rational.is_integer which returns True if the denominator is one. This implementation assumes the Rational is represented in it's lowest form, as required by the class docstring. * bpo-26680: Adds Integral.is_integer which always returns True. * bpo-26680: Adds tests for Fraction.is_integer called as an instance method. The tests for the Rational abstract base class use an unbound method to sidestep the inability to directly instantiate Rational. These tests check that everything works correct as an instance method. * 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. * bpo-26680: Adds Decimal.is_integer to the Python and C implementations. The C implementation of Decimal already implements and uses mpd_isinteger internally, we just expose the existing function to Python. The Python implementation uses internal conversion to integer using to_integral_value(). In both cases, the corresponding context methods are also implemented. Tests and documentation are included. * bpo-26680: Updates the ACKS file. * bpo-26680: NEWS entries for int, the numeric ABCs and Decimal. Co-authored-by: Robert Smallshire <[email protected]>
1 parent 256e54a commit 58a7da9

19 files changed

+230
-24
lines changed

Doc/library/decimal.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,13 @@ Decimal objects
621621
Return :const:`True` if the argument is either positive or negative
622622
infinity and :const:`False` otherwise.
623623

624+
.. method:: is_integer()
625+
626+
Return :const:`True` if the argument is a finite integral value and
627+
:const:`False` otherwise.
628+
629+
.. versionadded:: 3.10
630+
624631
.. method:: is_nan()
625632

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

12171224

1225+
.. method:: is_integer(x)
1226+
1227+
Returns ``True`` if *x* is finite and integral; otherwise
1228+
returns ``False``.
1229+
1230+
.. versionadded:: 3.10
1231+
12181232
.. method:: is_nan(x)
12191233

12201234
Returns ``True`` if *x* is a qNaN or sNaN; otherwise returns ``False``.

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

Lib/_pydecimal.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,12 @@ def is_zero(self):
31643164
"""Return True if self is a zero; otherwise return False."""
31653165
return not self._is_special and self._int == '0'
31663166

3167+
def is_integer(self):
3168+
"""Return True is self is finite and integral; otherwise False."""
3169+
if self._is_special:
3170+
return False
3171+
return self.to_integral_value(rounding=ROUND_FLOOR) == self
3172+
31673173
def _ln_exp_bound(self):
31683174
"""Compute a lower bound for the adjusted exponent of self.ln().
31693175
In other words, compute r such that self.ln() >= 10**r. Assumes
@@ -4659,6 +4665,25 @@ def is_zero(self, a):
46594665
a = _convert_other(a, raiseit=True)
46604666
return a.is_zero()
46614667

4668+
def is_integer(self, a):
4669+
"""Return True if the operand is integral; otherwise return False.
4670+
4671+
>>> ExtendedContext.is_integer(Decimal('0'))
4672+
True
4673+
>>> ExtendedContext.is_integer(Decimal('2.50'))
4674+
False
4675+
>>> ExtendedContext.is_integer(Decimal('-0E+2'))
4676+
True
4677+
>>> ExtendedContext.is_integer(Decimal('-0.5'))
4678+
False
4679+
>>> ExtendedContext.is_integer(Decimal('NaN'))
4680+
False
4681+
>>> ExtendedContext.is_integer(10)
4682+
True
4683+
"""
4684+
a = _convert_other(a, raiseit=True)
4685+
return a.is_integer()
4686+
46624687
def ln(self, a):
46634688
"""Returns the natural (base e) logarithm of the operand.
46644689

Lib/numbers.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Real(Complex):
148148
"""To Complex, Real adds the operations that work on real numbers.
149149
150150
In short, those are: a conversion to float, trunc(), divmod,
151-
%, <, <=, >, and >=.
151+
is_integer, %, <, <=, >, and >=.
152152
153153
Real also provides defaults for the derived operations.
154154
"""
@@ -242,6 +242,17 @@ def __le__(self, other):
242242
"""self <= other"""
243243
raise NotImplementedError
244244

245+
def is_integer(self):
246+
"""Return True if the Real is integral; otherwise return False.
247+
248+
This default implementation can be overridden in subclasses
249+
for performance reasons or to deal with values such as NaN,
250+
which would otherwise cause an exception to be raised.
251+
"""
252+
# Although __int__ is not defined at this level, the int
253+
# constructor falls back to __trunc__, which we do have.
254+
return self == int(self)
255+
245256
# Concrete implementations of Complex abstract methods.
246257
def __complex__(self):
247258
"""complex(self) == complex(float(self), 0)"""
@@ -290,6 +301,10 @@ def __float__(self):
290301
"""
291302
return self.numerator / self.denominator
292303

304+
def is_integer(self):
305+
"""Return True if the Rational is integral; otherwise return False."""
306+
return self.denominator == 1
307+
293308

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

404+
def is_integer(self):
405+
"""Return True; all Integrals represent an integral value."""
406+
return True
407+
389408
Integral.register(int)

Lib/test/decimaltestdata/extra.decTest

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,24 @@ bool2096 iszero sNaN -> 0
23462346
bool2097 iszero -sNaN -> 0
23472347
bool2098 iszero sNaN123 -> 0
23482348
bool2099 iszero -sNaN123 -> 0
2349+
bool2100 is_integer -1.0 -> 1
2350+
bool2101 is_integer 0.0 -> 1
2351+
bool2102 is_integer 1.0 -> 1
2352+
bool2103 is_integer 42 -> 1
2353+
bool2104 is_integer 1e2 -> 1
2354+
bool2105 is_integer 1.5 -> 0
2355+
bool2106 is_integer 1e-2 -> 0
2356+
bool2107 is_integer NaN -> 0
2357+
bool2109 is_integer -NaN -> 0
2358+
bool2110 is_integer NaN123 -> 0
2359+
bool2111 is_integer -NaN123 -> 0
2360+
bool2112 is_integer sNaN -> 0
2361+
bool2113 is_integer -sNaN -> 0
2362+
bool2114 is_integer sNaN123 -> 0
2363+
bool2115 is_integer -sNaN123 -> 0
2364+
bool2116 is_integer Infinity -> 0
2365+
bool2117 is_integer -Infinity -> 0
2366+
23492367

23502368
------------------------------------------------------------------------
23512369
-- The following tests (pwmx0 through pwmx440) are for the --

Lib/test/test_bool.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ def test_real_and_imag(self):
354354
self.assertIs(type(False.real), int)
355355
self.assertIs(type(False.imag), int)
356356

357+
def test_always_is_integer(self):
358+
# Issue #26680: Incorporating number.is_integer into bool
359+
self.assertTrue(all(b.is_integer() for b in (False, True)))
360+
361+
357362
def test_main():
358363
support.run_unittest(BoolTest)
359364

Lib/test/test_decimal.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def setUp(self):
276276
'is_snan',
277277
'is_subnormal',
278278
'is_zero',
279+
'is_integer',
279280
'same_quantum')
280281

281282
def read_unlimited(self, v, context):
@@ -2726,6 +2727,7 @@ def test_named_parameters(self):
27262727
self.assertRaises(TypeError, D(1).is_snan, context=xc)
27272728
self.assertRaises(TypeError, D(1).is_signed, context=xc)
27282729
self.assertRaises(TypeError, D(1).is_zero, context=xc)
2730+
self.assertRaises(TypeError, D(1).is_integer, context=xc)
27292731

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

3202+
def test_is_integer(self):
3203+
Decimal = self.decimal.Decimal
3204+
Context = self.decimal.Context
3205+
3206+
c = Context()
3207+
b = c.is_integer(Decimal(10))
3208+
self.assertEqual(c.is_integer(10), b)
3209+
self.assertRaises(TypeError, c.is_integer, '10')
3210+
32003211
def test_ln(self):
32013212
Decimal = self.decimal.Decimal
32023213
Context = self.decimal.Context
@@ -4360,6 +4371,19 @@ def test_implicit_context(self):
43604371
self.assertTrue(Decimal("-1").is_signed())
43614372
self.assertTrue(Decimal("0").is_zero())
43624373
self.assertTrue(Decimal("0").is_zero())
4374+
self.assertTrue(Decimal("-1").is_integer())
4375+
self.assertTrue(Decimal("0").is_integer())
4376+
self.assertTrue(Decimal("1").is_integer())
4377+
self.assertTrue(Decimal("42").is_integer())
4378+
self.assertTrue(Decimal("1e2").is_integer())
4379+
self.assertFalse(Decimal("1.5").is_integer())
4380+
self.assertFalse(Decimal("1e-2").is_integer())
4381+
self.assertFalse(Decimal("NaN").is_integer())
4382+
self.assertFalse(Decimal("-NaN").is_integer())
4383+
self.assertFalse(Decimal("sNaN").is_integer())
4384+
self.assertFalse(Decimal("-sNaN").is_integer())
4385+
self.assertFalse(Decimal("Inf").is_integer())
4386+
self.assertFalse(Decimal("-Inf").is_integer())
43634387

43644388
# Copy
43654389
with localcontext() as c:

Lib/test/test_fractions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,17 @@ def denominator(self):
724724
self.assertEqual(type(f.numerator), myint)
725725
self.assertEqual(type(f.denominator), myint)
726726

727+
def test_is_integer(self):
728+
# Issue #26680: Incorporating number.is_integer into Fraction
729+
self.assertTrue(F(-1, 1).is_integer())
730+
self.assertTrue(F(0, 1).is_integer())
731+
self.assertTrue(F(1, 1).is_integer())
732+
self.assertTrue(F(42, 1).is_integer())
733+
self.assertTrue(F(2, 2).is_integer())
734+
self.assertTrue(F(8, 4).is_integer())
735+
self.assertFalse(F(1, 2).is_integer())
736+
self.assertFalse(F(1, 3).is_integer())
737+
self.assertFalse(F(2, 3).is_integer())
727738

728739
if __name__ == '__main__':
729740
unittest.main()

Lib/test/test_long.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,10 @@ class myint(int):
13811381
self.assertEqual(type(numerator), int)
13821382
self.assertEqual(type(denominator), int)
13831383

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

13851389
if __name__ == "__main__":
13861390
unittest.main()

Lib/test/test_numeric_tower.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import operator
88

9+
from numbers import Real, Rational, Integral
910
from decimal import Decimal as D
1011
from fractions import Fraction as F
1112

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

200201

202+
class IsIntegerTest(unittest.TestCase):
203+
204+
def test_real_is_integer(self):
205+
self.assertTrue(Real.is_integer(-1.0))
206+
self.assertTrue(Real.is_integer(0.0))
207+
self.assertTrue(Real.is_integer(1.0))
208+
self.assertTrue(Real.is_integer(42.0))
209+
210+
self.assertFalse(Real.is_integer(-0.5))
211+
self.assertFalse(Real.is_integer(4.2))
212+
213+
def test_rational_is_integer(self):
214+
self.assertTrue(Rational.is_integer(F(-1, 1)))
215+
self.assertTrue(Rational.is_integer(F(0, 1)))
216+
self.assertTrue(Rational.is_integer(F(1, 1)))
217+
self.assertTrue(Rational.is_integer(F(42, 1)))
218+
self.assertTrue(Rational.is_integer(F(2, 2)))
219+
self.assertTrue(Rational.is_integer(F(8, 4)))
220+
221+
self.assertFalse(Rational.is_integer(F(1, 2)))
222+
self.assertFalse(Rational.is_integer(F(1, 3)))
223+
self.assertFalse(Rational.is_integer(F(2, 3)))
224+
225+
def test_integral_is_integer(self):
226+
self.assertTrue(Integral.is_integer(-1))
227+
self.assertTrue(Integral.is_integer(0))
228+
self.assertTrue(Integral.is_integer(1))
229+
self.assertTrue(Integral.is_integer(1729))
230+
231+
201232
if __name__ == '__main__':
202233
unittest.main()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,7 @@ Roman Skurikhin
16111611
Ville Skyttä
16121612
Michael Sloan
16131613
Nick Sloan
1614+
Robert Smallshire
16141615
Václav Šmilauer
16151616
Allen W. Smith
16161617
Christopher Smith
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The int type now supports the x.is_integer() method for compatibility with
2+
float.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The x.is_integer() method is incorporated into the abstract types of the
2+
numeric tower, Real, Rational and Integral, with appropriate default
3+
implementations.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The d.is_integer() method is added to the Decimal type, for compatibility
2+
with other number types.

0 commit comments

Comments
 (0)