@@ -2067,20 +2067,20 @@ to work with the :class:`Decimal` class::
20672067Decimal FAQ
20682068-----------
20692069
2070- Q. It is cumbersome to type ``decimal.Decimal('1234.5') ``. Is there a way to
2070+ Q: It is cumbersome to type ``decimal.Decimal('1234.5') ``. Is there a way to
20712071minimize typing when using the interactive interpreter?
20722072
2073- A. Some users abbreviate the constructor to just a single letter:
2073+ A: Some users abbreviate the constructor to just a single letter:
20742074
20752075 >>> D = decimal.Decimal
20762076 >>> D(' 1.23' ) + D(' 3.45' )
20772077 Decimal('4.68')
20782078
2079- Q. In a fixed-point application with two decimal places, some inputs have many
2079+ Q: In a fixed-point application with two decimal places, some inputs have many
20802080places and need to be rounded. Others are not supposed to have excess digits
20812081and need to be validated. What methods should be used?
20822082
2083- A. The :meth: `~Decimal.quantize ` method rounds to a fixed number of decimal places. If
2083+ A: The :meth: `~Decimal.quantize ` method rounds to a fixed number of decimal places. If
20842084the :const: `Inexact ` trap is set, it is also useful for validation:
20852085
20862086 >>> TWOPLACES = Decimal(10 ) ** - 2 # same as Decimal('0.01')
@@ -2098,10 +2098,10 @@ the :const:`Inexact` trap is set, it is also useful for validation:
20982098 ...
20992099 Inexact: None
21002100
2101- Q. Once I have valid two place inputs, how do I maintain that invariant
2101+ Q: Once I have valid two place inputs, how do I maintain that invariant
21022102throughout an application?
21032103
2104- A. Some operations like addition, subtraction, and multiplication by an integer
2104+ A: Some operations like addition, subtraction, and multiplication by an integer
21052105will automatically preserve fixed point. Others operations, like division and
21062106non-integer multiplication, will change the number of decimal places and need to
21072107be followed-up with a :meth: `~Decimal.quantize ` step:
@@ -2133,21 +2133,21 @@ to handle the :meth:`~Decimal.quantize` step:
21332133 >>> div(b, a)
21342134 Decimal('0.03')
21352135
2136- Q. There are many ways to express the same value. The numbers ``200 ``,
2136+ Q: There are many ways to express the same value. The numbers ``200 ``,
21372137``200.000 ``, ``2E2 ``, and ``.02E+4 `` all have the same value at
21382138various precisions. Is there a way to transform them to a single recognizable
21392139canonical value?
21402140
2141- A. The :meth: `~Decimal.normalize ` method maps all equivalent values to a single
2141+ A: The :meth: `~Decimal.normalize ` method maps all equivalent values to a single
21422142representative:
21432143
21442144 >>> values = map (Decimal, ' 200 200.000 2E2 .02E+4' .split())
21452145 >>> [v.normalize() for v in values]
21462146 [Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]
21472147
2148- Q. When does rounding occur in a computation?
2148+ Q: When does rounding occur in a computation?
21492149
2150- A. It occurs *after * the computation. The philosophy of the decimal
2150+ A: It occurs *after * the computation. The philosophy of the decimal
21512151specification is that numbers are considered exact and are created
21522152independent of the current context. They can even have greater
21532153precision than current context. Computations process with those
@@ -2165,10 +2165,10 @@ applied to the *result* of the computation::
21652165 >>> pi + 0 - Decimal('0.00005'). # Intermediate values are rounded
21662166 Decimal('3.1416')
21672167
2168- Q. Some decimal values always print with exponential notation. Is there a way
2168+ Q: Some decimal values always print with exponential notation. Is there a way
21692169to get a non-exponential representation?
21702170
2171- A. For some values, exponential notation is the only way to express the number
2171+ A: For some values, exponential notation is the only way to express the number
21722172of significant places in the coefficient. For example, expressing
21732173``5.0E+3 `` as ``5000 `` keeps the value constant but cannot show the
21742174original's two-place significance.
@@ -2183,9 +2183,9 @@ value unchanged:
21832183 >>> remove_exponent(Decimal(' 5E+3' ))
21842184 Decimal('5000')
21852185
2186- Q. Is there a way to convert a regular float to a :class: `Decimal `?
2186+ Q: Is there a way to convert a regular float to a :class: `Decimal `?
21872187
2188- A. Yes, any binary floating-point number can be exactly expressed as a
2188+ A: Yes, any binary floating-point number can be exactly expressed as a
21892189Decimal though an exact conversion may take more precision than intuition would
21902190suggest:
21912191
@@ -2194,19 +2194,19 @@ suggest:
21942194 >>> Decimal(math.pi)
21952195 Decimal('3.141592653589793115997963468544185161590576171875')
21962196
2197- Q. Within a complex calculation, how can I make sure that I haven't gotten a
2197+ Q: Within a complex calculation, how can I make sure that I haven't gotten a
21982198spurious result because of insufficient precision or rounding anomalies.
21992199
2200- A. The decimal module makes it easy to test results. A best practice is to
2200+ A: The decimal module makes it easy to test results. A best practice is to
22012201re-run calculations using greater precision and with various rounding modes.
22022202Widely differing results indicate insufficient precision, rounding mode issues,
22032203ill-conditioned inputs, or a numerically unstable algorithm.
22042204
2205- Q. I noticed that context precision is applied to the results of operations but
2205+ Q: I noticed that context precision is applied to the results of operations but
22062206not to the inputs. Is there anything to watch out for when mixing values of
22072207different precisions?
22082208
2209- A. Yes. The principle is that all values are considered to be exact and so is
2209+ A: Yes. The principle is that all values are considered to be exact and so is
22102210the arithmetic on those values. Only the results are rounded. The advantage
22112211for inputs is that "what you type is what you get". A disadvantage is that the
22122212results can look odd if you forget that the inputs haven't been rounded:
@@ -2234,9 +2234,9 @@ Alternatively, inputs can be rounded upon creation using the
22342234 >>> Context(prec = 5 , rounding = ROUND_DOWN ).create_decimal(' 1.2345678' )
22352235 Decimal('1.2345')
22362236
2237- Q. Is the CPython implementation fast for large numbers?
2237+ Q: Is the CPython implementation fast for large numbers?
22382238
2239- A. Yes. In the CPython and PyPy3 implementations, the C/CFFI versions of
2239+ A: Yes. In the CPython and PyPy3 implementations, the C/CFFI versions of
22402240the decimal module integrate the high speed `libmpdec
22412241<https://www.bytereef.org/mpdecimal/doc/libmpdec/index.html> `_ library for
22422242arbitrary precision correctly rounded decimal floating-point arithmetic [# ]_.
0 commit comments