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