-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Changes from 1 commit
d80b017
b46a9dc
3b1677f
f78bf5c
6a905a8
cc07ee5
3ae9866
54ca820
a9e364d
11db7f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
.. versionadded:: 3.10 | ||
|
||
.. method:: is_nan() | ||
|
||
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 | |
Returns ``True`` if *x* is infinite; otherwise returns ``False``. | ||
|
||
|
||
.. method:: is_integer(x) | ||
|
||
Returns ``True`` if *x* is finite and integral; otherwise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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``. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,6 +276,7 @@ def setUp(self): | |
'is_snan', | ||
'is_subnormal', | ||
'is_zero', | ||
'is_integer', | ||
'same_quantum') | ||
|
||
def read_unlimited(self, v, context): | ||
|
@@ -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)) | ||
|
@@ -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 | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5241,7 +5241,7 @@ Returns True for all integers. | |
|
||
static PyObject * | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we convert this to Argument Clinic? It seems somewhat trivial, but one advantage is having the docstring close to the definition. Another is having docstring consistency between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
int_is_integer_impl(PyObject *self) | ||
/*[clinic end generated code: output=90f8e794ce5430ef input=903121d57b734c35]*/ | ||
/*[clinic end generated code: output=90f8e794ce5430ef input=1c1a86957301d26d]*/ | ||
{ | ||
Py_RETURN_TRUE; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.