Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-102431: clarify constraints on arguments of logical_xxx methods#102836

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
serhiy-storchaka merged 14 commits intopython:mainfromskirpichev:decimal-logop-docs
Oct 14, 2025
Merged
Show file tree
Hide file tree
Changes from10 commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
f1496f8
gh-102431: clarify constraints on arguments of logical_xxx methods
skirpichevMar 8, 2023
319091e
Merge branch 'main' into decimal-logop-docs
skirpichevMar 21, 2023
b704f1c
Sync C/python implementation of the decimal: logical_ops for contexts
skirpichevMar 21, 2023
460e8af
Add news
skirpichevMar 21, 2023
4eb4e4a
Merge branch 'main' into decimal-logop-docs
skirpichevApr 3, 2023
4748f02
Merge branch 'main' into decimal-logop-docs
skirpichevApr 29, 2023
d9100d5
Merge branch 'main' into decimal-logop-docs
skirpichevJun 11, 2023
883cd86
Merge branch 'main' into decimal-logop-docs
skirpichevJul 26, 2023
6a3e1d3
Merge branch 'main' into decimal-logop-docs
skirpichevAug 2, 2023
44ea0c9
Merge branch 'main' into decimal-logop-docs
skirpichevJan 19, 2025
419c3a9
Merge branch 'master' into decimal-logop-docs
skirpichevSep 9, 2025
006d58d
+1
skirpichevOct 10, 2025
961b08c
Merge branch 'master' into decimal-logop-docs
skirpichevOct 14, 2025
50eb822
address review: use "logical numbers" term
skirpichevOct 14, 2025
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
35 changes: 27 additions & 8 deletionsLib/_pydecimal.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3317,7 +3317,11 @@ def _fill_logical(self, context, opa, opb):
return opa, opb

def logical_and(self, other, context=None):
"""Applies an 'and' operation between self and other's digits."""
"""Applies an 'and' operation between self and other's digits.

Both self and other should have zero sign and exponent,
and digits either 0 or 1.
"""
if context is None:
context = getcontext()

Expand All@@ -3334,14 +3338,21 @@ def logical_and(self, other, context=None):
return _dec_from_triple(0, result.lstrip('0') or '0', 0)

def logical_invert(self, context=None):
"""Invert all its digits."""
"""Invert all its digits.

The self should have zero sign and exponent, and digits either 0 or 1.
"""
if context is None:
context = getcontext()
return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0),
context)

def logical_or(self, other, context=None):
"""Applies an 'or' operation between self and other's digits."""
"""Applies an 'or' operation between self and other's digits.

Both self and other should have zero sign and exponent,
and digits either 0 or 1.
"""
if context is None:
context = getcontext()

Expand All@@ -3358,7 +3369,11 @@ def logical_or(self, other, context=None):
return _dec_from_triple(0, result.lstrip('0') or '0', 0)

def logical_xor(self, other, context=None):
"""Applies an 'xor' operation between self and other's digits."""
"""Applies an 'xor' operation between self and other's digits.

Both self and other should have zero sign and exponent,
and digits either 0 or 1.
"""
if context is None:
context = getcontext()

Expand DownExpand Up@@ -4681,7 +4696,8 @@ def logb(self, a):
def logical_and(self, a, b):
"""Applies the logical operation 'and' between each operand's digits.

The operands must be both logical numbers.
Both operands should have zero sign and exponent, and
digits either 0 or 1.

>>> ExtendedContext.logical_and(Decimal('0'), Decimal('0'))
Decimal('0')
Expand All@@ -4708,7 +4724,8 @@ def logical_and(self, a, b):
def logical_invert(self, a):
"""Invert all the digits in the operand.

The operand must be a logical number.
The operand should have zero sign and exponent, and digits
either 0 or 1.

>>> ExtendedContext.logical_invert(Decimal('0'))
Decimal('111111111')
Expand All@@ -4727,7 +4744,8 @@ def logical_invert(self, a):
def logical_or(self, a, b):
"""Applies the logical operation 'or' between each operand's digits.

The operands must be both logical numbers.
Both operands should have zero sign and exponent, and digits
either 0 or 1.

>>> ExtendedContext.logical_or(Decimal('0'), Decimal('0'))
Decimal('0')
Expand All@@ -4754,7 +4772,8 @@ def logical_or(self, a, b):
def logical_xor(self, a, b):
"""Applies the logical operation 'xor' between each operand's digits.

The operands must be both logical numbers.
Both operands should have zero sign and exponent, and digits
either 0 or 1.

>>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0'))
Decimal('0')
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Clarify constraints for "logical" arguments in methods of
:class:`decimal.Context`.
96 changes: 84 additions & 12 deletionsModules/_decimal/docstrings.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -295,22 +295,26 @@ an infinity then Decimal('Infinity') is returned.\n\

PyDoc_STRVAR(doc_logical_and,
"logical_and($self, /, other, context=None)\n--\n\n\
Return the digit-wise 'and' of the two (logical) operands.\n\
Return the digit-wise 'and' of the two (logical) operands,\n\
which both have zero sign and exponent, and digits either 0 or 1.\n\
\n");

PyDoc_STRVAR(doc_logical_invert,
"logical_invert($self, /, context=None)\n--\n\n\
Return the digit-wise inversion of the (logical) operand.\n\
Return the digit-wise inversion of the (logical) operand,\n\
which has zero sign and exponent, and digits either 0 or 1.\n\
\n");

PyDoc_STRVAR(doc_logical_or,
"logical_or($self, /, other, context=None)\n--\n\n\
Return the digit-wise 'or' of the two (logical) operands.\n\
Return the digit-wise 'or' of the two (logical) operands,\n\
which both have zero sign and exponent, and digits either 0 or 1.\n\
\n");

PyDoc_STRVAR(doc_logical_xor,
"logical_xor($self, /, other, context=None)\n--\n\n\
Return the digit-wise 'exclusive or' of the two (logical) operands.\n\
Return the digit-wise 'exclusive or' of the two (logical) operands,\n\
which both have zero sign and exponent, and digits either 0 or 1.\n\
\n");

PyDoc_STRVAR(doc_max,
Expand DownExpand Up@@ -715,23 +719,91 @@ Return the exponent of the magnitude of the operand's MSD.\n\

PyDoc_STRVAR(doc_ctx_logical_and,
"logical_and($self, x, y, /)\n--\n\n\
Digit-wise and of x and y.\n\
\n");
Applies the logical operation \'and\' between each operand\'s digits.\n\n\
Both operands should have zero sign and exponent, and\n\
digits either 0 or 1.\n\n\
>>> ExtendedContext.logical_and(Decimal(\'0\'), Decimal(\'0\'))\n\
Decimal(\'0\')\n\
>>> ExtendedContext.logical_and(Decimal(\'0\'), Decimal(\'1\'))\n\
Decimal(\'0\')\n\
>>> ExtendedContext.logical_and(Decimal(\'1\'), Decimal(\'0\'))\n\
Decimal(\'0\')\n\
>>> ExtendedContext.logical_and(Decimal(\'1\'), Decimal(\'1\'))\n\
Decimal(\'1\')\n\
>>> ExtendedContext.logical_and(Decimal(\'1100\'), Decimal(\'1010\'))\n\
Decimal(\'1000\')\n\
>>> ExtendedContext.logical_and(Decimal(\'1111\'), Decimal(\'10\'))\n\
Decimal(\'10\')\n\
>>> ExtendedContext.logical_and(110, 1101)\n\
Decimal(\'100\')\n\
>>> ExtendedContext.logical_and(Decimal(110), 1101)\n\
Decimal(\'100\')\n\
>>> ExtendedContext.logical_and(110, Decimal(1101))\n\
Decimal(\'100\')\n");

PyDoc_STRVAR(doc_ctx_logical_invert,
"logical_invert($self, x, /)\n--\n\n\
Invert all digits of x.\n\
\n");
Invert all the digits in the operand.\n\n\
The operand should have zero sign and exponent, and digits\n\
either 0 or 1.\n\n\
>>> ExtendedContext.logical_invert(Decimal(\'0\'))\n\
Decimal(\'111111111\')\n\
>>> ExtendedContext.logical_invert(Decimal(\'1\'))\n\
Decimal(\'111111110\')\n\
>>> ExtendedContext.logical_invert(Decimal(\'111111111\'))\n\
Decimal(\'0\')\n\
>>> ExtendedContext.logical_invert(Decimal(\'101010101\'))\n\
Decimal(\'10101010\')\n\
>>> ExtendedContext.logical_invert(1101)\n\
Decimal(\'111110010\')\n");

PyDoc_STRVAR(doc_ctx_logical_or,
"logical_or($self, x, y, /)\n--\n\n\
Digit-wise or of x and y.\n\
\n");
Applies the logical operation \'or\' between each operand\'s digits.\n\n\
Both operands should have zero sign and exponent, and digits\n\
either 0 or 1.\n\n\
>>> ExtendedContext.logical_or(Decimal(\'0\'), Decimal(\'0\'))\n\
Decimal(\'0\')\n\
>>> ExtendedContext.logical_or(Decimal(\'0\'), Decimal(\'1\'))\n\
Decimal(\'1\')\n\
>>> ExtendedContext.logical_or(Decimal(\'1\'), Decimal(\'0\'))\n\
Decimal(\'1\')\n\
>>> ExtendedContext.logical_or(Decimal(\'1\'), Decimal(\'1\'))\n\
Decimal(\'1\')\n\
>>> ExtendedContext.logical_or(Decimal(\'1100\'), Decimal(\'1010\'))\n\
Decimal(\'1110\')\n\
>>> ExtendedContext.logical_or(Decimal(\'1110\'), Decimal(\'10\'))\n\
Decimal(\'1110\')\n\
>>> ExtendedContext.logical_or(110, 1101)\n\
Decimal(\'1111\')\n\
>>> ExtendedContext.logical_or(Decimal(110), 1101)\n\
Decimal(\'1111\')\n\
>>> ExtendedContext.logical_or(110, Decimal(1101))\n\
Decimal(\'1111\')\n");

PyDoc_STRVAR(doc_ctx_logical_xor,
"logical_xor($self, x, y, /)\n--\n\n\
Digit-wise xor of x and y.\n\
\n");
Applies the logical operation \'xor\' between each operand\'s digits.\n\n\
Both operands should have zero sign and exponent, and digits\n\
either 0 or 1.\n\n\
>>> ExtendedContext.logical_xor(Decimal(\'0\'), Decimal(\'0\'))\n\
Decimal(\'0\')\n\
>>> ExtendedContext.logical_xor(Decimal(\'0\'), Decimal(\'1\'))\n\
Decimal(\'1\')\n\
>>> ExtendedContext.logical_xor(Decimal(\'1\'), Decimal(\'0\'))\n\
Decimal(\'1\')\n\
>>> ExtendedContext.logical_xor(Decimal(\'1\'), Decimal(\'1\'))\n\
Decimal(\'0\')\n\
>>> ExtendedContext.logical_xor(Decimal(\'1100\'), Decimal(\'1010\'))\n\
Decimal(\'110\')\n\
>>> ExtendedContext.logical_xor(Decimal(\'1111\'), Decimal(\'10\'))\n\
Decimal(\'1101\')\n\
>>> ExtendedContext.logical_xor(110, 1101)\n\
Decimal(\'1011\')\n\
>>> ExtendedContext.logical_xor(Decimal(110), 1101)\n\
Decimal(\'1011\')\n\
>>> ExtendedContext.logical_xor(110, Decimal(1101))\n\
Decimal(\'1011\')\n");

PyDoc_STRVAR(doc_ctx_max,
"max($self, x, y, /)\n--\n\n\
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp