Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
Raymond Hettinger | 13a7075 | 2008-02-10 07:21:09 +0000 | [diff] [blame] | 2 | :mod:`decimal` --- Decimal fixed point and floating point arithmetic |
| 3 | ==================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 4 | |
| 5 | .. module:: decimal |
| 6 | :synopsis: Implementation of the General Decimal Arithmetic Specification. |
| 7 | |
| 8 | |
| 9 | .. moduleauthor:: Eric Price <eprice at tjhsst.edu> |
| 10 | .. moduleauthor:: Facundo Batista <facundo at taniquetil.com.ar> |
| 11 | .. moduleauthor:: Raymond Hettinger <python at rcn.com> |
| 12 | .. moduleauthor:: Aahz <aahz at pobox.com> |
| 13 | .. moduleauthor:: Tim Peters <tim.one at comcast.net> |
| 14 | |
| 15 | |
| 16 | .. sectionauthor:: Raymond D. Hettinger <python at rcn.com> |
| 17 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | .. versionadded:: 2.4 |
| 19 | |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 20 | .. import modules for testing inline doctests with the Sphinx doctest builder |
Georg Brandl | 17baef0 | 2008-03-22 10:56:23 +0000 | [diff] [blame] | 21 | .. testsetup:: * |
| 22 | |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 23 | import decimal |
| 24 | import math |
Georg Brandl | 17baef0 | 2008-03-22 10:56:23 +0000 | [diff] [blame] | 25 | from decimal import * |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 26 | # make sure each group gets a fresh context |
| 27 | setcontext(Context()) |
Georg Brandl | 17baef0 | 2008-03-22 10:56:23 +0000 | [diff] [blame] | 28 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 29 | The :mod:`decimal` module provides support for decimal floating point |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 30 | arithmetic. It offers several advantages over the :class:`float` datatype: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 31 | |
Raymond Hettinger | 13a7075 | 2008-02-10 07:21:09 +0000 | [diff] [blame] | 32 | * Decimal "is based on a floating-point model which was designed with people |
| 33 | in mind, and necessarily has a paramount guiding principle -- computers must |
| 34 | provide an arithmetic that works in the same way as the arithmetic that |
| 35 | people learn at school." -- excerpt from the decimal arithmetic specification. |
| 36 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 37 | * Decimal numbers can be represented exactly. In contrast, numbers like |
Terry Jan Reedy | 4ba76b6 | 2012-01-13 23:41:31 -0500 | [diff] [blame] | 38 | :const:`1.1` and :const:`2.2` do not have exact representations in binary |
Mark Dickinson | 6b87f11 | 2009-11-24 14:27:02 +0000 | [diff] [blame] | 39 | floating point. End users typically would not expect ``1.1 + 2.2`` to display |
| 40 | as :const:`3.3000000000000003` as it does with binary floating point. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 41 | |
| 42 | * The exactness carries over into arithmetic. In decimal floating point, ``0.1 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 43 | + 0.1 + 0.1 - 0.3`` is exactly equal to zero. In binary floating point, the result |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 44 | is :const:`5.5511151231257827e-017`. While near to zero, the differences |
| 45 | prevent reliable equality testing and differences can accumulate. For this |
Raymond Hettinger | 13a7075 | 2008-02-10 07:21:09 +0000 | [diff] [blame] | 46 | reason, decimal is preferred in accounting applications which have strict |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 47 | equality invariants. |
| 48 | |
| 49 | * The decimal module incorporates a notion of significant places so that ``1.30 |
| 50 | + 1.20`` is :const:`2.50`. The trailing zero is kept to indicate significance. |
| 51 | This is the customary presentation for monetary applications. For |
| 52 | multiplication, the "schoolbook" approach uses all the figures in the |
| 53 | multiplicands. For instance, ``1.3 * 1.2`` gives :const:`1.56` while ``1.30 * |
| 54 | 1.20`` gives :const:`1.5600`. |
| 55 | |
| 56 | * Unlike hardware based binary floating point, the decimal module has a user |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 57 | alterable precision (defaulting to 28 places) which can be as large as needed for |
Georg Brandl | 17baef0 | 2008-03-22 10:56:23 +0000 | [diff] [blame] | 58 | a given problem: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 59 | |
Mark Dickinson | 0333517 | 2010-11-07 11:29:03 +0000 | [diff] [blame] | 60 | >>> from decimal import * |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 61 | >>> getcontext().prec = 6 |
| 62 | >>> Decimal(1) / Decimal(7) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 63 | Decimal('0.142857') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 64 | >>> getcontext().prec = 28 |
| 65 | >>> Decimal(1) / Decimal(7) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 66 | Decimal('0.1428571428571428571428571429') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 67 | |
| 68 | * Both binary and decimal floating point are implemented in terms of published |
| 69 | standards. While the built-in float type exposes only a modest portion of its |
| 70 | capabilities, the decimal module exposes all required parts of the standard. |
| 71 | When needed, the programmer has full control over rounding and signal handling. |
Raymond Hettinger | 13a7075 | 2008-02-10 07:21:09 +0000 | [diff] [blame] | 72 | This includes an option to enforce exact arithmetic by using exceptions |
| 73 | to block any inexact operations. |
| 74 | |
| 75 | * The decimal module was designed to support "without prejudice, both exact |
| 76 | unrounded decimal arithmetic (sometimes called fixed-point arithmetic) |
| 77 | and rounded floating-point arithmetic." -- excerpt from the decimal |
| 78 | arithmetic specification. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 79 | |
| 80 | The module design is centered around three concepts: the decimal number, the |
| 81 | context for arithmetic, and signals. |
| 82 | |
| 83 | A decimal number is immutable. It has a sign, coefficient digits, and an |
| 84 | exponent. To preserve significance, the coefficient digits do not truncate |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 85 | trailing zeros. Decimals also include special values such as |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 86 | :const:`Infinity`, :const:`-Infinity`, and :const:`NaN`. The standard also |
| 87 | differentiates :const:`-0` from :const:`+0`. |
| 88 | |
| 89 | The context for arithmetic is an environment specifying precision, rounding |
| 90 | rules, limits on exponents, flags indicating the results of operations, and trap |
| 91 | enablers which determine whether signals are treated as exceptions. Rounding |
| 92 | options include :const:`ROUND_CEILING`, :const:`ROUND_DOWN`, |
| 93 | :const:`ROUND_FLOOR`, :const:`ROUND_HALF_DOWN`, :const:`ROUND_HALF_EVEN`, |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 94 | :const:`ROUND_HALF_UP`, :const:`ROUND_UP`, and :const:`ROUND_05UP`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 95 | |
| 96 | Signals are groups of exceptional conditions arising during the course of |
| 97 | computation. Depending on the needs of the application, signals may be ignored, |
| 98 | considered as informational, or treated as exceptions. The signals in the |
| 99 | decimal module are: :const:`Clamped`, :const:`InvalidOperation`, |
| 100 | :const:`DivisionByZero`, :const:`Inexact`, :const:`Rounded`, :const:`Subnormal`, |
| 101 | :const:`Overflow`, and :const:`Underflow`. |
| 102 | |
| 103 | For each signal there is a flag and a trap enabler. When a signal is |
Mark Dickinson | 1840c1a | 2008-05-03 18:23:14 +0000 | [diff] [blame] | 104 | encountered, its flag is set to one, then, if the trap enabler is |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 105 | set to one, an exception is raised. Flags are sticky, so the user needs to |
| 106 | reset them before monitoring a calculation. |
| 107 | |
| 108 | |
| 109 | .. seealso:: |
| 110 | |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 111 | * IBM's General Decimal Arithmetic Specification, `The General Decimal Arithmetic |
Raymond Hettinger | f345a21 | 2009-03-10 01:07:30 +0000 | [diff] [blame] | 112 | Specification <http://speleotrove.com/decimal/>`_. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 113 | |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 114 | * IEEE standard 854-1987, `Unofficial IEEE 854 Text |
Mark Dickinson | ff6672f | 2008-02-07 01:14:23 +0000 | [diff] [blame] | 115 | <http://754r.ucbtest.org/standards/854.pdf>`_. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 116 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 117 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | .. _decimal-tutorial: |
| 121 | |
| 122 | Quick-start Tutorial |
| 123 | -------------------- |
| 124 | |
| 125 | The usual start to using decimals is importing the module, viewing the current |
| 126 | context with :func:`getcontext` and, if necessary, setting new values for |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 127 | precision, rounding, or enabled traps:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 128 | |
| 129 | >>> from decimal import * |
| 130 | >>> getcontext() |
| 131 | Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 132 | capitals=1, flags=[], traps=[Overflow, DivisionByZero, |
| 133 | InvalidOperation]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 134 | |
| 135 | >>> getcontext().prec = 7 # Set a new precision |
| 136 | |
Mark Dickinson | b1affc5 | 2010-04-04 22:09:21 +0000 | [diff] [blame] | 137 | Decimal instances can be constructed from integers, strings, floats, or tuples. |
| 138 | Construction from an integer or a float performs an exact conversion of the |
| 139 | value of that integer or float. Decimal numbers include special values such as |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | :const:`NaN` which stands for "Not a number", positive and negative |
Georg Brandl | 17baef0 | 2008-03-22 10:56:23 +0000 | [diff] [blame] | 141 | :const:`Infinity`, and :const:`-0`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 142 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 143 | >>> getcontext().prec = 28 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 144 | >>> Decimal(10) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 145 | Decimal('10') |
| 146 | >>> Decimal('3.14') |
| 147 | Decimal('3.14') |
Mark Dickinson | b1affc5 | 2010-04-04 22:09:21 +0000 | [diff] [blame] | 148 | >>> Decimal(3.14) |
| 149 | Decimal('3.140000000000000124344978758017532527446746826171875') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 150 | >>> Decimal((0, (3, 1, 4), -2)) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 151 | Decimal('3.14') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 152 | >>> Decimal(str(2.0 ** 0.5)) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 153 | Decimal('1.41421356237') |
| 154 | >>> Decimal(2) ** Decimal('0.5') |
| 155 | Decimal('1.414213562373095048801688724') |
| 156 | >>> Decimal('NaN') |
| 157 | Decimal('NaN') |
| 158 | >>> Decimal('-Infinity') |
| 159 | Decimal('-Infinity') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 160 | |
| 161 | The significance of a new Decimal is determined solely by the number of digits |
| 162 | input. Context precision and rounding only come into play during arithmetic |
Georg Brandl | 17baef0 | 2008-03-22 10:56:23 +0000 | [diff] [blame] | 163 | operations. |
| 164 | |
| 165 | .. doctest:: newcontext |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 166 | |
| 167 | >>> getcontext().prec = 6 |
| 168 | >>> Decimal('3.0') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 169 | Decimal('3.0') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 170 | >>> Decimal('3.1415926535') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 171 | Decimal('3.1415926535') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 172 | >>> Decimal('3.1415926535') + Decimal('2.7182818285') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 173 | Decimal('5.85987') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 174 | >>> getcontext().rounding = ROUND_UP |
| 175 | >>> Decimal('3.1415926535') + Decimal('2.7182818285') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 176 | Decimal('5.85988') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 177 | |
| 178 | Decimals interact well with much of the rest of Python. Here is a small decimal |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 179 | floating point flying circus: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 180 | |
Georg Brandl | 838b4b0 | 2008-03-22 13:07:06 +0000 | [diff] [blame] | 181 | .. doctest:: |
| 182 | :options: +NORMALIZE_WHITESPACE |
| 183 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 184 | >>> data = map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split()) |
| 185 | >>> max(data) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 186 | Decimal('9.25') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 187 | >>> min(data) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 188 | Decimal('0.03') |
Georg Brandl | 838b4b0 | 2008-03-22 13:07:06 +0000 | [diff] [blame] | 189 | >>> sorted(data) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 190 | [Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'), |
| 191 | Decimal('2.35'), Decimal('3.45'), Decimal('9.25')] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 192 | >>> sum(data) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 193 | Decimal('19.29') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 194 | >>> a,b,c = data[:3] |
| 195 | >>> str(a) |
| 196 | '1.34' |
| 197 | >>> float(a) |
Mark Dickinson | 6b87f11 | 2009-11-24 14:27:02 +0000 | [diff] [blame] | 198 | 1.34 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 199 | >>> round(a, 1) # round() first converts to binary floating point |
| 200 | 1.3 |
| 201 | >>> int(a) |
| 202 | 1 |
| 203 | >>> a * 5 |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 204 | Decimal('6.70') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 205 | >>> a * b |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 206 | Decimal('2.5058') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 207 | >>> c % a |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 208 | Decimal('0.77') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 209 | |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 210 | And some mathematical functions are also available to Decimal: |
Facundo Batista | e90bc3c | 2007-09-14 21:29:52 +0000 | [diff] [blame] | 211 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 212 | >>> getcontext().prec = 28 |
Facundo Batista | e90bc3c | 2007-09-14 21:29:52 +0000 | [diff] [blame] | 213 | >>> Decimal(2).sqrt() |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 214 | Decimal('1.414213562373095048801688724') |
Facundo Batista | e90bc3c | 2007-09-14 21:29:52 +0000 | [diff] [blame] | 215 | >>> Decimal(1).exp() |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 216 | Decimal('2.718281828459045235360287471') |
| 217 | >>> Decimal('10').ln() |
| 218 | Decimal('2.302585092994045684017991455') |
| 219 | >>> Decimal('10').log10() |
| 220 | Decimal('1') |
Facundo Batista | e90bc3c | 2007-09-14 21:29:52 +0000 | [diff] [blame] | 221 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 222 | The :meth:`quantize` method rounds a number to a fixed exponent. This method is |
| 223 | useful for monetary applications that often round results to a fixed number of |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 224 | places: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 225 | |
| 226 | >>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 227 | Decimal('7.32') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 228 | >>> Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 229 | Decimal('8') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 230 | |
| 231 | As shown above, the :func:`getcontext` function accesses the current context and |
| 232 | allows the settings to be changed. This approach meets the needs of most |
| 233 | applications. |
| 234 | |
| 235 | For more advanced work, it may be useful to create alternate contexts using the |
| 236 | Context() constructor. To make an alternate active, use the :func:`setcontext` |
| 237 | function. |
| 238 | |
| 239 | In accordance with the standard, the :mod:`Decimal` module provides two ready to |
| 240 | use standard contexts, :const:`BasicContext` and :const:`ExtendedContext`. The |
| 241 | former is especially useful for debugging because many of the traps are |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 242 | enabled: |
| 243 | |
| 244 | .. doctest:: newcontext |
| 245 | :options: +NORMALIZE_WHITESPACE |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 246 | |
| 247 | >>> myothercontext = Context(prec=60, rounding=ROUND_HALF_DOWN) |
| 248 | >>> setcontext(myothercontext) |
| 249 | >>> Decimal(1) / Decimal(7) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 250 | Decimal('0.142857142857142857142857142857142857142857142857142857142857') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 251 | |
| 252 | >>> ExtendedContext |
| 253 | Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, |
| 254 | capitals=1, flags=[], traps=[]) |
| 255 | >>> setcontext(ExtendedContext) |
| 256 | >>> Decimal(1) / Decimal(7) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 257 | Decimal('0.142857143') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 258 | >>> Decimal(42) / Decimal(0) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 259 | Decimal('Infinity') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 260 | |
| 261 | >>> setcontext(BasicContext) |
| 262 | >>> Decimal(42) / Decimal(0) |
| 263 | Traceback (most recent call last): |
| 264 | File "<pyshell#143>", line 1, in -toplevel- |
| 265 | Decimal(42) / Decimal(0) |
| 266 | DivisionByZero: x / 0 |
| 267 | |
| 268 | Contexts also have signal flags for monitoring exceptional conditions |
| 269 | encountered during computations. The flags remain set until explicitly cleared, |
| 270 | so it is best to clear the flags before each set of monitored computations by |
| 271 | using the :meth:`clear_flags` method. :: |
| 272 | |
| 273 | >>> setcontext(ExtendedContext) |
| 274 | >>> getcontext().clear_flags() |
| 275 | >>> Decimal(355) / Decimal(113) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 276 | Decimal('3.14159292') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 277 | >>> getcontext() |
| 278 | Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 279 | capitals=1, flags=[Rounded, Inexact], traps=[]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 280 | |
| 281 | The *flags* entry shows that the rational approximation to :const:`Pi` was |
| 282 | rounded (digits beyond the context precision were thrown away) and that the |
| 283 | result is inexact (some of the discarded digits were non-zero). |
| 284 | |
| 285 | Individual traps are set using the dictionary in the :attr:`traps` field of a |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 286 | context: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 287 | |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 288 | .. doctest:: newcontext |
| 289 | |
| 290 | >>> setcontext(ExtendedContext) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 291 | >>> Decimal(1) / Decimal(0) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 292 | Decimal('Infinity') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 293 | >>> getcontext().traps[DivisionByZero] = 1 |
| 294 | >>> Decimal(1) / Decimal(0) |
| 295 | Traceback (most recent call last): |
| 296 | File "<pyshell#112>", line 1, in -toplevel- |
| 297 | Decimal(1) / Decimal(0) |
| 298 | DivisionByZero: x / 0 |
| 299 | |
| 300 | Most programs adjust the current context only once, at the beginning of the |
| 301 | program. And, in many applications, data is converted to :class:`Decimal` with |
| 302 | a single cast inside a loop. With context set and decimals created, the bulk of |
| 303 | the program manipulates the data no differently than with other Python numeric |
| 304 | types. |
| 305 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 306 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 307 | |
| 308 | |
| 309 | .. _decimal-decimal: |
| 310 | |
| 311 | Decimal objects |
| 312 | --------------- |
| 313 | |
| 314 | |
| 315 | .. class:: Decimal([value [, context]]) |
| 316 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 317 | Construct a new :class:`Decimal` object based from *value*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 318 | |
Raymond Hettinger | ed171ab | 2010-04-02 18:39:24 +0000 | [diff] [blame] | 319 | *value* can be an integer, string, tuple, :class:`float`, or another :class:`Decimal` |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 320 | object. If no *value* is given, returns ``Decimal('0')``. If *value* is a |
Mark Dickinson | 59bc20b | 2008-01-12 01:56:00 +0000 | [diff] [blame] | 321 | string, it should conform to the decimal numeric string syntax after leading |
| 322 | and trailing whitespace characters are removed:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 323 | |
| 324 | sign ::= '+' | '-' |
| 325 | digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
| 326 | indicator ::= 'e' | 'E' |
| 327 | digits ::= digit [digit]... |
| 328 | decimal-part ::= digits '.' [digits] | ['.'] digits |
| 329 | exponent-part ::= indicator [sign] digits |
| 330 | infinity ::= 'Infinity' | 'Inf' |
| 331 | nan ::= 'NaN' [digits] | 'sNaN' [digits] |
| 332 | numeric-value ::= decimal-part [exponent-part] | infinity |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 333 | numeric-string ::= [sign] numeric-value | [sign] nan |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 334 | |
Mark Dickinson | 4326ad8 | 2009-08-02 10:59:36 +0000 | [diff] [blame] | 335 | If *value* is a unicode string then other Unicode decimal digits |
| 336 | are also permitted where ``digit`` appears above. These include |
| 337 | decimal digits from various other alphabets (for example, |
| 338 | Arabic-Indic and Devanāgarī digits) along with the fullwidth digits |
| 339 | ``u'\uff10'`` through ``u'\uff19'``. |
| 340 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 341 | If *value* is a :class:`tuple`, it should have three components, a sign |
| 342 | (:const:`0` for positive or :const:`1` for negative), a :class:`tuple` of |
| 343 | digits, and an integer exponent. For example, ``Decimal((0, (1, 4, 1, 4), -3))`` |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 344 | returns ``Decimal('1.414')``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 345 | |
Raymond Hettinger | ed171ab | 2010-04-02 18:39:24 +0000 | [diff] [blame] | 346 | If *value* is a :class:`float`, the binary floating point value is losslessly |
| 347 | converted to its exact decimal equivalent. This conversion can often require |
Mark Dickinson | b1affc5 | 2010-04-04 22:09:21 +0000 | [diff] [blame] | 348 | 53 or more digits of precision. For example, ``Decimal(float('1.1'))`` |
| 349 | converts to |
| 350 | ``Decimal('1.100000000000000088817841970012523233890533447265625')``. |
Raymond Hettinger | ed171ab | 2010-04-02 18:39:24 +0000 | [diff] [blame] | 351 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 352 | The *context* precision does not affect how many digits are stored. That is |
| 353 | determined exclusively by the number of digits in *value*. For example, |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 354 | ``Decimal('3.00000')`` records all five zeros even if the context precision is |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 355 | only three. |
| 356 | |
| 357 | The purpose of the *context* argument is determining what to do if *value* is a |
| 358 | malformed string. If the context traps :const:`InvalidOperation`, an exception |
| 359 | is raised; otherwise, the constructor returns a new Decimal with the value of |
| 360 | :const:`NaN`. |
| 361 | |
| 362 | Once constructed, :class:`Decimal` objects are immutable. |
| 363 | |
Mark Dickinson | 59bc20b | 2008-01-12 01:56:00 +0000 | [diff] [blame] | 364 | .. versionchanged:: 2.6 |
| 365 | leading and trailing whitespace characters are permitted when |
| 366 | creating a Decimal instance from a string. |
| 367 | |
Mark Dickinson | b1affc5 | 2010-04-04 22:09:21 +0000 | [diff] [blame] | 368 | .. versionchanged:: 2.7 |
Ezio Melotti | 6f65d2d | 2010-04-04 23:21:53 +0000 | [diff] [blame] | 369 | The argument to the constructor is now permitted to be a :class:`float` instance. |
Mark Dickinson | b1affc5 | 2010-04-04 22:09:21 +0000 | [diff] [blame] | 370 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 371 | Decimal floating point objects share many properties with the other built-in |
| 372 | numeric types such as :class:`float` and :class:`int`. All of the usual math |
| 373 | operations and special methods apply. Likewise, decimal objects can be |
| 374 | copied, pickled, printed, used as dictionary keys, used as set elements, |
| 375 | compared, sorted, and coerced to another type (such as :class:`float` or |
| 376 | :class:`long`). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 377 | |
Mark Dickinson | 99d8096 | 2010-04-02 08:53:22 +0000 | [diff] [blame] | 378 | Decimal objects cannot generally be combined with floats in |
| 379 | arithmetic operations: an attempt to add a :class:`Decimal` to a |
| 380 | :class:`float`, for example, will raise a :exc:`TypeError`. |
| 381 | There's one exception to this rule: it's possible to use Python's |
| 382 | comparison operators to compare a :class:`float` instance ``x`` |
| 383 | with a :class:`Decimal` instance ``y``. Without this exception, |
| 384 | comparisons between :class:`Decimal` and :class:`float` instances |
| 385 | would follow the general rules for comparing objects of different |
| 386 | types described in the :ref:`expressions` section of the reference |
| 387 | manual, leading to confusing results. |
| 388 | |
| 389 | .. versionchanged:: 2.7 |
| 390 | A comparison between a :class:`float` instance ``x`` and a |
| 391 | :class:`Decimal` instance ``y`` now returns a result based on |
| 392 | the values of ``x`` and ``y``. In earlier versions ``x < y`` |
| 393 | returned the same (arbitrary) result for any :class:`Decimal` |
| 394 | instance ``x`` and any :class:`float` instance ``y``. |
| 395 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 396 | In addition to the standard numeric properties, decimal floating point |
| 397 | objects also have a number of specialized methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 398 | |
| 399 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 400 | .. method:: adjusted() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 401 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 402 | Return the adjusted exponent after shifting out the coefficient's |
| 403 | rightmost digits until only the lead digit remains: |
| 404 | ``Decimal('321e+5').adjusted()`` returns seven. Used for determining the |
| 405 | position of the most significant digit with respect to the decimal point. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 406 | |
| 407 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 408 | .. method:: as_tuple() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 409 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 410 | Return a :term:`named tuple` representation of the number: |
| 411 | ``DecimalTuple(sign, digits, exponent)``. |
Georg Brandl | e3c3db5 | 2008-01-11 09:55:53 +0000 | [diff] [blame] | 412 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 413 | .. versionchanged:: 2.6 |
| 414 | Use a named tuple. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 415 | |
| 416 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 417 | .. method:: canonical() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 418 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 419 | Return the canonical encoding of the argument. Currently, the encoding of |
| 420 | a :class:`Decimal` instance is always canonical, so this operation returns |
| 421 | its argument unchanged. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 422 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 423 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 424 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 425 | .. method:: compare(other[, context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 426 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 427 | Compare the values of two Decimal instances. This operation behaves in |
| 428 | the same way as the usual comparison method :meth:`__cmp__`, except that |
| 429 | :meth:`compare` returns a Decimal instance rather than an integer, and if |
| 430 | either operand is a NaN then the result is a NaN:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 431 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 432 | a or b is a NaN ==> Decimal('NaN') |
| 433 | a < b ==> Decimal('-1') |
| 434 | a == b ==> Decimal('0') |
| 435 | a > b ==> Decimal('1') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 436 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 437 | .. method:: compare_signal(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 438 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 439 | This operation is identical to the :meth:`compare` method, except that all |
| 440 | NaNs signal. That is, if neither operand is a signaling NaN then any |
| 441 | quiet NaN operand is treated as though it were a signaling NaN. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 442 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 443 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 444 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 445 | .. method:: compare_total(other) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 446 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 447 | Compare two operands using their abstract representation rather than their |
| 448 | numerical value. Similar to the :meth:`compare` method, but the result |
| 449 | gives a total ordering on :class:`Decimal` instances. Two |
| 450 | :class:`Decimal` instances with the same numeric value but different |
| 451 | representations compare unequal in this ordering: |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 452 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 453 | >>> Decimal('12.0').compare_total(Decimal('12')) |
| 454 | Decimal('-1') |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 455 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 456 | Quiet and signaling NaNs are also included in the total ordering. The |
| 457 | result of this function is ``Decimal('0')`` if both operands have the same |
| 458 | representation, ``Decimal('-1')`` if the first operand is lower in the |
| 459 | total order than the second, and ``Decimal('1')`` if the first operand is |
| 460 | higher in the total order than the second operand. See the specification |
| 461 | for details of the total order. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 462 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 463 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 464 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 465 | .. method:: compare_total_mag(other) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 466 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 467 | Compare two operands using their abstract representation rather than their |
| 468 | value as in :meth:`compare_total`, but ignoring the sign of each operand. |
| 469 | ``x.compare_total_mag(y)`` is equivalent to |
| 470 | ``x.copy_abs().compare_total(y.copy_abs())``. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 471 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 472 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 473 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 474 | .. method:: conjugate() |
| 475 | |
| 476 | Just returns self, this method is only to comply with the Decimal |
| 477 | Specification. |
| 478 | |
| 479 | .. versionadded:: 2.6 |
| 480 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 481 | .. method:: copy_abs() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 482 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 483 | Return the absolute value of the argument. This operation is unaffected |
| 484 | by the context and is quiet: no flags are changed and no rounding is |
| 485 | performed. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 486 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 487 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 488 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 489 | .. method:: copy_negate() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 490 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 491 | Return the negation of the argument. This operation is unaffected by the |
| 492 | context and is quiet: no flags are changed and no rounding is performed. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 493 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 494 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 495 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 496 | .. method:: copy_sign(other) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 497 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 498 | Return a copy of the first operand with the sign set to be the same as the |
| 499 | sign of the second operand. For example: |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 500 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 501 | >>> Decimal('2.3').copy_sign(Decimal('-1.5')) |
| 502 | Decimal('-2.3') |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 503 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 504 | This operation is unaffected by the context and is quiet: no flags are |
| 505 | changed and no rounding is performed. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 506 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 507 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 508 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 509 | .. method:: exp([context]) |
Facundo Batista | e90bc3c | 2007-09-14 21:29:52 +0000 | [diff] [blame] | 510 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 511 | Return the value of the (natural) exponential function ``e**x`` at the |
| 512 | given number. The result is correctly rounded using the |
| 513 | :const:`ROUND_HALF_EVEN` rounding mode. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 514 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 515 | >>> Decimal(1).exp() |
| 516 | Decimal('2.718281828459045235360287471') |
| 517 | >>> Decimal(321).exp() |
| 518 | Decimal('2.561702493119680037517373933E+139') |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 519 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 520 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 521 | |
Raymond Hettinger | f4d8597 | 2009-01-03 19:02:23 +0000 | [diff] [blame] | 522 | .. method:: from_float(f) |
| 523 | |
| 524 | Classmethod that converts a float to a decimal number, exactly. |
| 525 | |
| 526 | Note `Decimal.from_float(0.1)` is not the same as `Decimal('0.1')`. |
| 527 | Since 0.1 is not exactly representable in binary floating point, the |
| 528 | value is stored as the nearest representable value which is |
| 529 | `0x1.999999999999ap-4`. That equivalent value in decimal is |
| 530 | `0.1000000000000000055511151231257827021181583404541015625`. |
| 531 | |
Mark Dickinson | b1affc5 | 2010-04-04 22:09:21 +0000 | [diff] [blame] | 532 | .. note:: From Python 2.7 onwards, a :class:`Decimal` instance |
| 533 | can also be constructed directly from a :class:`float`. |
| 534 | |
Raymond Hettinger | f4d8597 | 2009-01-03 19:02:23 +0000 | [diff] [blame] | 535 | .. doctest:: |
| 536 | |
| 537 | >>> Decimal.from_float(0.1) |
| 538 | Decimal('0.1000000000000000055511151231257827021181583404541015625') |
| 539 | >>> Decimal.from_float(float('nan')) |
| 540 | Decimal('NaN') |
| 541 | >>> Decimal.from_float(float('inf')) |
| 542 | Decimal('Infinity') |
| 543 | >>> Decimal.from_float(float('-inf')) |
| 544 | Decimal('-Infinity') |
| 545 | |
| 546 | .. versionadded:: 2.7 |
| 547 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 548 | .. method:: fma(other, third[, context]) |
Facundo Batista | e90bc3c | 2007-09-14 21:29:52 +0000 | [diff] [blame] | 549 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 550 | Fused multiply-add. Return self*other+third with no rounding of the |
| 551 | intermediate product self*other. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 552 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 553 | >>> Decimal(2).fma(3, 5) |
| 554 | Decimal('11') |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 555 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 556 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 557 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 558 | .. method:: is_canonical() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 559 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 560 | Return :const:`True` if the argument is canonical and :const:`False` |
| 561 | otherwise. Currently, a :class:`Decimal` instance is always canonical, so |
| 562 | this operation always returns :const:`True`. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 563 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 564 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 565 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 566 | .. method:: is_finite() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 567 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 568 | Return :const:`True` if the argument is a finite number, and |
| 569 | :const:`False` if the argument is an infinity or a NaN. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 570 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 571 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 572 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 573 | .. method:: is_infinite() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 574 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 575 | Return :const:`True` if the argument is either positive or negative |
| 576 | infinity and :const:`False` otherwise. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 577 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 578 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 579 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 580 | .. method:: is_nan() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 581 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 582 | Return :const:`True` if the argument is a (quiet or signaling) NaN and |
| 583 | :const:`False` otherwise. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 584 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 585 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 586 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 587 | .. method:: is_normal() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 588 | |
Raymond Hettinger | eecd1dc6 | 2009-03-10 04:40:24 +0000 | [diff] [blame] | 589 | Return :const:`True` if the argument is a *normal* finite non-zero |
| 590 | number with an adjusted exponent greater than or equal to *Emin*. |
| 591 | Return :const:`False` if the argument is zero, subnormal, infinite or a |
| 592 | NaN. Note, the term *normal* is used here in a different sense with |
| 593 | the :meth:`normalize` method which is used to create canonical values. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 594 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 595 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 596 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 597 | .. method:: is_qnan() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 598 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 599 | Return :const:`True` if the argument is a quiet NaN, and |
| 600 | :const:`False` otherwise. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 601 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 602 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 603 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 604 | .. method:: is_signed() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 605 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 606 | Return :const:`True` if the argument has a negative sign and |
| 607 | :const:`False` otherwise. Note that zeros and NaNs can both carry signs. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 608 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 609 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 610 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 611 | .. method:: is_snan() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 612 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 613 | Return :const:`True` if the argument is a signaling NaN and :const:`False` |
| 614 | otherwise. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 615 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 616 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 617 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 618 | .. method:: is_subnormal() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 619 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 620 | Return :const:`True` if the argument is subnormal, and :const:`False` |
Raymond Hettinger | eecd1dc6 | 2009-03-10 04:40:24 +0000 | [diff] [blame] | 621 | otherwise. A number is subnormal is if it is nonzero, finite, and has an |
| 622 | adjusted exponent less than *Emin*. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 623 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 624 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 625 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 626 | .. method:: is_zero() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 627 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 628 | Return :const:`True` if the argument is a (positive or negative) zero and |
| 629 | :const:`False` otherwise. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 630 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 631 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 632 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 633 | .. method:: ln([context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 634 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 635 | Return the natural (base e) logarithm of the operand. The result is |
| 636 | correctly rounded using the :const:`ROUND_HALF_EVEN` rounding mode. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 637 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 638 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 639 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 640 | .. method:: log10([context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 641 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 642 | Return the base ten logarithm of the operand. The result is correctly |
| 643 | rounded using the :const:`ROUND_HALF_EVEN` rounding mode. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 644 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 645 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 646 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 647 | .. method:: logb([context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 648 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 649 | For a nonzero number, return the adjusted exponent of its operand as a |
| 650 | :class:`Decimal` instance. If the operand is a zero then |
| 651 | ``Decimal('-Infinity')`` is returned and the :const:`DivisionByZero` flag |
| 652 | is raised. If the operand is an infinity then ``Decimal('Infinity')`` is |
| 653 | returned. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 654 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 655 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 656 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 657 | .. method:: logical_and(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 658 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 659 | :meth:`logical_and` is a logical operation which takes two *logical |
| 660 | operands* (see :ref:`logical_operands_label`). The result is the |
| 661 | digit-wise ``and`` of the two operands. |
| 662 | |
| 663 | .. versionadded:: 2.6 |
| 664 | |
Georg Brandl | 3d5c87a | 2009-06-30 16:15:43 +0000 | [diff] [blame] | 665 | .. method:: logical_invert([context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 666 | |
Georg Brandl | 3d5c87a | 2009-06-30 16:15:43 +0000 | [diff] [blame] | 667 | :meth:`logical_invert` is a logical operation. The |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 668 | result is the digit-wise inversion of the operand. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 669 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 670 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 671 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 672 | .. method:: logical_or(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 673 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 674 | :meth:`logical_or` is a logical operation which takes two *logical |
| 675 | operands* (see :ref:`logical_operands_label`). The result is the |
| 676 | digit-wise ``or`` of the two operands. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 677 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 678 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 679 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 680 | .. method:: logical_xor(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 681 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 682 | :meth:`logical_xor` is a logical operation which takes two *logical |
| 683 | operands* (see :ref:`logical_operands_label`). The result is the |
| 684 | digit-wise exclusive or of the two operands. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 685 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 686 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 687 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 688 | .. method:: max(other[, context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 689 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 690 | Like ``max(self, other)`` except that the context rounding rule is applied |
| 691 | before returning and that :const:`NaN` values are either signaled or |
| 692 | ignored (depending on the context and whether they are signaling or |
| 693 | quiet). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 694 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 695 | .. method:: max_mag(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 696 | |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 697 | Similar to the :meth:`.max` method, but the comparison is done using the |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 698 | absolute values of the operands. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 699 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 700 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 701 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 702 | .. method:: min(other[, context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 703 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 704 | Like ``min(self, other)`` except that the context rounding rule is applied |
| 705 | before returning and that :const:`NaN` values are either signaled or |
| 706 | ignored (depending on the context and whether they are signaling or |
| 707 | quiet). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 708 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 709 | .. method:: min_mag(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 710 | |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 711 | Similar to the :meth:`.min` method, but the comparison is done using the |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 712 | absolute values of the operands. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 713 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 714 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 715 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 716 | .. method:: next_minus([context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 717 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 718 | Return the largest number representable in the given context (or in the |
| 719 | current thread's context if no context is given) that is smaller than the |
| 720 | given operand. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 721 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 722 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 723 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 724 | .. method:: next_plus([context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 725 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 726 | Return the smallest number representable in the given context (or in the |
| 727 | current thread's context if no context is given) that is larger than the |
| 728 | given operand. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 729 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 730 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 731 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 732 | .. method:: next_toward(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 733 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 734 | If the two operands are unequal, return the number closest to the first |
| 735 | operand in the direction of the second operand. If both operands are |
| 736 | numerically equal, return a copy of the first operand with the sign set to |
| 737 | be the same as the sign of the second operand. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 738 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 739 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 740 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 741 | .. method:: normalize([context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 742 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 743 | Normalize the number by stripping the rightmost trailing zeros and |
| 744 | converting any result equal to :const:`Decimal('0')` to |
Senthil Kumaran | 6f18b98 | 2011-07-04 12:50:02 -0700 | [diff] [blame] | 745 | :const:`Decimal('0e0')`. Used for producing canonical values for attributes |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 746 | of an equivalence class. For example, ``Decimal('32.100')`` and |
| 747 | ``Decimal('0.321000e+2')`` both normalize to the equivalent value |
| 748 | ``Decimal('32.1')``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 749 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 750 | .. method:: number_class([context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 751 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 752 | Return a string describing the *class* of the operand. The returned value |
| 753 | is one of the following ten strings. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 754 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 755 | * ``"-Infinity"``, indicating that the operand is negative infinity. |
| 756 | * ``"-Normal"``, indicating that the operand is a negative normal number. |
| 757 | * ``"-Subnormal"``, indicating that the operand is negative and subnormal. |
| 758 | * ``"-Zero"``, indicating that the operand is a negative zero. |
| 759 | * ``"+Zero"``, indicating that the operand is a positive zero. |
| 760 | * ``"+Subnormal"``, indicating that the operand is positive and subnormal. |
| 761 | * ``"+Normal"``, indicating that the operand is a positive normal number. |
| 762 | * ``"+Infinity"``, indicating that the operand is positive infinity. |
| 763 | * ``"NaN"``, indicating that the operand is a quiet NaN (Not a Number). |
| 764 | * ``"sNaN"``, indicating that the operand is a signaling NaN. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 765 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 766 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 767 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 768 | .. method:: quantize(exp[, rounding[, context[, watchexp]]]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 769 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 770 | Return a value equal to the first operand after rounding and having the |
| 771 | exponent of the second operand. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 772 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 773 | >>> Decimal('1.41421356').quantize(Decimal('1.000')) |
| 774 | Decimal('1.414') |
Facundo Batista | e90bc3c | 2007-09-14 21:29:52 +0000 | [diff] [blame] | 775 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 776 | Unlike other operations, if the length of the coefficient after the |
| 777 | quantize operation would be greater than precision, then an |
| 778 | :const:`InvalidOperation` is signaled. This guarantees that, unless there |
| 779 | is an error condition, the quantized exponent is always equal to that of |
| 780 | the right-hand operand. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 781 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 782 | Also unlike other operations, quantize never signals Underflow, even if |
| 783 | the result is subnormal and inexact. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 784 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 785 | If the exponent of the second operand is larger than that of the first |
| 786 | then rounding may be necessary. In this case, the rounding mode is |
| 787 | determined by the ``rounding`` argument if given, else by the given |
| 788 | ``context`` argument; if neither argument is given the rounding mode of |
| 789 | the current thread's context is used. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 790 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 791 | If *watchexp* is set (default), then an error is returned whenever the |
| 792 | resulting exponent is greater than :attr:`Emax` or less than |
| 793 | :attr:`Etiny`. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 794 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 795 | .. method:: radix() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 796 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 797 | Return ``Decimal(10)``, the radix (base) in which the :class:`Decimal` |
| 798 | class does all its arithmetic. Included for compatibility with the |
| 799 | specification. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 800 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 801 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 802 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 803 | .. method:: remainder_near(other[, context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 804 | |
Mark Dickinson | 89e8f54 | 2012-10-31 19:44:09 +0000 | [diff] [blame^] | 805 | Return the remainder from dividing *self* by *other*. This differs from |
| 806 | ``self % other`` in that the sign of the remainder is chosen so as to |
| 807 | minimize its absolute value. More precisely, the return value is |
| 808 | ``self - n * other`` where ``n`` is the integer nearest to the exact |
| 809 | value of ``self / other``, and if two integers are equally near then the |
| 810 | even one is chosen. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 811 | |
Mark Dickinson | 89e8f54 | 2012-10-31 19:44:09 +0000 | [diff] [blame^] | 812 | If the result is zero then its sign will be the sign of *self*. |
| 813 | |
| 814 | >>> Decimal(18).remainder_near(Decimal(10)) |
| 815 | Decimal('-2') |
| 816 | >>> Decimal(25).remainder_near(Decimal(10)) |
| 817 | Decimal('5') |
| 818 | >>> Decimal(35).remainder_near(Decimal(10)) |
| 819 | Decimal('-5') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 820 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 821 | .. method:: rotate(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 822 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 823 | Return the result of rotating the digits of the first operand by an amount |
| 824 | specified by the second operand. The second operand must be an integer in |
| 825 | the range -precision through precision. The absolute value of the second |
| 826 | operand gives the number of places to rotate. If the second operand is |
| 827 | positive then rotation is to the left; otherwise rotation is to the right. |
| 828 | The coefficient of the first operand is padded on the left with zeros to |
| 829 | length precision if necessary. The sign and exponent of the first operand |
| 830 | are unchanged. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 831 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 832 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 833 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 834 | .. method:: same_quantum(other[, context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 835 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 836 | Test whether self and other have the same exponent or whether both are |
| 837 | :const:`NaN`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 838 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 839 | .. method:: scaleb(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 840 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 841 | Return the first operand with exponent adjusted by the second. |
| 842 | Equivalently, return the first operand multiplied by ``10**other``. The |
| 843 | second operand must be an integer. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 844 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 845 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 846 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 847 | .. method:: shift(other[, context]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 848 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 849 | Return the result of shifting the digits of the first operand by an amount |
| 850 | specified by the second operand. The second operand must be an integer in |
| 851 | the range -precision through precision. The absolute value of the second |
| 852 | operand gives the number of places to shift. If the second operand is |
| 853 | positive then the shift is to the left; otherwise the shift is to the |
| 854 | right. Digits shifted into the coefficient are zeros. The sign and |
| 855 | exponent of the first operand are unchanged. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 856 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 857 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 858 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 859 | .. method:: sqrt([context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 860 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 861 | Return the square root of the argument to full precision. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 862 | |
| 863 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 864 | .. method:: to_eng_string([context]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 865 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 866 | Convert to an engineering-type string. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 867 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 868 | Engineering notation has an exponent which is a multiple of 3, so there |
| 869 | are up to 3 digits left of the decimal place. For example, converts |
| 870 | ``Decimal('123E+1')`` to ``Decimal('1.23E+3')`` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 871 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 872 | .. method:: to_integral([rounding[, context]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 873 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 874 | Identical to the :meth:`to_integral_value` method. The ``to_integral`` |
| 875 | name has been kept for compatibility with older versions. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 876 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 877 | .. method:: to_integral_exact([rounding[, context]]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 878 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 879 | Round to the nearest integer, signaling :const:`Inexact` or |
| 880 | :const:`Rounded` as appropriate if rounding occurs. The rounding mode is |
| 881 | determined by the ``rounding`` parameter if given, else by the given |
| 882 | ``context``. If neither parameter is given then the rounding mode of the |
| 883 | current context is used. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 884 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 885 | .. versionadded:: 2.6 |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 886 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 887 | .. method:: to_integral_value([rounding[, context]]) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 888 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 889 | Round to the nearest integer without signaling :const:`Inexact` or |
| 890 | :const:`Rounded`. If given, applies *rounding*; otherwise, uses the |
| 891 | rounding method in either the supplied *context* or the current context. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 892 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 893 | .. versionchanged:: 2.6 |
| 894 | renamed from ``to_integral`` to ``to_integral_value``. The old name |
| 895 | remains valid for compatibility. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 896 | |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 897 | .. _logical_operands_label: |
| 898 | |
| 899 | Logical operands |
| 900 | ^^^^^^^^^^^^^^^^ |
| 901 | |
| 902 | The :meth:`logical_and`, :meth:`logical_invert`, :meth:`logical_or`, |
| 903 | and :meth:`logical_xor` methods expect their arguments to be *logical |
| 904 | operands*. A *logical operand* is a :class:`Decimal` instance whose |
| 905 | exponent and sign are both zero, and whose digits are all either |
| 906 | :const:`0` or :const:`1`. |
| 907 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 908 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 909 | |
| 910 | |
| 911 | .. _decimal-context: |
| 912 | |
| 913 | Context objects |
| 914 | --------------- |
| 915 | |
| 916 | Contexts are environments for arithmetic operations. They govern precision, set |
| 917 | rules for rounding, determine which signals are treated as exceptions, and limit |
| 918 | the range for exponents. |
| 919 | |
| 920 | Each thread has its own current context which is accessed or changed using the |
| 921 | :func:`getcontext` and :func:`setcontext` functions: |
| 922 | |
| 923 | |
| 924 | .. function:: getcontext() |
| 925 | |
| 926 | Return the current context for the active thread. |
| 927 | |
| 928 | |
| 929 | .. function:: setcontext(c) |
| 930 | |
| 931 | Set the current context for the active thread to *c*. |
| 932 | |
| 933 | Beginning with Python 2.5, you can also use the :keyword:`with` statement and |
| 934 | the :func:`localcontext` function to temporarily change the active context. |
| 935 | |
| 936 | |
| 937 | .. function:: localcontext([c]) |
| 938 | |
| 939 | Return a context manager that will set the current context for the active thread |
| 940 | to a copy of *c* on entry to the with-statement and restore the previous context |
| 941 | when exiting the with-statement. If no context is specified, a copy of the |
| 942 | current context is used. |
| 943 | |
| 944 | .. versionadded:: 2.5 |
| 945 | |
| 946 | For example, the following code sets the current decimal precision to 42 places, |
| 947 | performs a calculation, and then automatically restores the previous context:: |
| 948 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 949 | from decimal import localcontext |
| 950 | |
| 951 | with localcontext() as ctx: |
| 952 | ctx.prec = 42 # Perform a high precision calculation |
| 953 | s = calculate_something() |
| 954 | s = +s # Round the final result back to the default precision |
| 955 | |
Raymond Hettinger | 56f5c38 | 2012-05-11 12:50:11 -0700 | [diff] [blame] | 956 | with localcontext(BasicContext): # temporarily use the BasicContext |
| 957 | print Decimal(1) / Decimal(7) |
| 958 | print Decimal(355) / Decimal(113) |
| 959 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 960 | New contexts can also be created using the :class:`Context` constructor |
| 961 | described below. In addition, the module provides three pre-made contexts: |
| 962 | |
| 963 | |
| 964 | .. class:: BasicContext |
| 965 | |
| 966 | This is a standard context defined by the General Decimal Arithmetic |
| 967 | Specification. Precision is set to nine. Rounding is set to |
| 968 | :const:`ROUND_HALF_UP`. All flags are cleared. All traps are enabled (treated |
| 969 | as exceptions) except :const:`Inexact`, :const:`Rounded`, and |
| 970 | :const:`Subnormal`. |
| 971 | |
| 972 | Because many of the traps are enabled, this context is useful for debugging. |
| 973 | |
| 974 | |
| 975 | .. class:: ExtendedContext |
| 976 | |
| 977 | This is a standard context defined by the General Decimal Arithmetic |
| 978 | Specification. Precision is set to nine. Rounding is set to |
| 979 | :const:`ROUND_HALF_EVEN`. All flags are cleared. No traps are enabled (so that |
| 980 | exceptions are not raised during computations). |
| 981 | |
Mark Dickinson | 3a94ee0 | 2008-02-10 15:19:58 +0000 | [diff] [blame] | 982 | Because the traps are disabled, this context is useful for applications that |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 983 | prefer to have result value of :const:`NaN` or :const:`Infinity` instead of |
| 984 | raising exceptions. This allows an application to complete a run in the |
| 985 | presence of conditions that would otherwise halt the program. |
| 986 | |
| 987 | |
| 988 | .. class:: DefaultContext |
| 989 | |
| 990 | This context is used by the :class:`Context` constructor as a prototype for new |
| 991 | contexts. Changing a field (such a precision) has the effect of changing the |
Stefan Krah | 3d08d88 | 2010-05-29 12:54:35 +0000 | [diff] [blame] | 992 | default for new contexts created by the :class:`Context` constructor. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 993 | |
| 994 | This context is most useful in multi-threaded environments. Changing one of the |
| 995 | fields before threads are started has the effect of setting system-wide |
| 996 | defaults. Changing the fields after threads have started is not recommended as |
| 997 | it would require thread synchronization to prevent race conditions. |
| 998 | |
| 999 | In single threaded environments, it is preferable to not use this context at |
| 1000 | all. Instead, simply create contexts explicitly as described below. |
| 1001 | |
| 1002 | The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled traps |
| 1003 | for Overflow, InvalidOperation, and DivisionByZero. |
| 1004 | |
| 1005 | In addition to the three supplied contexts, new contexts can be created with the |
| 1006 | :class:`Context` constructor. |
| 1007 | |
| 1008 | |
| 1009 | .. class:: Context(prec=None, rounding=None, traps=None, flags=None, Emin=None, Emax=None, capitals=1) |
| 1010 | |
| 1011 | Creates a new context. If a field is not specified or is :const:`None`, the |
| 1012 | default values are copied from the :const:`DefaultContext`. If the *flags* |
| 1013 | field is not specified or is :const:`None`, all flags are cleared. |
| 1014 | |
| 1015 | The *prec* field is a positive integer that sets the precision for arithmetic |
| 1016 | operations in the context. |
| 1017 | |
| 1018 | The *rounding* option is one of: |
| 1019 | |
| 1020 | * :const:`ROUND_CEILING` (towards :const:`Infinity`), |
| 1021 | * :const:`ROUND_DOWN` (towards zero), |
| 1022 | * :const:`ROUND_FLOOR` (towards :const:`-Infinity`), |
| 1023 | * :const:`ROUND_HALF_DOWN` (to nearest with ties going towards zero), |
| 1024 | * :const:`ROUND_HALF_EVEN` (to nearest with ties going to nearest even integer), |
| 1025 | * :const:`ROUND_HALF_UP` (to nearest with ties going away from zero), or |
| 1026 | * :const:`ROUND_UP` (away from zero). |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1027 | * :const:`ROUND_05UP` (away from zero if last digit after rounding towards zero |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 1028 | would have been 0 or 5; otherwise towards zero) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1029 | |
| 1030 | The *traps* and *flags* fields list any signals to be set. Generally, new |
| 1031 | contexts should only set traps and leave the flags clear. |
| 1032 | |
| 1033 | The *Emin* and *Emax* fields are integers specifying the outer limits allowable |
| 1034 | for exponents. |
| 1035 | |
| 1036 | The *capitals* field is either :const:`0` or :const:`1` (the default). If set to |
| 1037 | :const:`1`, exponents are printed with a capital :const:`E`; otherwise, a |
| 1038 | lowercase :const:`e` is used: :const:`Decimal('6.02e+23')`. |
| 1039 | |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 1040 | .. versionchanged:: 2.6 |
| 1041 | The :const:`ROUND_05UP` rounding mode was added. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1042 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1043 | The :class:`Context` class defines several general purpose methods as well as |
| 1044 | a large number of methods for doing arithmetic directly in a given context. |
| 1045 | In addition, for each of the :class:`Decimal` methods described above (with |
| 1046 | the exception of the :meth:`adjusted` and :meth:`as_tuple` methods) there is |
Mark Dickinson | 6d8effb | 2010-02-18 14:27:02 +0000 | [diff] [blame] | 1047 | a corresponding :class:`Context` method. For example, for a :class:`Context` |
| 1048 | instance ``C`` and :class:`Decimal` instance ``x``, ``C.exp(x)`` is |
| 1049 | equivalent to ``x.exp(context=C)``. Each :class:`Context` method accepts a |
| 1050 | Python integer (an instance of :class:`int` or :class:`long`) anywhere that a |
| 1051 | Decimal instance is accepted. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1052 | |
| 1053 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1054 | .. method:: clear_flags() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1055 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1056 | Resets all of the flags to :const:`0`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1057 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1058 | .. method:: copy() |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 1059 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1060 | Return a duplicate of the context. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1061 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1062 | .. method:: copy_decimal(num) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1063 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1064 | Return a copy of the Decimal instance num. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1065 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1066 | .. method:: create_decimal(num) |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1067 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1068 | Creates a new Decimal instance from *num* but using *self* as |
| 1069 | context. Unlike the :class:`Decimal` constructor, the context precision, |
| 1070 | rounding method, flags, and traps are applied to the conversion. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1071 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1072 | This is useful because constants are often given to a greater precision |
| 1073 | than is needed by the application. Another benefit is that rounding |
| 1074 | immediately eliminates unintended effects from digits beyond the current |
| 1075 | precision. In the following example, using unrounded inputs means that |
| 1076 | adding zero to a sum can change the result: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1077 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1078 | .. doctest:: newcontext |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1079 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1080 | >>> getcontext().prec = 3 |
| 1081 | >>> Decimal('3.4445') + Decimal('1.0023') |
| 1082 | Decimal('4.45') |
| 1083 | >>> Decimal('3.4445') + Decimal(0) + Decimal('1.0023') |
| 1084 | Decimal('4.44') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1085 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1086 | This method implements the to-number operation of the IBM specification. |
| 1087 | If the argument is a string, no leading or trailing whitespace is |
| 1088 | permitted. |
| 1089 | |
Georg Brandl | aa5bb32 | 2009-01-03 19:44:48 +0000 | [diff] [blame] | 1090 | .. method:: create_decimal_from_float(f) |
Raymond Hettinger | f4d8597 | 2009-01-03 19:02:23 +0000 | [diff] [blame] | 1091 | |
| 1092 | Creates a new Decimal instance from a float *f* but rounding using *self* |
Georg Brandl | a24067e | 2009-01-03 20:15:14 +0000 | [diff] [blame] | 1093 | as the context. Unlike the :meth:`Decimal.from_float` class method, |
Raymond Hettinger | f4d8597 | 2009-01-03 19:02:23 +0000 | [diff] [blame] | 1094 | the context precision, rounding method, flags, and traps are applied to |
| 1095 | the conversion. |
| 1096 | |
| 1097 | .. doctest:: |
| 1098 | |
Georg Brandl | aa5bb32 | 2009-01-03 19:44:48 +0000 | [diff] [blame] | 1099 | >>> context = Context(prec=5, rounding=ROUND_DOWN) |
| 1100 | >>> context.create_decimal_from_float(math.pi) |
| 1101 | Decimal('3.1415') |
| 1102 | >>> context = Context(prec=5, traps=[Inexact]) |
| 1103 | >>> context.create_decimal_from_float(math.pi) |
| 1104 | Traceback (most recent call last): |
| 1105 | ... |
| 1106 | Inexact: None |
Raymond Hettinger | f4d8597 | 2009-01-03 19:02:23 +0000 | [diff] [blame] | 1107 | |
| 1108 | .. versionadded:: 2.7 |
| 1109 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1110 | .. method:: Etiny() |
| 1111 | |
| 1112 | Returns a value equal to ``Emin - prec + 1`` which is the minimum exponent |
| 1113 | value for subnormal results. When underflow occurs, the exponent is set |
| 1114 | to :const:`Etiny`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1115 | |
| 1116 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1117 | .. method:: Etop() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1118 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1119 | Returns a value equal to ``Emax - prec + 1``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1120 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1121 | The usual approach to working with decimals is to create :class:`Decimal` |
| 1122 | instances and then apply arithmetic operations which take place within the |
| 1123 | current context for the active thread. An alternative approach is to use |
| 1124 | context methods for calculating within a specific context. The methods are |
| 1125 | similar to those for the :class:`Decimal` class and are only briefly |
| 1126 | recounted here. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1127 | |
| 1128 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1129 | .. method:: abs(x) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1130 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1131 | Returns the absolute value of *x*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1132 | |
| 1133 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1134 | .. method:: add(x, y) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1135 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1136 | Return the sum of *x* and *y*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1137 | |
| 1138 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1139 | .. method:: canonical(x) |
| 1140 | |
| 1141 | Returns the same Decimal object *x*. |
| 1142 | |
| 1143 | |
| 1144 | .. method:: compare(x, y) |
| 1145 | |
| 1146 | Compares *x* and *y* numerically. |
| 1147 | |
| 1148 | |
| 1149 | .. method:: compare_signal(x, y) |
| 1150 | |
| 1151 | Compares the values of the two operands numerically. |
| 1152 | |
| 1153 | |
| 1154 | .. method:: compare_total(x, y) |
| 1155 | |
| 1156 | Compares two operands using their abstract representation. |
| 1157 | |
| 1158 | |
| 1159 | .. method:: compare_total_mag(x, y) |
| 1160 | |
| 1161 | Compares two operands using their abstract representation, ignoring sign. |
| 1162 | |
| 1163 | |
| 1164 | .. method:: copy_abs(x) |
| 1165 | |
| 1166 | Returns a copy of *x* with the sign set to 0. |
| 1167 | |
| 1168 | |
| 1169 | .. method:: copy_negate(x) |
| 1170 | |
| 1171 | Returns a copy of *x* with the sign inverted. |
| 1172 | |
| 1173 | |
| 1174 | .. method:: copy_sign(x, y) |
| 1175 | |
| 1176 | Copies the sign from *y* to *x*. |
| 1177 | |
| 1178 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1179 | .. method:: divide(x, y) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1180 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1181 | Return *x* divided by *y*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1182 | |
| 1183 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1184 | .. method:: divide_int(x, y) |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 1185 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1186 | Return *x* divided by *y*, truncated to an integer. |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 1187 | |
| 1188 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1189 | .. method:: divmod(x, y) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1190 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1191 | Divides two numbers and returns the integer part of the result. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1192 | |
| 1193 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1194 | .. method:: exp(x) |
| 1195 | |
| 1196 | Returns `e ** x`. |
| 1197 | |
| 1198 | |
| 1199 | .. method:: fma(x, y, z) |
| 1200 | |
| 1201 | Returns *x* multiplied by *y*, plus *z*. |
| 1202 | |
| 1203 | |
| 1204 | .. method:: is_canonical(x) |
| 1205 | |
| 1206 | Returns True if *x* is canonical; otherwise returns False. |
| 1207 | |
| 1208 | |
| 1209 | .. method:: is_finite(x) |
| 1210 | |
| 1211 | Returns True if *x* is finite; otherwise returns False. |
| 1212 | |
| 1213 | |
| 1214 | .. method:: is_infinite(x) |
| 1215 | |
| 1216 | Returns True if *x* is infinite; otherwise returns False. |
| 1217 | |
| 1218 | |
| 1219 | .. method:: is_nan(x) |
| 1220 | |
| 1221 | Returns True if *x* is a qNaN or sNaN; otherwise returns False. |
| 1222 | |
| 1223 | |
| 1224 | .. method:: is_normal(x) |
| 1225 | |
| 1226 | Returns True if *x* is a normal number; otherwise returns False. |
| 1227 | |
| 1228 | |
| 1229 | .. method:: is_qnan(x) |
| 1230 | |
| 1231 | Returns True if *x* is a quiet NaN; otherwise returns False. |
| 1232 | |
| 1233 | |
| 1234 | .. method:: is_signed(x) |
| 1235 | |
| 1236 | Returns True if *x* is negative; otherwise returns False. |
| 1237 | |
| 1238 | |
| 1239 | .. method:: is_snan(x) |
| 1240 | |
| 1241 | Returns True if *x* is a signaling NaN; otherwise returns False. |
| 1242 | |
| 1243 | |
| 1244 | .. method:: is_subnormal(x) |
| 1245 | |
| 1246 | Returns True if *x* is subnormal; otherwise returns False. |
| 1247 | |
| 1248 | |
| 1249 | .. method:: is_zero(x) |
| 1250 | |
| 1251 | Returns True if *x* is a zero; otherwise returns False. |
| 1252 | |
| 1253 | |
| 1254 | .. method:: ln(x) |
| 1255 | |
| 1256 | Returns the natural (base e) logarithm of *x*. |
| 1257 | |
| 1258 | |
| 1259 | .. method:: log10(x) |
| 1260 | |
| 1261 | Returns the base 10 logarithm of *x*. |
| 1262 | |
| 1263 | |
| 1264 | .. method:: logb(x) |
| 1265 | |
| 1266 | Returns the exponent of the magnitude of the operand's MSD. |
| 1267 | |
| 1268 | |
| 1269 | .. method:: logical_and(x, y) |
| 1270 | |
Georg Brandl | e92818f | 2009-01-03 20:47:01 +0000 | [diff] [blame] | 1271 | Applies the logical operation *and* between each operand's digits. |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1272 | |
| 1273 | |
| 1274 | .. method:: logical_invert(x) |
| 1275 | |
| 1276 | Invert all the digits in *x*. |
| 1277 | |
| 1278 | |
| 1279 | .. method:: logical_or(x, y) |
| 1280 | |
Georg Brandl | e92818f | 2009-01-03 20:47:01 +0000 | [diff] [blame] | 1281 | Applies the logical operation *or* between each operand's digits. |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1282 | |
| 1283 | |
| 1284 | .. method:: logical_xor(x, y) |
| 1285 | |
Georg Brandl | e92818f | 2009-01-03 20:47:01 +0000 | [diff] [blame] | 1286 | Applies the logical operation *xor* between each operand's digits. |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1287 | |
| 1288 | |
| 1289 | .. method:: max(x, y) |
| 1290 | |
| 1291 | Compares two values numerically and returns the maximum. |
| 1292 | |
| 1293 | |
| 1294 | .. method:: max_mag(x, y) |
| 1295 | |
| 1296 | Compares the values numerically with their sign ignored. |
| 1297 | |
| 1298 | |
| 1299 | .. method:: min(x, y) |
| 1300 | |
| 1301 | Compares two values numerically and returns the minimum. |
| 1302 | |
| 1303 | |
| 1304 | .. method:: min_mag(x, y) |
| 1305 | |
| 1306 | Compares the values numerically with their sign ignored. |
| 1307 | |
| 1308 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1309 | .. method:: minus(x) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1310 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1311 | Minus corresponds to the unary prefix minus operator in Python. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1312 | |
| 1313 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1314 | .. method:: multiply(x, y) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1315 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1316 | Return the product of *x* and *y*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1317 | |
| 1318 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1319 | .. method:: next_minus(x) |
| 1320 | |
| 1321 | Returns the largest representable number smaller than *x*. |
| 1322 | |
| 1323 | |
| 1324 | .. method:: next_plus(x) |
| 1325 | |
| 1326 | Returns the smallest representable number larger than *x*. |
| 1327 | |
| 1328 | |
| 1329 | .. method:: next_toward(x, y) |
| 1330 | |
| 1331 | Returns the number closest to *x*, in direction towards *y*. |
| 1332 | |
| 1333 | |
| 1334 | .. method:: normalize(x) |
| 1335 | |
| 1336 | Reduces *x* to its simplest form. |
| 1337 | |
| 1338 | |
| 1339 | .. method:: number_class(x) |
| 1340 | |
| 1341 | Returns an indication of the class of *x*. |
| 1342 | |
| 1343 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1344 | .. method:: plus(x) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1345 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1346 | Plus corresponds to the unary prefix plus operator in Python. This |
| 1347 | operation applies the context precision and rounding, so it is *not* an |
| 1348 | identity operation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1349 | |
| 1350 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1351 | .. method:: power(x, y[, modulo]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1352 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1353 | Return ``x`` to the power of ``y``, reduced modulo ``modulo`` if given. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1354 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1355 | With two arguments, compute ``x**y``. If ``x`` is negative then ``y`` |
| 1356 | must be integral. The result will be inexact unless ``y`` is integral and |
| 1357 | the result is finite and can be expressed exactly in 'precision' digits. |
| 1358 | The result should always be correctly rounded, using the rounding mode of |
| 1359 | the current thread's context. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1360 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1361 | With three arguments, compute ``(x**y) % modulo``. For the three argument |
| 1362 | form, the following restrictions on the arguments hold: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1363 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1364 | - all three arguments must be integral |
| 1365 | - ``y`` must be nonnegative |
| 1366 | - at least one of ``x`` or ``y`` must be nonzero |
| 1367 | - ``modulo`` must be nonzero and have at most 'precision' digits |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1368 | |
Mark Dickinson | f5be4e6 | 2010-02-22 15:40:28 +0000 | [diff] [blame] | 1369 | The value resulting from ``Context.power(x, y, modulo)`` is |
| 1370 | equal to the value that would be obtained by computing ``(x**y) |
| 1371 | % modulo`` with unbounded precision, but is computed more |
| 1372 | efficiently. The exponent of the result is zero, regardless of |
| 1373 | the exponents of ``x``, ``y`` and ``modulo``. The result is |
| 1374 | always exact. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1375 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1376 | .. versionchanged:: 2.6 |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1377 | ``y`` may now be nonintegral in ``x**y``. |
| 1378 | Stricter requirements for the three-argument version. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1379 | |
| 1380 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1381 | .. method:: quantize(x, y) |
| 1382 | |
| 1383 | Returns a value equal to *x* (rounded), having the exponent of *y*. |
| 1384 | |
| 1385 | |
| 1386 | .. method:: radix() |
| 1387 | |
| 1388 | Just returns 10, as this is Decimal, :) |
| 1389 | |
| 1390 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1391 | .. method:: remainder(x, y) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1392 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1393 | Returns the remainder from integer division. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1394 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1395 | The sign of the result, if non-zero, is the same as that of the original |
| 1396 | dividend. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1397 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1398 | .. method:: remainder_near(x, y) |
| 1399 | |
Georg Brandl | e92818f | 2009-01-03 20:47:01 +0000 | [diff] [blame] | 1400 | Returns ``x - y * n``, where *n* is the integer nearest the exact value |
| 1401 | of ``x / y`` (if the result is 0 then its sign will be the sign of *x*). |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1402 | |
| 1403 | |
| 1404 | .. method:: rotate(x, y) |
| 1405 | |
| 1406 | Returns a rotated copy of *x*, *y* times. |
| 1407 | |
| 1408 | |
| 1409 | .. method:: same_quantum(x, y) |
| 1410 | |
| 1411 | Returns True if the two operands have the same exponent. |
| 1412 | |
| 1413 | |
| 1414 | .. method:: scaleb (x, y) |
| 1415 | |
| 1416 | Returns the first operand after adding the second value its exp. |
| 1417 | |
| 1418 | |
| 1419 | .. method:: shift(x, y) |
| 1420 | |
| 1421 | Returns a shifted copy of *x*, *y* times. |
| 1422 | |
| 1423 | |
| 1424 | .. method:: sqrt(x) |
| 1425 | |
| 1426 | Square root of a non-negative number to context precision. |
| 1427 | |
| 1428 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1429 | .. method:: subtract(x, y) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1430 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1431 | Return the difference between *x* and *y*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1432 | |
Facundo Batista | 8e1c52a | 2008-06-21 17:30:06 +0000 | [diff] [blame] | 1433 | |
| 1434 | .. method:: to_eng_string(x) |
| 1435 | |
| 1436 | Converts a number to a string, using scientific notation. |
| 1437 | |
| 1438 | |
| 1439 | .. method:: to_integral_exact(x) |
| 1440 | |
| 1441 | Rounds to an integer. |
| 1442 | |
| 1443 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1444 | .. method:: to_sci_string(x) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1445 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1446 | Converts a number to a string using scientific notation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1447 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1448 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1449 | |
| 1450 | |
| 1451 | .. _decimal-signals: |
| 1452 | |
| 1453 | Signals |
| 1454 | ------- |
| 1455 | |
| 1456 | Signals represent conditions that arise during computation. Each corresponds to |
| 1457 | one context flag and one context trap enabler. |
| 1458 | |
Mark Dickinson | 1840c1a | 2008-05-03 18:23:14 +0000 | [diff] [blame] | 1459 | The context flag is set whenever the condition is encountered. After the |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1460 | computation, flags may be checked for informational purposes (for instance, to |
| 1461 | determine whether a computation was exact). After checking the flags, be sure to |
| 1462 | clear all flags before starting the next computation. |
| 1463 | |
| 1464 | If the context's trap enabler is set for the signal, then the condition causes a |
| 1465 | Python exception to be raised. For example, if the :class:`DivisionByZero` trap |
| 1466 | is set, then a :exc:`DivisionByZero` exception is raised upon encountering the |
| 1467 | condition. |
| 1468 | |
| 1469 | |
| 1470 | .. class:: Clamped |
| 1471 | |
| 1472 | Altered an exponent to fit representation constraints. |
| 1473 | |
| 1474 | Typically, clamping occurs when an exponent falls outside the context's |
| 1475 | :attr:`Emin` and :attr:`Emax` limits. If possible, the exponent is reduced to |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 1476 | fit by adding zeros to the coefficient. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1477 | |
| 1478 | |
| 1479 | .. class:: DecimalException |
| 1480 | |
| 1481 | Base class for other signals and a subclass of :exc:`ArithmeticError`. |
| 1482 | |
| 1483 | |
| 1484 | .. class:: DivisionByZero |
| 1485 | |
| 1486 | Signals the division of a non-infinite number by zero. |
| 1487 | |
| 1488 | Can occur with division, modulo division, or when raising a number to a negative |
| 1489 | power. If this signal is not trapped, returns :const:`Infinity` or |
| 1490 | :const:`-Infinity` with the sign determined by the inputs to the calculation. |
| 1491 | |
| 1492 | |
| 1493 | .. class:: Inexact |
| 1494 | |
| 1495 | Indicates that rounding occurred and the result is not exact. |
| 1496 | |
| 1497 | Signals when non-zero digits were discarded during rounding. The rounded result |
| 1498 | is returned. The signal flag or trap is used to detect when results are |
| 1499 | inexact. |
| 1500 | |
| 1501 | |
| 1502 | .. class:: InvalidOperation |
| 1503 | |
| 1504 | An invalid operation was performed. |
| 1505 | |
| 1506 | Indicates that an operation was requested that does not make sense. If not |
| 1507 | trapped, returns :const:`NaN`. Possible causes include:: |
| 1508 | |
| 1509 | Infinity - Infinity |
| 1510 | 0 * Infinity |
| 1511 | Infinity / Infinity |
| 1512 | x % 0 |
| 1513 | Infinity % x |
| 1514 | x._rescale( non-integer ) |
| 1515 | sqrt(-x) and x > 0 |
| 1516 | 0 ** 0 |
| 1517 | x ** (non-integer) |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1518 | x ** Infinity |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1519 | |
| 1520 | |
| 1521 | .. class:: Overflow |
| 1522 | |
| 1523 | Numerical overflow. |
| 1524 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1525 | Indicates the exponent is larger than :attr:`Emax` after rounding has |
| 1526 | occurred. If not trapped, the result depends on the rounding mode, either |
| 1527 | pulling inward to the largest representable finite number or rounding outward |
| 1528 | to :const:`Infinity`. In either case, :class:`Inexact` and :class:`Rounded` |
| 1529 | are also signaled. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1530 | |
| 1531 | |
| 1532 | .. class:: Rounded |
| 1533 | |
| 1534 | Rounding occurred though possibly no information was lost. |
| 1535 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1536 | Signaled whenever rounding discards digits; even if those digits are zero |
| 1537 | (such as rounding :const:`5.00` to :const:`5.0`). If not trapped, returns |
| 1538 | the result unchanged. This signal is used to detect loss of significant |
| 1539 | digits. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1540 | |
| 1541 | |
| 1542 | .. class:: Subnormal |
| 1543 | |
| 1544 | Exponent was lower than :attr:`Emin` prior to rounding. |
| 1545 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1546 | Occurs when an operation result is subnormal (the exponent is too small). If |
| 1547 | not trapped, returns the result unchanged. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1548 | |
| 1549 | |
| 1550 | .. class:: Underflow |
| 1551 | |
| 1552 | Numerical underflow with result rounded to zero. |
| 1553 | |
| 1554 | Occurs when a subnormal result is pushed to zero by rounding. :class:`Inexact` |
| 1555 | and :class:`Subnormal` are also signaled. |
| 1556 | |
| 1557 | The following table summarizes the hierarchy of signals:: |
| 1558 | |
| 1559 | exceptions.ArithmeticError(exceptions.StandardError) |
| 1560 | DecimalException |
| 1561 | Clamped |
| 1562 | DivisionByZero(DecimalException, exceptions.ZeroDivisionError) |
| 1563 | Inexact |
| 1564 | Overflow(Inexact, Rounded) |
| 1565 | Underflow(Inexact, Rounded, Subnormal) |
| 1566 | InvalidOperation |
| 1567 | Rounded |
| 1568 | Subnormal |
| 1569 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1570 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1571 | |
| 1572 | |
| 1573 | .. _decimal-notes: |
| 1574 | |
| 1575 | Floating Point Notes |
| 1576 | -------------------- |
| 1577 | |
| 1578 | |
| 1579 | Mitigating round-off error with increased precision |
| 1580 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 1581 | |
| 1582 | The use of decimal floating point eliminates decimal representation error |
| 1583 | (making it possible to represent :const:`0.1` exactly); however, some operations |
| 1584 | can still incur round-off error when non-zero digits exceed the fixed precision. |
| 1585 | |
| 1586 | The effects of round-off error can be amplified by the addition or subtraction |
| 1587 | of nearly offsetting quantities resulting in loss of significance. Knuth |
| 1588 | provides two instructive examples where rounded floating point arithmetic with |
| 1589 | insufficient precision causes the breakdown of the associative and distributive |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1590 | properties of addition: |
| 1591 | |
| 1592 | .. doctest:: newcontext |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1593 | |
| 1594 | # Examples from Seminumerical Algorithms, Section 4.2.2. |
| 1595 | >>> from decimal import Decimal, getcontext |
| 1596 | >>> getcontext().prec = 8 |
| 1597 | |
| 1598 | >>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111') |
| 1599 | >>> (u + v) + w |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1600 | Decimal('9.5111111') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1601 | >>> u + (v + w) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1602 | Decimal('10') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1603 | |
| 1604 | >>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003') |
| 1605 | >>> (u*v) + (u*w) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1606 | Decimal('0.01') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1607 | >>> u * (v+w) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1608 | Decimal('0.0060000') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1609 | |
| 1610 | The :mod:`decimal` module makes it possible to restore the identities by |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1611 | expanding the precision sufficiently to avoid loss of significance: |
| 1612 | |
| 1613 | .. doctest:: newcontext |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1614 | |
| 1615 | >>> getcontext().prec = 20 |
| 1616 | >>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111') |
| 1617 | >>> (u + v) + w |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1618 | Decimal('9.51111111') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1619 | >>> u + (v + w) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1620 | Decimal('9.51111111') |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1621 | >>> |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1622 | >>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003') |
| 1623 | >>> (u*v) + (u*w) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1624 | Decimal('0.0060000') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1625 | >>> u * (v+w) |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1626 | Decimal('0.0060000') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1627 | |
| 1628 | |
| 1629 | Special values |
| 1630 | ^^^^^^^^^^^^^^ |
| 1631 | |
| 1632 | The number system for the :mod:`decimal` module provides special values |
| 1633 | including :const:`NaN`, :const:`sNaN`, :const:`-Infinity`, :const:`Infinity`, |
Facundo Batista | 7c82a3e9 | 2007-09-14 18:58:34 +0000 | [diff] [blame] | 1634 | and two zeros, :const:`+0` and :const:`-0`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1635 | |
| 1636 | Infinities can be constructed directly with: ``Decimal('Infinity')``. Also, |
| 1637 | they can arise from dividing by zero when the :exc:`DivisionByZero` signal is |
| 1638 | not trapped. Likewise, when the :exc:`Overflow` signal is not trapped, infinity |
| 1639 | can result from rounding beyond the limits of the largest representable number. |
| 1640 | |
| 1641 | The infinities are signed (affine) and can be used in arithmetic operations |
| 1642 | where they get treated as very large, indeterminate numbers. For instance, |
| 1643 | adding a constant to infinity gives another infinite result. |
| 1644 | |
| 1645 | Some operations are indeterminate and return :const:`NaN`, or if the |
| 1646 | :exc:`InvalidOperation` signal is trapped, raise an exception. For example, |
| 1647 | ``0/0`` returns :const:`NaN` which means "not a number". This variety of |
| 1648 | :const:`NaN` is quiet and, once created, will flow through other computations |
| 1649 | always resulting in another :const:`NaN`. This behavior can be useful for a |
| 1650 | series of computations that occasionally have missing inputs --- it allows the |
| 1651 | calculation to proceed while flagging specific results as invalid. |
| 1652 | |
| 1653 | A variant is :const:`sNaN` which signals rather than remaining quiet after every |
| 1654 | operation. This is a useful return value when an invalid result needs to |
| 1655 | interrupt a calculation for special handling. |
| 1656 | |
Mark Dickinson | 2fc9263 | 2008-02-06 22:10:50 +0000 | [diff] [blame] | 1657 | The behavior of Python's comparison operators can be a little surprising where a |
| 1658 | :const:`NaN` is involved. A test for equality where one of the operands is a |
| 1659 | quiet or signaling :const:`NaN` always returns :const:`False` (even when doing |
| 1660 | ``Decimal('NaN')==Decimal('NaN')``), while a test for inequality always returns |
Mark Dickinson | bafa942 | 2008-02-06 22:25:16 +0000 | [diff] [blame] | 1661 | :const:`True`. An attempt to compare two Decimals using any of the ``<``, |
Mark Dickinson | 00c2e65 | 2008-02-07 01:42:06 +0000 | [diff] [blame] | 1662 | ``<=``, ``>`` or ``>=`` operators will raise the :exc:`InvalidOperation` signal |
| 1663 | if either operand is a :const:`NaN`, and return :const:`False` if this signal is |
Mark Dickinson | 3a94ee0 | 2008-02-10 15:19:58 +0000 | [diff] [blame] | 1664 | not trapped. Note that the General Decimal Arithmetic specification does not |
Mark Dickinson | 00c2e65 | 2008-02-07 01:42:06 +0000 | [diff] [blame] | 1665 | specify the behavior of direct comparisons; these rules for comparisons |
| 1666 | involving a :const:`NaN` were taken from the IEEE 854 standard (see Table 3 in |
| 1667 | section 5.7). To ensure strict standards-compliance, use the :meth:`compare` |
Mark Dickinson | 2fc9263 | 2008-02-06 22:10:50 +0000 | [diff] [blame] | 1668 | and :meth:`compare-signal` methods instead. |
| 1669 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1670 | The signed zeros can result from calculations that underflow. They keep the sign |
| 1671 | that would have resulted if the calculation had been carried out to greater |
| 1672 | precision. Since their magnitude is zero, both positive and negative zeros are |
| 1673 | treated as equal and their sign is informational. |
| 1674 | |
| 1675 | In addition to the two signed zeros which are distinct yet equal, there are |
| 1676 | various representations of zero with differing precisions yet equivalent in |
| 1677 | value. This takes a bit of getting used to. For an eye accustomed to |
| 1678 | normalized floating point representations, it is not immediately obvious that |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1679 | the following calculation returns a value equal to zero: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1680 | |
| 1681 | >>> 1 / Decimal('Infinity') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1682 | Decimal('0E-1000000026') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1683 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1684 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1685 | |
| 1686 | |
| 1687 | .. _decimal-threads: |
| 1688 | |
| 1689 | Working with threads |
| 1690 | -------------------- |
| 1691 | |
| 1692 | The :func:`getcontext` function accesses a different :class:`Context` object for |
| 1693 | each thread. Having separate thread contexts means that threads may make |
| 1694 | changes (such as ``getcontext.prec=10``) without interfering with other threads. |
| 1695 | |
| 1696 | Likewise, the :func:`setcontext` function automatically assigns its target to |
| 1697 | the current thread. |
| 1698 | |
| 1699 | If :func:`setcontext` has not been called before :func:`getcontext`, then |
| 1700 | :func:`getcontext` will automatically create a new context for use in the |
| 1701 | current thread. |
| 1702 | |
| 1703 | The new context is copied from a prototype context called *DefaultContext*. To |
| 1704 | control the defaults so that each thread will use the same values throughout the |
| 1705 | application, directly modify the *DefaultContext* object. This should be done |
| 1706 | *before* any threads are started so that there won't be a race condition between |
| 1707 | threads calling :func:`getcontext`. For example:: |
| 1708 | |
| 1709 | # Set applicationwide defaults for all threads about to be launched |
| 1710 | DefaultContext.prec = 12 |
| 1711 | DefaultContext.rounding = ROUND_DOWN |
| 1712 | DefaultContext.traps = ExtendedContext.traps.copy() |
| 1713 | DefaultContext.traps[InvalidOperation] = 1 |
| 1714 | setcontext(DefaultContext) |
| 1715 | |
| 1716 | # Afterwards, the threads can be started |
| 1717 | t1.start() |
| 1718 | t2.start() |
| 1719 | t3.start() |
| 1720 | . . . |
| 1721 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1722 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1723 | |
| 1724 | |
| 1725 | .. _decimal-recipes: |
| 1726 | |
| 1727 | Recipes |
| 1728 | ------- |
| 1729 | |
| 1730 | Here are a few recipes that serve as utility functions and that demonstrate ways |
| 1731 | to work with the :class:`Decimal` class:: |
| 1732 | |
| 1733 | def moneyfmt(value, places=2, curr='', sep=',', dp='.', |
| 1734 | pos='', neg='-', trailneg=''): |
| 1735 | """Convert Decimal to a money formatted string. |
| 1736 | |
| 1737 | places: required number of places after the decimal point |
| 1738 | curr: optional currency symbol before the sign (may be blank) |
| 1739 | sep: optional grouping separator (comma, period, space, or blank) |
| 1740 | dp: decimal point indicator (comma or period) |
| 1741 | only specify as blank when places is zero |
| 1742 | pos: optional sign for positive numbers: '+', space or blank |
| 1743 | neg: optional sign for negative numbers: '-', '(', space or blank |
| 1744 | trailneg:optional trailing minus indicator: '-', ')', space or blank |
| 1745 | |
| 1746 | >>> d = Decimal('-1234567.8901') |
| 1747 | >>> moneyfmt(d, curr='$') |
| 1748 | '-$1,234,567.89' |
| 1749 | >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-') |
| 1750 | '1.234.568-' |
| 1751 | >>> moneyfmt(d, curr='$', neg='(', trailneg=')') |
| 1752 | '($1,234,567.89)' |
| 1753 | >>> moneyfmt(Decimal(123456789), sep=' ') |
| 1754 | '123 456 789.00' |
| 1755 | >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>') |
Raymond Hettinger | 5eaffc4 | 2008-04-17 10:48:31 +0000 | [diff] [blame] | 1756 | '<0.02>' |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1757 | |
| 1758 | """ |
Raymond Hettinger | 0cd7170 | 2008-02-14 12:49:37 +0000 | [diff] [blame] | 1759 | q = Decimal(10) ** -places # 2 places --> '0.01' |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1760 | sign, digits, exp = value.quantize(q).as_tuple() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1761 | result = [] |
| 1762 | digits = map(str, digits) |
| 1763 | build, next = result.append, digits.pop |
| 1764 | if sign: |
| 1765 | build(trailneg) |
| 1766 | for i in range(places): |
Raymond Hettinger | 0cd7170 | 2008-02-14 12:49:37 +0000 | [diff] [blame] | 1767 | build(next() if digits else '0') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1768 | build(dp) |
Raymond Hettinger | 5eaffc4 | 2008-04-17 10:48:31 +0000 | [diff] [blame] | 1769 | if not digits: |
| 1770 | build('0') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1771 | i = 0 |
| 1772 | while digits: |
| 1773 | build(next()) |
| 1774 | i += 1 |
| 1775 | if i == 3 and digits: |
| 1776 | i = 0 |
| 1777 | build(sep) |
| 1778 | build(curr) |
Raymond Hettinger | 0cd7170 | 2008-02-14 12:49:37 +0000 | [diff] [blame] | 1779 | build(neg if sign else pos) |
| 1780 | return ''.join(reversed(result)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1781 | |
| 1782 | def pi(): |
| 1783 | """Compute Pi to the current precision. |
| 1784 | |
| 1785 | >>> print pi() |
| 1786 | 3.141592653589793238462643383 |
| 1787 | |
| 1788 | """ |
| 1789 | getcontext().prec += 2 # extra digits for intermediate steps |
| 1790 | three = Decimal(3) # substitute "three=3.0" for regular floats |
| 1791 | lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 |
| 1792 | while s != lasts: |
| 1793 | lasts = s |
| 1794 | n, na = n+na, na+8 |
| 1795 | d, da = d+da, da+32 |
| 1796 | t = (t * n) / d |
| 1797 | s += t |
| 1798 | getcontext().prec -= 2 |
| 1799 | return +s # unary plus applies the new precision |
| 1800 | |
| 1801 | def exp(x): |
| 1802 | """Return e raised to the power of x. Result type matches input type. |
| 1803 | |
| 1804 | >>> print exp(Decimal(1)) |
| 1805 | 2.718281828459045235360287471 |
| 1806 | >>> print exp(Decimal(2)) |
| 1807 | 7.389056098930650227230427461 |
| 1808 | >>> print exp(2.0) |
| 1809 | 7.38905609893 |
| 1810 | >>> print exp(2+0j) |
| 1811 | (7.38905609893+0j) |
| 1812 | |
| 1813 | """ |
| 1814 | getcontext().prec += 2 |
| 1815 | i, lasts, s, fact, num = 0, 0, 1, 1, 1 |
| 1816 | while s != lasts: |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1817 | lasts = s |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1818 | i += 1 |
| 1819 | fact *= i |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1820 | num *= x |
| 1821 | s += num / fact |
| 1822 | getcontext().prec -= 2 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1823 | return +s |
| 1824 | |
| 1825 | def cos(x): |
| 1826 | """Return the cosine of x as measured in radians. |
| 1827 | |
| 1828 | >>> print cos(Decimal('0.5')) |
| 1829 | 0.8775825618903727161162815826 |
| 1830 | >>> print cos(0.5) |
| 1831 | 0.87758256189 |
| 1832 | >>> print cos(0.5+0j) |
| 1833 | (0.87758256189+0j) |
| 1834 | |
| 1835 | """ |
| 1836 | getcontext().prec += 2 |
| 1837 | i, lasts, s, fact, num, sign = 0, 0, 1, 1, 1, 1 |
| 1838 | while s != lasts: |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1839 | lasts = s |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1840 | i += 2 |
| 1841 | fact *= i * (i-1) |
| 1842 | num *= x * x |
| 1843 | sign *= -1 |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1844 | s += num / fact * sign |
| 1845 | getcontext().prec -= 2 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1846 | return +s |
| 1847 | |
| 1848 | def sin(x): |
| 1849 | """Return the sine of x as measured in radians. |
| 1850 | |
| 1851 | >>> print sin(Decimal('0.5')) |
| 1852 | 0.4794255386042030002732879352 |
| 1853 | >>> print sin(0.5) |
| 1854 | 0.479425538604 |
| 1855 | >>> print sin(0.5+0j) |
| 1856 | (0.479425538604+0j) |
| 1857 | |
| 1858 | """ |
| 1859 | getcontext().prec += 2 |
| 1860 | i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1 |
| 1861 | while s != lasts: |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1862 | lasts = s |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1863 | i += 2 |
| 1864 | fact *= i * (i-1) |
| 1865 | num *= x * x |
| 1866 | sign *= -1 |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1867 | s += num / fact * sign |
| 1868 | getcontext().prec -= 2 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1869 | return +s |
| 1870 | |
| 1871 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1872 | .. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1873 | |
| 1874 | |
| 1875 | .. _decimal-faq: |
| 1876 | |
| 1877 | Decimal FAQ |
| 1878 | ----------- |
| 1879 | |
| 1880 | Q. It is cumbersome to type ``decimal.Decimal('1234.5')``. Is there a way to |
| 1881 | minimize typing when using the interactive interpreter? |
| 1882 | |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1883 | A. Some users abbreviate the constructor to just a single letter: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1884 | |
| 1885 | >>> D = decimal.Decimal |
| 1886 | >>> D('1.23') + D('3.45') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1887 | Decimal('4.68') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1888 | |
| 1889 | Q. In a fixed-point application with two decimal places, some inputs have many |
| 1890 | places and need to be rounded. Others are not supposed to have excess digits |
| 1891 | and need to be validated. What methods should be used? |
| 1892 | |
| 1893 | A. The :meth:`quantize` method rounds to a fixed number of decimal places. If |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1894 | the :const:`Inexact` trap is set, it is also useful for validation: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1895 | |
| 1896 | >>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01') |
| 1897 | |
| 1898 | >>> # Round to two places |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1899 | >>> Decimal('3.214').quantize(TWOPLACES) |
| 1900 | Decimal('3.21') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1901 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1902 | >>> # Validate that a number does not exceed two places |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1903 | >>> Decimal('3.21').quantize(TWOPLACES, context=Context(traps=[Inexact])) |
| 1904 | Decimal('3.21') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1905 | |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1906 | >>> Decimal('3.214').quantize(TWOPLACES, context=Context(traps=[Inexact])) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1907 | Traceback (most recent call last): |
| 1908 | ... |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 1909 | Inexact: None |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1910 | |
| 1911 | Q. Once I have valid two place inputs, how do I maintain that invariant |
| 1912 | throughout an application? |
| 1913 | |
Raymond Hettinger | 4631481 | 2008-02-14 10:46:57 +0000 | [diff] [blame] | 1914 | A. Some operations like addition, subtraction, and multiplication by an integer |
| 1915 | will automatically preserve fixed point. Others operations, like division and |
| 1916 | non-integer multiplication, will change the number of decimal places and need to |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1917 | be followed-up with a :meth:`quantize` step: |
Raymond Hettinger | 4631481 | 2008-02-14 10:46:57 +0000 | [diff] [blame] | 1918 | |
| 1919 | >>> a = Decimal('102.72') # Initial fixed-point values |
| 1920 | >>> b = Decimal('3.17') |
| 1921 | >>> a + b # Addition preserves fixed-point |
| 1922 | Decimal('105.89') |
| 1923 | >>> a - b |
| 1924 | Decimal('99.55') |
| 1925 | >>> a * 42 # So does integer multiplication |
| 1926 | Decimal('4314.24') |
| 1927 | >>> (a * b).quantize(TWOPLACES) # Must quantize non-integer multiplication |
| 1928 | Decimal('325.62') |
Raymond Hettinger | 27a90d9 | 2008-02-14 11:01:10 +0000 | [diff] [blame] | 1929 | >>> (b / a).quantize(TWOPLACES) # And quantize division |
Raymond Hettinger | 4631481 | 2008-02-14 10:46:57 +0000 | [diff] [blame] | 1930 | Decimal('0.03') |
| 1931 | |
| 1932 | In developing fixed-point applications, it is convenient to define functions |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1933 | to handle the :meth:`quantize` step: |
Raymond Hettinger | 4631481 | 2008-02-14 10:46:57 +0000 | [diff] [blame] | 1934 | |
Raymond Hettinger | 27a90d9 | 2008-02-14 11:01:10 +0000 | [diff] [blame] | 1935 | >>> def mul(x, y, fp=TWOPLACES): |
| 1936 | ... return (x * y).quantize(fp) |
| 1937 | >>> def div(x, y, fp=TWOPLACES): |
| 1938 | ... return (x / y).quantize(fp) |
Raymond Hettinger | d68bf02 | 2008-02-14 11:57:25 +0000 | [diff] [blame] | 1939 | |
Raymond Hettinger | 4631481 | 2008-02-14 10:46:57 +0000 | [diff] [blame] | 1940 | >>> mul(a, b) # Automatically preserve fixed-point |
| 1941 | Decimal('325.62') |
| 1942 | >>> div(b, a) |
| 1943 | Decimal('0.03') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1944 | |
| 1945 | Q. There are many ways to express the same value. The numbers :const:`200`, |
| 1946 | :const:`200.000`, :const:`2E2`, and :const:`.02E+4` all have the same value at |
| 1947 | various precisions. Is there a way to transform them to a single recognizable |
| 1948 | canonical value? |
| 1949 | |
| 1950 | A. The :meth:`normalize` method maps all equivalent values to a single |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1951 | representative: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1952 | |
| 1953 | >>> values = map(Decimal, '200 200.000 2E2 .02E+4'.split()) |
| 1954 | >>> [v.normalize() for v in values] |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 1955 | [Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1956 | |
| 1957 | Q. Some decimal values always print with exponential notation. Is there a way |
| 1958 | to get a non-exponential representation? |
| 1959 | |
| 1960 | A. For some values, exponential notation is the only way to express the number |
| 1961 | of significant places in the coefficient. For example, expressing |
| 1962 | :const:`5.0E+3` as :const:`5000` keeps the value constant but cannot show the |
| 1963 | original's two-place significance. |
| 1964 | |
Raymond Hettinger | d68bf02 | 2008-02-14 11:57:25 +0000 | [diff] [blame] | 1965 | If an application does not care about tracking significance, it is easy to |
Raymond Hettinger | ced6b1d | 2009-03-10 08:16:05 +0000 | [diff] [blame] | 1966 | remove the exponent and trailing zeros, losing significance, but keeping the |
| 1967 | value unchanged:: |
Raymond Hettinger | d68bf02 | 2008-02-14 11:57:25 +0000 | [diff] [blame] | 1968 | |
Raymond Hettinger | ced6b1d | 2009-03-10 08:16:05 +0000 | [diff] [blame] | 1969 | def remove_exponent(d): |
| 1970 | '''Remove exponent and trailing zeros. |
Raymond Hettinger | d68bf02 | 2008-02-14 11:57:25 +0000 | [diff] [blame] | 1971 | |
Raymond Hettinger | ced6b1d | 2009-03-10 08:16:05 +0000 | [diff] [blame] | 1972 | >>> remove_exponent(Decimal('5E+3')) |
| 1973 | Decimal('5000') |
Raymond Hettinger | d68bf02 | 2008-02-14 11:57:25 +0000 | [diff] [blame] | 1974 | |
Raymond Hettinger | ced6b1d | 2009-03-10 08:16:05 +0000 | [diff] [blame] | 1975 | ''' |
| 1976 | return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1977 | |
Raymond Hettinger | ed171ab | 2010-04-02 18:39:24 +0000 | [diff] [blame] | 1978 | Q. Is there a way to convert a regular float to a :class:`Decimal`? |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 1979 | |
Mark Dickinson | b1affc5 | 2010-04-04 22:09:21 +0000 | [diff] [blame] | 1980 | A. Yes, any binary floating point number can be exactly expressed as a |
Raymond Hettinger | ed171ab | 2010-04-02 18:39:24 +0000 | [diff] [blame] | 1981 | Decimal though an exact conversion may take more precision than intuition would |
| 1982 | suggest: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1983 | |
Raymond Hettinger | ed171ab | 2010-04-02 18:39:24 +0000 | [diff] [blame] | 1984 | .. doctest:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1985 | |
Raymond Hettinger | ed171ab | 2010-04-02 18:39:24 +0000 | [diff] [blame] | 1986 | >>> Decimal(math.pi) |
| 1987 | Decimal('3.141592653589793115997963468544185161590576171875') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1988 | |
| 1989 | Q. Within a complex calculation, how can I make sure that I haven't gotten a |
| 1990 | spurious result because of insufficient precision or rounding anomalies. |
| 1991 | |
| 1992 | A. The decimal module makes it easy to test results. A best practice is to |
| 1993 | re-run calculations using greater precision and with various rounding modes. |
| 1994 | Widely differing results indicate insufficient precision, rounding mode issues, |
| 1995 | ill-conditioned inputs, or a numerically unstable algorithm. |
| 1996 | |
| 1997 | Q. I noticed that context precision is applied to the results of operations but |
| 1998 | not to the inputs. Is there anything to watch out for when mixing values of |
| 1999 | different precisions? |
| 2000 | |
| 2001 | A. Yes. The principle is that all values are considered to be exact and so is |
| 2002 | the arithmetic on those values. Only the results are rounded. The advantage |
| 2003 | for inputs is that "what you type is what you get". A disadvantage is that the |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 2004 | results can look odd if you forget that the inputs haven't been rounded: |
| 2005 | |
| 2006 | .. doctest:: newcontext |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2007 | |
| 2008 | >>> getcontext().prec = 3 |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 2009 | >>> Decimal('3.104') + Decimal('2.104') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 2010 | Decimal('5.21') |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 2011 | >>> Decimal('3.104') + Decimal('0.000') + Decimal('2.104') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 2012 | Decimal('5.20') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2013 | |
| 2014 | The solution is either to increase precision or to force rounding of inputs |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 2015 | using the unary plus operation: |
| 2016 | |
| 2017 | .. doctest:: newcontext |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2018 | |
| 2019 | >>> getcontext().prec = 3 |
| 2020 | >>> +Decimal('1.23456789') # unary plus triggers rounding |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 2021 | Decimal('1.23') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2022 | |
| 2023 | Alternatively, inputs can be rounded upon creation using the |
Georg Brandl | 9f66232 | 2008-03-22 11:47:10 +0000 | [diff] [blame] | 2024 | :meth:`Context.create_decimal` method: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2025 | |
| 2026 | >>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678') |
Raymond Hettinger | abe3237 | 2008-02-14 02:41:22 +0000 | [diff] [blame] | 2027 | Decimal('1.2345') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 2028 | |