Skip to content

Update {_py,}decimal.py from 3.13.5 #6034

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 1 commit into from
Jul 25, 2025
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
261 changes: 73 additions & 188 deletions Lib/_pydecimal.py

Large diffs are not rendered by default.

108 changes: 103 additions & 5 deletions Lib/decimal.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,109 @@
"""Decimal fixed-point and floating-point arithmetic.
This is an implementation of decimal floating-point arithmetic based on
the General Decimal Arithmetic Specification:
http://speleotrove.com/decimal/decarith.html
and IEEE standard 854-1987:
http://en.wikipedia.org/wiki/IEEE_854-1987
Decimal floating point has finite precision with arbitrarily large bounds.
The purpose of this module is to support arithmetic using familiar
"schoolhouse" rules and to avoid some of the tricky representation
issues associated with binary floating point. The package is especially
useful for financial applications or for contexts where users have
expectations that are at odds with binary floating point (for instance,
in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected
Decimal('0.00')).
Here are some examples of using the decimal module:
>>> from decimal import *
>>> setcontext(ExtendedContext)
>>> Decimal(0)
Decimal('0')
>>> Decimal('1')
Decimal('1')
>>> Decimal('-.0123')
Decimal('-0.0123')
>>> Decimal(123456)
Decimal('123456')
>>> Decimal('123.45e12345678')
Decimal('1.2345E+12345680')
>>> Decimal('1.33') + Decimal('1.27')
Decimal('2.60')
>>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
Decimal('-2.20')
>>> dig = Decimal(1)
>>> print(dig / Decimal(3))
0.333333333
>>> getcontext().prec = 18
>>> print(dig / Decimal(3))
0.333333333333333333
>>> print(dig.sqrt())
1
>>> print(Decimal(3).sqrt())
1.73205080756887729
>>> print(Decimal(3) ** 123)
4.85192780976896427E+58
>>> inf = Decimal(1) / Decimal(0)
>>> print(inf)
Infinity
>>> neginf = Decimal(-1) / Decimal(0)
>>> print(neginf)
-Infinity
>>> print(neginf + inf)
NaN
>>> print(neginf * inf)
-Infinity
>>> print(dig / 0)
Infinity
>>> getcontext().traps[DivisionByZero] = 1
>>> print(dig / 0)
Traceback (most recent call last):
...
...
...
decimal.DivisionByZero: x / 0
>>> c = Context()
>>> c.traps[InvalidOperation] = 0
>>> print(c.flags[InvalidOperation])
0
>>> c.divide(Decimal(0), Decimal(0))
Decimal('NaN')
>>> c.traps[InvalidOperation] = 1
>>> print(c.flags[InvalidOperation])
1
>>> c.flags[InvalidOperation] = 0
>>> print(c.flags[InvalidOperation])
0
>>> print(c.divide(Decimal(0), Decimal(0)))
Traceback (most recent call last):
...
...
...
decimal.InvalidOperation: 0 / 0
>>> print(c.flags[InvalidOperation])
1
>>> c.flags[InvalidOperation] = 0
>>> c.traps[InvalidOperation] = 0
>>> print(c.divide(Decimal(0), Decimal(0)))
NaN
>>> print(c.flags[InvalidOperation])
1
>>>
"""

try:
from _decimal import *
from _decimal import __doc__
from _decimal import __version__
from _decimal import __libmpdec_version__
except ImportError:
from _pydecimal import *
from _pydecimal import __doc__
from _pydecimal import __version__
from _pydecimal import __libmpdec_version__
import _pydecimal
import sys
_pydecimal.__doc__ = __doc__
sys.modules[__name__] = _pydecimal
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/abs.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
version: 2.59

-- This set of tests primarily tests the existence of the operator.
-- Additon, subtraction, rounding, and more overflows are tested
-- Addition, subtraction, rounding, and more overflows are tested
-- elsewhere.

precision: 9
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/ddFMA.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ ddfma375087 fma 1 12345678 1E-33 -> 12345678.00000001 Inexac
ddfma375088 fma 1 12345678 1E-34 -> 12345678.00000001 Inexact Rounded
ddfma375089 fma 1 12345678 1E-35 -> 12345678.00000001 Inexact Rounded

-- desctructive subtraction (from remainder tests)
-- destructive subtraction (from remainder tests)

-- +++ some of these will be off-by-one remainder vs remainderNear

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/ddQuantize.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ ddqua520 quantize 1.234 1e359 -> 0E+359 Inexact Rounded
ddqua521 quantize 123.456 1e359 -> 0E+359 Inexact Rounded
ddqua522 quantize 1.234 1e359 -> 0E+359 Inexact Rounded
ddqua523 quantize 123.456 1e359 -> 0E+359 Inexact Rounded
-- next four are "won't fit" overfl
-- next four are "won't fit" overflow
ddqua526 quantize 1.234 1e-299 -> NaN Invalid_operation
ddqua527 quantize 123.456 1e-299 -> NaN Invalid_operation
ddqua528 quantize 1.234 1e-299 -> NaN Invalid_operation
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/ddRemainder.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ ddrem757 remainder 1 sNaN -> NaN Invalid_operation
ddrem758 remainder 1000 sNaN -> NaN Invalid_operation
ddrem759 remainder Inf -sNaN -> -NaN Invalid_operation

-- propaging NaNs
-- propagating NaNs
ddrem760 remainder NaN1 NaN7 -> NaN1
ddrem761 remainder sNaN2 NaN8 -> NaN2 Invalid_operation
ddrem762 remainder NaN3 sNaN9 -> NaN9 Invalid_operation
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/ddRemainderNear.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ ddrmn757 remaindernear 1 sNaN -> NaN Invalid_operation
ddrmn758 remaindernear 1000 sNaN -> NaN Invalid_operation
ddrmn759 remaindernear Inf -sNaN -> -NaN Invalid_operation

-- propaging NaNs
-- propagating NaNs
ddrmn760 remaindernear NaN1 NaN7 -> NaN1
ddrmn761 remaindernear sNaN2 NaN8 -> NaN2 Invalid_operation
ddrmn762 remaindernear NaN3 sNaN9 -> NaN9 Invalid_operation
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/dqRemainder.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ dqrem757 remainder 1 sNaN -> NaN Invalid_operation
dqrem758 remainder 1000 sNaN -> NaN Invalid_operation
dqrem759 remainder Inf -sNaN -> -NaN Invalid_operation

-- propaging NaNs
-- propagating NaNs
dqrem760 remainder NaN1 NaN7 -> NaN1
dqrem761 remainder sNaN2 NaN8 -> NaN2 Invalid_operation
dqrem762 remainder NaN3 sNaN9 -> NaN9 Invalid_operation
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/dqRemainderNear.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ dqrmn757 remaindernear 1 sNaN -> NaN Invalid_operation
dqrmn758 remaindernear 1000 sNaN -> NaN Invalid_operation
dqrmn759 remaindernear Inf -sNaN -> -NaN Invalid_operation

-- propaging NaNs
-- propagating NaNs
dqrmn760 remaindernear NaN1 NaN7 -> NaN1
dqrmn761 remaindernear sNaN2 NaN8 -> NaN2 Invalid_operation
dqrmn762 remaindernear NaN3 sNaN9 -> NaN9 Invalid_operation
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/exp.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rounding: half_even
maxExponent: 384
minexponent: -383

-- basics (examples in specificiation, etc.)
-- basics (examples in specification, etc.)
expx001 exp -Infinity -> 0
expx002 exp -10 -> 0.0000453999298 Inexact Rounded
expx003 exp -1 -> 0.367879441 Inexact Rounded
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/extra.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ extr1302 fma -Inf 0E-456 sNaN148 -> NaN Invalid_operation

-- max/min/max_mag/min_mag bug in 2.5.2/2.6/3.0: max(NaN, finite) gave
-- incorrect answers when the finite number required rounding; similarly
-- for the other thre functions
-- for the other three functions
maxexponent: 999
minexponent: -999
precision: 6
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/remainder.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ remx757 remainder 1 sNaN -> NaN Invalid_operation
remx758 remainder 1000 sNaN -> NaN Invalid_operation
remx759 remainder Inf -sNaN -> -NaN Invalid_operation

-- propaging NaNs
-- propagating NaNs
remx760 remainder NaN1 NaN7 -> NaN1
remx761 remainder sNaN2 NaN8 -> NaN2 Invalid_operation
remx762 remainder NaN3 sNaN9 -> NaN9 Invalid_operation
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/decimaltestdata/remainderNear.decTest
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ rmnx758 remaindernear 1000 sNaN -> NaN Invalid_operation
rmnx759 remaindernear Inf sNaN -> NaN Invalid_operation
rmnx760 remaindernear NaN sNaN -> NaN Invalid_operation

-- propaging NaNs
-- propagating NaNs
rmnx761 remaindernear NaN1 NaN7 -> NaN1
rmnx762 remaindernear sNaN2 NaN8 -> NaN2 Invalid_operation
rmnx763 remaindernear NaN3 -sNaN9 -> -NaN9 Invalid_operation
Expand Down
Loading
Loading