@@ -2109,20 +2109,20 @@ to work with the :class:`Decimal` class::
21092109Decimal FAQ
21102110-----------
21112111
2112- Q. It is cumbersome to type ``decimal.Decimal('1234.5') ``. Is there a way to
2112+ Q: It is cumbersome to type ``decimal.Decimal('1234.5') ``. Is there a way to
21132113minimize typing when using the interactive interpreter?
21142114
2115- A. Some users abbreviate the constructor to just a single letter:
2115+ A: Some users abbreviate the constructor to just a single letter:
21162116
21172117 >>> D = decimal.Decimal
21182118 >>> D(' 1.23' ) + D(' 3.45' )
21192119 Decimal('4.68')
21202120
2121- Q. In a fixed-point application with two decimal places, some inputs have many
2121+ Q: In a fixed-point application with two decimal places, some inputs have many
21222122places and need to be rounded. Others are not supposed to have excess digits
21232123and need to be validated. What methods should be used?
21242124
2125- A. The :meth: `~Decimal.quantize ` method rounds to a fixed number of decimal places. If
2125+ A: The :meth: `~Decimal.quantize ` method rounds to a fixed number of decimal places. If
21262126the :const: `Inexact ` trap is set, it is also useful for validation:
21272127
21282128 >>> TWOPLACES = Decimal(10 ) ** - 2 # same as Decimal('0.01')
@@ -2140,10 +2140,10 @@ the :const:`Inexact` trap is set, it is also useful for validation:
21402140 ...
21412141 Inexact: None
21422142
2143- Q. Once I have valid two place inputs, how do I maintain that invariant
2143+ Q: Once I have valid two place inputs, how do I maintain that invariant
21442144throughout an application?
21452145
2146- A. Some operations like addition, subtraction, and multiplication by an integer
2146+ A: Some operations like addition, subtraction, and multiplication by an integer
21472147will automatically preserve fixed point. Others operations, like division and
21482148non-integer multiplication, will change the number of decimal places and need to
21492149be followed-up with a :meth: `~Decimal.quantize ` step:
@@ -2175,21 +2175,21 @@ to handle the :meth:`~Decimal.quantize` step:
21752175 >>> div(b, a)
21762176 Decimal('0.03')
21772177
2178- Q. There are many ways to express the same value. The numbers ``200 ``,
2178+ Q: There are many ways to express the same value. The numbers ``200 ``,
21792179``200.000 ``, ``2E2 ``, and ``.02E+4 `` all have the same value at
21802180various precisions. Is there a way to transform them to a single recognizable
21812181canonical value?
21822182
2183- A. The :meth: `~Decimal.normalize ` method maps all equivalent values to a single
2183+ A: The :meth: `~Decimal.normalize ` method maps all equivalent values to a single
21842184representative:
21852185
21862186 >>> values = map (Decimal, ' 200 200.000 2E2 .02E+4' .split())
21872187 >>> [v.normalize() for v in values]
21882188 [Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]
21892189
2190- Q. When does rounding occur in a computation?
2190+ Q: When does rounding occur in a computation?
21912191
2192- A. It occurs *after * the computation. The philosophy of the decimal
2192+ A: It occurs *after * the computation. The philosophy of the decimal
21932193specification is that numbers are considered exact and are created
21942194independent of the current context. They can even have greater
21952195precision than current context. Computations process with those
@@ -2207,10 +2207,10 @@ applied to the *result* of the computation::
22072207 >>> pi + 0 - Decimal('0.00005'). # Intermediate values are rounded
22082208 Decimal('3.1416')
22092209
2210- Q. Some decimal values always print with exponential notation. Is there a way
2210+ Q: Some decimal values always print with exponential notation. Is there a way
22112211to get a non-exponential representation?
22122212
2213- A. For some values, exponential notation is the only way to express the number
2213+ A: For some values, exponential notation is the only way to express the number
22142214of significant places in the coefficient. For example, expressing
22152215``5.0E+3 `` as ``5000 `` keeps the value constant but cannot show the
22162216original's two-place significance.
@@ -2225,9 +2225,9 @@ value unchanged:
22252225 >>> remove_exponent(Decimal(' 5E+3' ))
22262226 Decimal('5000')
22272227
2228- Q. Is there a way to convert a regular float to a :class: `Decimal `?
2228+ Q: Is there a way to convert a regular float to a :class: `Decimal `?
22292229
2230- A. Yes, any binary floating-point number can be exactly expressed as a
2230+ A: Yes, any binary floating-point number can be exactly expressed as a
22312231Decimal though an exact conversion may take more precision than intuition would
22322232suggest:
22332233
@@ -2236,19 +2236,19 @@ suggest:
22362236 >>> Decimal(math.pi)
22372237 Decimal('3.141592653589793115997963468544185161590576171875')
22382238
2239- Q. Within a complex calculation, how can I make sure that I haven't gotten a
2239+ Q: Within a complex calculation, how can I make sure that I haven't gotten a
22402240spurious result because of insufficient precision or rounding anomalies.
22412241
2242- A. The decimal module makes it easy to test results. A best practice is to
2242+ A: The decimal module makes it easy to test results. A best practice is to
22432243re-run calculations using greater precision and with various rounding modes.
22442244Widely differing results indicate insufficient precision, rounding mode issues,
22452245ill-conditioned inputs, or a numerically unstable algorithm.
22462246
2247- Q. I noticed that context precision is applied to the results of operations but
2247+ Q: I noticed that context precision is applied to the results of operations but
22482248not to the inputs. Is there anything to watch out for when mixing values of
22492249different precisions?
22502250
2251- A. Yes. The principle is that all values are considered to be exact and so is
2251+ A: Yes. The principle is that all values are considered to be exact and so is
22522252the arithmetic on those values. Only the results are rounded. The advantage
22532253for inputs is that "what you type is what you get". A disadvantage is that the
22542254results can look odd if you forget that the inputs haven't been rounded:
@@ -2276,9 +2276,9 @@ Alternatively, inputs can be rounded upon creation using the
22762276 >>> Context(prec = 5 , rounding = ROUND_DOWN ).create_decimal(' 1.2345678' )
22772277 Decimal('1.2345')
22782278
2279- Q. Is the CPython implementation fast for large numbers?
2279+ Q: Is the CPython implementation fast for large numbers?
22802280
2281- A. Yes. In the CPython and PyPy3 implementations, the C/CFFI versions of
2281+ A: Yes. In the CPython and PyPy3 implementations, the C/CFFI versions of
22822282the decimal module integrate the high speed `libmpdec
22832283<https://www.bytereef.org/mpdecimal/doc/libmpdec/index.html> `_ library for
22842284arbitrary precision correctly rounded decimal floating-point arithmetic [# ]_.
0 commit comments