Skip to content

Commit a8210b6

Browse files
authored
GH-105774: Clarify operation of normalize() (GH-106093)
1 parent 0345b0c commit a8210b6

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

Doc/library/decimal.rst

+37-6
Original file line numberDiff line numberDiff line change
@@ -743,12 +743,23 @@ Decimal objects
743743

744744
.. method:: normalize(context=None)
745745

746-
Normalize the number by stripping the rightmost trailing zeros and
747-
converting any result equal to ``Decimal('0')`` to
748-
``Decimal('0e0')``. Used for producing canonical values for attributes
749-
of an equivalence class. For example, ``Decimal('32.100')`` and
750-
``Decimal('0.321000e+2')`` both normalize to the equivalent value
751-
``Decimal('32.1')``.
746+
Used for producing canonical values of an equivalence
747+
class within either the current context or the specified context.
748+
749+
This has the same semantics as the unary plus operation, except that if
750+
the final result is finite it is reduced to its simplest form, with all
751+
trailing zeros removed and its sign preserved. That is, while the
752+
coefficient is non-zero and a multiple of ten the coefficient is divided
753+
by ten and the exponent is incremented by 1. Otherwise (the coefficient is
754+
zero) the exponent is set to 0. In all cases the sign is unchanged.
755+
756+
For example, ``Decimal('32.100')`` and ``Decimal('0.321000e+2')`` both
757+
normalize to the equivalent value ``Decimal('32.1')``.
758+
759+
Note that rounding is applied *before* reducing to simplest form.
760+
761+
In the latest versions of the specification, this operation is also known
762+
as ``reduce``.
752763

753764
.. method:: number_class(context=None)
754765

@@ -2078,6 +2089,26 @@ representative:
20782089
>>> [v.normalize() for v in values]
20792090
[Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]
20802091

2092+
Q. When does rounding occur in a computation?
2093+
2094+
A. It occurs *after* the computation. The philosophy of the decimal
2095+
specification is that numbers are considered exact and are created
2096+
independent of the current context. They can even have greater
2097+
precision than current context. Computations process with those
2098+
exact inputs and then rounding (or other context operations) is
2099+
applied to the *result* of the computation::
2100+
2101+
>>> getcontext().prec = 5
2102+
>>> pi = Decimal('3.1415926535') # More than 5 digits
2103+
>>> pi # All digits are retained
2104+
Decimal('3.1415926535')
2105+
>>> pi + 0 # Rounded after an addition
2106+
Decimal('3.1416')
2107+
>>> pi - Decimal('0.00005') # Subtract unrounded numbers, then round
2108+
Decimal('3.1415')
2109+
>>> pi + 0 - Decimal('0.00005'). # Intermediate values are rounded
2110+
Decimal('3.1416')
2111+
20812112
Q. Some decimal values always print with exponential notation. Is there a way
20822113
to get a non-exponential representation?
20832114

0 commit comments

Comments
 (0)