@@ -2100,20 +2100,20 @@ to work with the :class:`Decimal` class::
21002100Decimal FAQ
21012101-----------
21022102
2103- Q. It is cumbersome to type ``decimal.Decimal('1234.5') ``. Is there a way to
2103+ Q\ . It is cumbersome to type ``decimal.Decimal('1234.5') ``. Is there a way to
21042104minimize typing when using the interactive interpreter?
21052105
2106- A. Some users abbreviate the constructor to just a single letter:
2106+ A\ . Some users abbreviate the constructor to just a single letter:
21072107
21082108 >>> D = decimal.Decimal
21092109 >>> D(' 1.23' ) + D(' 3.45' )
21102110 Decimal('4.68')
21112111
2112- Q. In a fixed-point application with two decimal places, some inputs have many
2112+ Q\ . In a fixed-point application with two decimal places, some inputs have many
21132113places and need to be rounded. Others are not supposed to have excess digits
21142114and need to be validated. What methods should be used?
21152115
2116- A. The :meth: `~Decimal.quantize ` method rounds to a fixed number of decimal places. If
2116+ A\ . The :meth: `~Decimal.quantize ` method rounds to a fixed number of decimal places. If
21172117the :const: `Inexact ` trap is set, it is also useful for validation:
21182118
21192119 >>> TWOPLACES = Decimal(10 ) ** - 2 # same as Decimal('0.01')
@@ -2131,10 +2131,10 @@ the :const:`Inexact` trap is set, it is also useful for validation:
21312131 ...
21322132 Inexact: None
21332133
2134- Q. Once I have valid two place inputs, how do I maintain that invariant
2134+ Q\ . Once I have valid two place inputs, how do I maintain that invariant
21352135throughout an application?
21362136
2137- A. Some operations like addition, subtraction, and multiplication by an integer
2137+ A\ . Some operations like addition, subtraction, and multiplication by an integer
21382138will automatically preserve fixed point. Others operations, like division and
21392139non-integer multiplication, will change the number of decimal places and need to
21402140be followed-up with a :meth: `~Decimal.quantize ` step:
@@ -2166,21 +2166,21 @@ to handle the :meth:`~Decimal.quantize` step:
21662166 >>> div(b, a)
21672167 Decimal('0.03')
21682168
2169- Q. There are many ways to express the same value. The numbers ``200 ``,
2169+ Q\ . There are many ways to express the same value. The numbers ``200 ``,
21702170``200.000 ``, ``2E2 ``, and ``.02E+4 `` all have the same value at
21712171various precisions. Is there a way to transform them to a single recognizable
21722172canonical value?
21732173
2174- A. The :meth: `~Decimal.normalize ` method maps all equivalent values to a single
2174+ A\ . The :meth: `~Decimal.normalize ` method maps all equivalent values to a single
21752175representative:
21762176
21772177 >>> values = map (Decimal, ' 200 200.000 2E2 .02E+4' .split())
21782178 >>> [v.normalize() for v in values]
21792179 [Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]
21802180
2181- Q. When does rounding occur in a computation?
2181+ Q\ . When does rounding occur in a computation?
21822182
2183- A. It occurs *after * the computation. The philosophy of the decimal
2183+ A\ . It occurs *after * the computation. The philosophy of the decimal
21842184specification is that numbers are considered exact and are created
21852185independent of the current context. They can even have greater
21862186precision than current context. Computations process with those
@@ -2198,10 +2198,10 @@ applied to the *result* of the computation::
21982198 >>> pi + 0 - Decimal('0.00005'). # Intermediate values are rounded
21992199 Decimal('3.1416')
22002200
2201- Q. Some decimal values always print with exponential notation. Is there a way
2201+ Q\ . Some decimal values always print with exponential notation. Is there a way
22022202to get a non-exponential representation?
22032203
2204- A. For some values, exponential notation is the only way to express the number
2204+ A\ . For some values, exponential notation is the only way to express the number
22052205of significant places in the coefficient. For example, expressing
22062206``5.0E+3 `` as ``5000 `` keeps the value constant but cannot show the
22072207original's two-place significance.
@@ -2216,9 +2216,9 @@ value unchanged:
22162216 >>> remove_exponent(Decimal(' 5E+3' ))
22172217 Decimal('5000')
22182218
2219- Q. Is there a way to convert a regular float to a :class: `Decimal `?
2219+ Q\ . Is there a way to convert a regular float to a :class: `Decimal `?
22202220
2221- A. Yes, any binary floating-point number can be exactly expressed as a
2221+ A\ . Yes, any binary floating-point number can be exactly expressed as a
22222222Decimal though an exact conversion may take more precision than intuition would
22232223suggest:
22242224
@@ -2227,19 +2227,19 @@ suggest:
22272227 >>> Decimal(math.pi)
22282228 Decimal('3.141592653589793115997963468544185161590576171875')
22292229
2230- Q. Within a complex calculation, how can I make sure that I haven't gotten a
2230+ Q\ . Within a complex calculation, how can I make sure that I haven't gotten a
22312231spurious result because of insufficient precision or rounding anomalies.
22322232
2233- A. The decimal module makes it easy to test results. A best practice is to
2233+ A\ . The decimal module makes it easy to test results. A best practice is to
22342234re-run calculations using greater precision and with various rounding modes.
22352235Widely differing results indicate insufficient precision, rounding mode issues,
22362236ill-conditioned inputs, or a numerically unstable algorithm.
22372237
2238- Q. I noticed that context precision is applied to the results of operations but
2238+ Q\ . I noticed that context precision is applied to the results of operations but
22392239not to the inputs. Is there anything to watch out for when mixing values of
22402240different precisions?
22412241
2242- A. Yes. The principle is that all values are considered to be exact and so is
2242+ A\ . Yes. The principle is that all values are considered to be exact and so is
22432243the arithmetic on those values. Only the results are rounded. The advantage
22442244for inputs is that "what you type is what you get". A disadvantage is that the
22452245results can look odd if you forget that the inputs haven't been rounded:
@@ -2267,9 +2267,9 @@ Alternatively, inputs can be rounded upon creation using the
22672267 >>> Context(prec = 5 , rounding = ROUND_DOWN ).create_decimal(' 1.2345678' )
22682268 Decimal('1.2345')
22692269
2270- Q. Is the CPython implementation fast for large numbers?
2270+ Q\ . Is the CPython implementation fast for large numbers?
22712271
2272- A. Yes. In the CPython and PyPy3 implementations, the C/CFFI versions of
2272+ A\ . Yes. In the CPython and PyPy3 implementations, the C/CFFI versions of
22732273the decimal module integrate the high speed `libmpdec
22742274<https://www.bytereef.org/mpdecimal/doc/libmpdec/index.html> `_ library for
22752275arbitrary precision correctly rounded decimal floating-point arithmetic [# ]_.
0 commit comments