Skip to content

Commit 00e9c55

Browse files
csabellaStefan Krah
authored andcommitted
bpo-26256: Document algorithm speed for the Decimal module. (#4808)
1 parent e5ef45b commit 00e9c55

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Doc/library/decimal.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,3 +2115,23 @@ Alternatively, inputs can be rounded upon creation using the
21152115

21162116
>>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678')
21172117
Decimal('1.2345')
2118+
2119+
Q. Is the CPython implementation fast for large numbers?
2120+
2121+
A. Yes. In the CPython and PyPy3 implementations, the C/CFFI versions of
2122+
the decimal module integrate the high speed `libmpdec
2123+
<https://www.bytereef.org/mpdecimal/doc/libmpdec/index.html>`_ library for
2124+
arbitrary precision correctly-rounded decimal floating point arithmetic.
2125+
``libmpdec`` uses `Karatsuba multiplication
2126+
<https://en.wikipedia.org/wiki/Karatsuba_algorithm>`_
2127+
for medium-sized numbers and the `Number Theoretic Transform
2128+
<https://en.wikipedia.org/wiki/Discrete_Fourier_transform_(general)#Number-theoretic_transform>`_
2129+
for very large numbers. However, to realize this performance gain, the
2130+
context needs to be set for unrounded calculations.
2131+
2132+
>>> c = getcontext()
2133+
>>> c.prec = MAX_PREC
2134+
>>> c.Emax = MAX_EMAX
2135+
>>> c.Emin = MIN_EMIN
2136+
2137+
.. versionadded:: 3.3

0 commit comments

Comments
 (0)