Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1 | # Copyright (c) 2004 Python Software Foundation. |
| 2 | # All rights reserved. |
| 3 | |
| 4 | # Written by Eric Price <eprice at tjhsst.edu> |
| 5 | # and Facundo Batista <facundo at taniquetil.com.ar> |
| 6 | # and Raymond Hettinger <python at rcn.com> |
Fred Drake | 1f34eb1 | 2004-07-01 14:28:36 +0000 | [diff] [blame] | 7 | # and Aahz <aahz at pobox.com> |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 8 | # and Tim Peters |
| 9 | |
Raymond Hettinger | 27dbcf2 | 2004-08-19 22:39:55 +0000 | [diff] [blame] | 10 | # This module is currently Py2.3 compatible and should be kept that way |
| 11 | # unless a major compelling advantage arises. IOW, 2.3 compatibility is |
| 12 | # strongly preferred, but not guaranteed. |
| 13 | |
| 14 | # Also, this module should be kept in sync with the latest updates of |
| 15 | # the IBM specification as it evolves. Those updates will be treated |
| 16 | # as bug fixes (deviation from the spec is a compatibility, usability |
| 17 | # bug) and will be backported. At this point the spec is stabilizing |
| 18 | # and the updates are becoming fewer, smaller, and less significant. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 19 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 20 | """ |
| 21 | This is a Py2.3 implementation of decimal floating point arithmetic based on |
| 22 | the General Decimal Arithmetic Specification: |
| 23 | |
| 24 | www2.hursley.ibm.com/decimal/decarith.html |
| 25 | |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 26 | and IEEE standard 854-1987: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 27 | |
| 28 | www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html |
| 29 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 30 | Decimal floating point has finite precision with arbitrarily large bounds. |
| 31 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 32 | The purpose of this module is to support arithmetic using familiar |
| 33 | "schoolhouse" rules and to avoid some of the tricky representation |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 34 | issues associated with binary floating point. The package is especially |
| 35 | useful for financial applications or for contexts where users have |
| 36 | expectations that are at odds with binary floating point (for instance, |
| 37 | in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead |
| 38 | of the expected Decimal("0.00") returned by decimal floating point). |
| 39 | |
| 40 | Here are some examples of using the decimal module: |
| 41 | |
| 42 | >>> from decimal import * |
Raymond Hettinger | bd7f76d | 2004-07-08 00:49:18 +0000 | [diff] [blame] | 43 | >>> setcontext(ExtendedContext) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 44 | >>> Decimal(0) |
| 45 | Decimal("0") |
| 46 | >>> Decimal("1") |
| 47 | Decimal("1") |
| 48 | >>> Decimal("-.0123") |
| 49 | Decimal("-0.0123") |
| 50 | >>> Decimal(123456) |
| 51 | Decimal("123456") |
| 52 | >>> Decimal("123.45e12345678901234567890") |
| 53 | Decimal("1.2345E+12345678901234567892") |
| 54 | >>> Decimal("1.33") + Decimal("1.27") |
| 55 | Decimal("2.60") |
| 56 | >>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41") |
| 57 | Decimal("-2.20") |
| 58 | >>> dig = Decimal(1) |
| 59 | >>> print dig / Decimal(3) |
| 60 | 0.333333333 |
| 61 | >>> getcontext().prec = 18 |
| 62 | >>> print dig / Decimal(3) |
| 63 | 0.333333333333333333 |
| 64 | >>> print dig.sqrt() |
| 65 | 1 |
| 66 | >>> print Decimal(3).sqrt() |
| 67 | 1.73205080756887729 |
| 68 | >>> print Decimal(3) ** 123 |
| 69 | 4.85192780976896427E+58 |
| 70 | >>> inf = Decimal(1) / Decimal(0) |
| 71 | >>> print inf |
| 72 | Infinity |
| 73 | >>> neginf = Decimal(-1) / Decimal(0) |
| 74 | >>> print neginf |
| 75 | -Infinity |
| 76 | >>> print neginf + inf |
| 77 | NaN |
| 78 | >>> print neginf * inf |
| 79 | -Infinity |
| 80 | >>> print dig / 0 |
| 81 | Infinity |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 82 | >>> getcontext().traps[DivisionByZero] = 1 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 83 | >>> print dig / 0 |
| 84 | Traceback (most recent call last): |
| 85 | ... |
| 86 | ... |
| 87 | ... |
| 88 | DivisionByZero: x / 0 |
| 89 | >>> c = Context() |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 90 | >>> c.traps[InvalidOperation] = 0 |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 91 | >>> print c.flags[InvalidOperation] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 92 | 0 |
| 93 | >>> c.divide(Decimal(0), Decimal(0)) |
| 94 | Decimal("NaN") |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 95 | >>> c.traps[InvalidOperation] = 1 |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 96 | >>> print c.flags[InvalidOperation] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 97 | 1 |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 98 | >>> c.flags[InvalidOperation] = 0 |
| 99 | >>> print c.flags[InvalidOperation] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 100 | 0 |
| 101 | >>> print c.divide(Decimal(0), Decimal(0)) |
| 102 | Traceback (most recent call last): |
| 103 | ... |
| 104 | ... |
| 105 | ... |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 106 | InvalidOperation: 0 / 0 |
| 107 | >>> print c.flags[InvalidOperation] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 108 | 1 |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 109 | >>> c.flags[InvalidOperation] = 0 |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 110 | >>> c.traps[InvalidOperation] = 0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 111 | >>> print c.divide(Decimal(0), Decimal(0)) |
| 112 | NaN |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 113 | >>> print c.flags[InvalidOperation] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 114 | 1 |
| 115 | >>> |
| 116 | """ |
| 117 | |
| 118 | __all__ = [ |
| 119 | # Two major classes |
| 120 | 'Decimal', 'Context', |
| 121 | |
| 122 | # Contexts |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 123 | 'DefaultContext', 'BasicContext', 'ExtendedContext', |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 124 | |
| 125 | # Exceptions |
Raymond Hettinger | d87ac8f | 2004-07-09 10:52:54 +0000 | [diff] [blame] | 126 | 'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero', |
| 127 | 'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow', |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 128 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 129 | # Constants for use in setting up contexts |
| 130 | 'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING', |
| 131 | 'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 132 | |
| 133 | # Functions for manipulating contexts |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 134 | 'setcontext', 'getcontext', 'localcontext' |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 135 | ] |
| 136 | |
Raymond Hettinger | eb26084 | 2005-06-07 18:52:34 +0000 | [diff] [blame] | 137 | import copy as _copy |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 138 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 139 | # Rounding |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 140 | ROUND_DOWN = 'ROUND_DOWN' |
| 141 | ROUND_HALF_UP = 'ROUND_HALF_UP' |
| 142 | ROUND_HALF_EVEN = 'ROUND_HALF_EVEN' |
| 143 | ROUND_CEILING = 'ROUND_CEILING' |
| 144 | ROUND_FLOOR = 'ROUND_FLOOR' |
| 145 | ROUND_UP = 'ROUND_UP' |
| 146 | ROUND_HALF_DOWN = 'ROUND_HALF_DOWN' |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 147 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 148 | # Rounding decision (not part of the public API) |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 149 | NEVER_ROUND = 'NEVER_ROUND' # Round in division (non-divmod), sqrt ONLY |
| 150 | ALWAYS_ROUND = 'ALWAYS_ROUND' # Every operation rounds at end. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 151 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 152 | # Errors |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 153 | |
| 154 | class DecimalException(ArithmeticError): |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 155 | """Base exception class. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 156 | |
| 157 | Used exceptions derive from this. |
| 158 | If an exception derives from another exception besides this (such as |
| 159 | Underflow (Inexact, Rounded, Subnormal) that indicates that it is only |
| 160 | called if the others are present. This isn't actually used for |
| 161 | anything, though. |
| 162 | |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 163 | handle -- Called when context._raise_error is called and the |
| 164 | trap_enabler is set. First argument is self, second is the |
| 165 | context. More arguments can be given, those being after |
| 166 | the explanation in _raise_error (For example, |
| 167 | context._raise_error(NewError, '(-x)!', self._sign) would |
| 168 | call NewError().handle(context, self._sign).) |
| 169 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 170 | To define a new exception, it should be sufficient to have it derive |
| 171 | from DecimalException. |
| 172 | """ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 173 | def handle(self, context, *args): |
| 174 | pass |
| 175 | |
| 176 | |
| 177 | class Clamped(DecimalException): |
| 178 | """Exponent of a 0 changed to fit bounds. |
| 179 | |
| 180 | This occurs and signals clamped if the exponent of a result has been |
| 181 | altered in order to fit the constraints of a specific concrete |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 182 | representation. This may occur when the exponent of a zero result would |
| 183 | be outside the bounds of a representation, or when a large normal |
| 184 | number would have an encoded exponent that cannot be represented. In |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 185 | this latter case, the exponent is reduced to fit and the corresponding |
| 186 | number of zero digits are appended to the coefficient ("fold-down"). |
| 187 | """ |
| 188 | |
| 189 | |
| 190 | class InvalidOperation(DecimalException): |
| 191 | """An invalid operation was performed. |
| 192 | |
| 193 | Various bad things cause this: |
| 194 | |
| 195 | Something creates a signaling NaN |
| 196 | -INF + INF |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 197 | 0 * (+-)INF |
| 198 | (+-)INF / (+-)INF |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 199 | x % 0 |
| 200 | (+-)INF % x |
| 201 | x._rescale( non-integer ) |
| 202 | sqrt(-x) , x > 0 |
| 203 | 0 ** 0 |
| 204 | x ** (non-integer) |
| 205 | x ** (+-)INF |
| 206 | An operand is invalid |
| 207 | """ |
| 208 | def handle(self, context, *args): |
| 209 | if args: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 210 | if args[0] == 1: # sNaN, must drop 's' but keep diagnostics |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 211 | return Decimal( (args[1]._sign, args[1]._int, 'n') ) |
| 212 | return NaN |
| 213 | |
| 214 | class ConversionSyntax(InvalidOperation): |
| 215 | """Trying to convert badly formed string. |
| 216 | |
| 217 | This occurs and signals invalid-operation if an string is being |
| 218 | converted to a number and it does not conform to the numeric string |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 219 | syntax. The result is [0,qNaN]. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 220 | """ |
Facundo Batista | ac4ae4b | 2006-07-18 12:16:13 +0000 | [diff] [blame] | 221 | |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 222 | def handle(self, context, *args): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 223 | return (0, (0,), 'n') # Passed to something which uses a tuple. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 224 | |
| 225 | class DivisionByZero(DecimalException, ZeroDivisionError): |
| 226 | """Division by 0. |
| 227 | |
| 228 | This occurs and signals division-by-zero if division of a finite number |
| 229 | by zero was attempted (during a divide-integer or divide operation, or a |
| 230 | power operation with negative right-hand operand), and the dividend was |
| 231 | not zero. |
| 232 | |
| 233 | The result of the operation is [sign,inf], where sign is the exclusive |
| 234 | or of the signs of the operands for divide, or is 1 for an odd power of |
| 235 | -0, for power. |
| 236 | """ |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 237 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 238 | def handle(self, context, sign, double = None, *args): |
| 239 | if double is not None: |
| 240 | return (Infsign[sign],)*2 |
| 241 | return Infsign[sign] |
| 242 | |
| 243 | class DivisionImpossible(InvalidOperation): |
| 244 | """Cannot perform the division adequately. |
| 245 | |
| 246 | This occurs and signals invalid-operation if the integer result of a |
| 247 | divide-integer or remainder operation had too many digits (would be |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 248 | longer than precision). The result is [0,qNaN]. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 249 | """ |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 250 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 251 | def handle(self, context, *args): |
| 252 | return (NaN, NaN) |
| 253 | |
| 254 | class DivisionUndefined(InvalidOperation, ZeroDivisionError): |
| 255 | """Undefined result of division. |
| 256 | |
| 257 | This occurs and signals invalid-operation if division by zero was |
| 258 | attempted (during a divide-integer, divide, or remainder operation), and |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 259 | the dividend is also zero. The result is [0,qNaN]. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 260 | """ |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 261 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 262 | def handle(self, context, tup=None, *args): |
| 263 | if tup is not None: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 264 | return (NaN, NaN) # for 0 %0, 0 // 0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 265 | return NaN |
| 266 | |
| 267 | class Inexact(DecimalException): |
| 268 | """Had to round, losing information. |
| 269 | |
| 270 | This occurs and signals inexact whenever the result of an operation is |
| 271 | not exact (that is, it needed to be rounded and any discarded digits |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 272 | were non-zero), or if an overflow or underflow condition occurs. The |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 273 | result in all cases is unchanged. |
| 274 | |
| 275 | The inexact signal may be tested (or trapped) to determine if a given |
| 276 | operation (or sequence of operations) was inexact. |
| 277 | """ |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 278 | pass |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 279 | |
| 280 | class InvalidContext(InvalidOperation): |
| 281 | """Invalid context. Unknown rounding, for example. |
| 282 | |
| 283 | This occurs and signals invalid-operation if an invalid context was |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 284 | detected during an operation. This can occur if contexts are not checked |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 285 | on creation and either the precision exceeds the capability of the |
| 286 | underlying concrete representation or an unknown or unsupported rounding |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 287 | was specified. These aspects of the context need only be checked when |
| 288 | the values are required to be used. The result is [0,qNaN]. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 289 | """ |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 290 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 291 | def handle(self, context, *args): |
| 292 | return NaN |
| 293 | |
| 294 | class Rounded(DecimalException): |
| 295 | """Number got rounded (not necessarily changed during rounding). |
| 296 | |
| 297 | This occurs and signals rounded whenever the result of an operation is |
| 298 | rounded (that is, some zero or non-zero digits were discarded from the |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 299 | coefficient), or if an overflow or underflow condition occurs. The |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 300 | result in all cases is unchanged. |
| 301 | |
| 302 | The rounded signal may be tested (or trapped) to determine if a given |
| 303 | operation (or sequence of operations) caused a loss of precision. |
| 304 | """ |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 305 | pass |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 306 | |
| 307 | class Subnormal(DecimalException): |
| 308 | """Exponent < Emin before rounding. |
| 309 | |
| 310 | This occurs and signals subnormal whenever the result of a conversion or |
| 311 | operation is subnormal (that is, its adjusted exponent is less than |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 312 | Emin, before any rounding). The result in all cases is unchanged. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 313 | |
| 314 | The subnormal signal may be tested (or trapped) to determine if a given |
| 315 | or operation (or sequence of operations) yielded a subnormal result. |
| 316 | """ |
| 317 | pass |
| 318 | |
| 319 | class Overflow(Inexact, Rounded): |
| 320 | """Numerical overflow. |
| 321 | |
| 322 | This occurs and signals overflow if the adjusted exponent of a result |
| 323 | (from a conversion or from an operation that is not an attempt to divide |
| 324 | by zero), after rounding, would be greater than the largest value that |
| 325 | can be handled by the implementation (the value Emax). |
| 326 | |
| 327 | The result depends on the rounding mode: |
| 328 | |
| 329 | For round-half-up and round-half-even (and for round-half-down and |
| 330 | round-up, if implemented), the result of the operation is [sign,inf], |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 331 | where sign is the sign of the intermediate result. For round-down, the |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 332 | result is the largest finite number that can be represented in the |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 333 | current precision, with the sign of the intermediate result. For |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 334 | round-ceiling, the result is the same as for round-down if the sign of |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 335 | the intermediate result is 1, or is [0,inf] otherwise. For round-floor, |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 336 | the result is the same as for round-down if the sign of the intermediate |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 337 | result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 338 | will also be raised. |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 339 | """ |
| 340 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 341 | def handle(self, context, sign, *args): |
| 342 | if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN, |
| 343 | ROUND_HALF_DOWN, ROUND_UP): |
| 344 | return Infsign[sign] |
| 345 | if sign == 0: |
| 346 | if context.rounding == ROUND_CEILING: |
| 347 | return Infsign[sign] |
| 348 | return Decimal((sign, (9,)*context.prec, |
| 349 | context.Emax-context.prec+1)) |
| 350 | if sign == 1: |
| 351 | if context.rounding == ROUND_FLOOR: |
| 352 | return Infsign[sign] |
| 353 | return Decimal( (sign, (9,)*context.prec, |
| 354 | context.Emax-context.prec+1)) |
| 355 | |
| 356 | |
| 357 | class Underflow(Inexact, Rounded, Subnormal): |
| 358 | """Numerical underflow with result rounded to 0. |
| 359 | |
| 360 | This occurs and signals underflow if a result is inexact and the |
| 361 | adjusted exponent of the result would be smaller (more negative) than |
| 362 | the smallest value that can be handled by the implementation (the value |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 363 | Emin). That is, the result is both inexact and subnormal. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 364 | |
| 365 | The result after an underflow will be a subnormal number rounded, if |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 366 | necessary, so that its exponent is not less than Etiny. This may result |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 367 | in 0 with the sign of the intermediate result and an exponent of Etiny. |
| 368 | |
| 369 | In all cases, Inexact, Rounded, and Subnormal will also be raised. |
| 370 | """ |
| 371 | |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 372 | # List of public traps and flags |
Raymond Hettinger | fed5296 | 2004-07-14 15:41:57 +0000 | [diff] [blame] | 373 | _signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded, |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 374 | Underflow, InvalidOperation, Subnormal] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 375 | |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 376 | # Map conditions (per the spec) to signals |
| 377 | _condition_map = {ConversionSyntax:InvalidOperation, |
| 378 | DivisionImpossible:InvalidOperation, |
| 379 | DivisionUndefined:InvalidOperation, |
| 380 | InvalidContext:InvalidOperation} |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 381 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 382 | ##### Context Functions ################################################## |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 383 | |
Raymond Hettinger | ef66deb | 2004-07-14 21:04:27 +0000 | [diff] [blame] | 384 | # The getcontext() and setcontext() function manage access to a thread-local |
| 385 | # current context. Py2.4 offers direct support for thread locals. If that |
| 386 | # is not available, use threading.currentThread() which is slower but will |
Raymond Hettinger | 7e71fa5 | 2004-12-18 19:07:19 +0000 | [diff] [blame] | 387 | # work for older Pythons. If threads are not part of the build, create a |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 388 | # mock threading object with threading.local() returning the module namespace. |
Raymond Hettinger | 7e71fa5 | 2004-12-18 19:07:19 +0000 | [diff] [blame] | 389 | |
| 390 | try: |
| 391 | import threading |
| 392 | except ImportError: |
| 393 | # Python was compiled without threads; create a mock object instead |
| 394 | import sys |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 395 | class MockThreading(object): |
Raymond Hettinger | 7e71fa5 | 2004-12-18 19:07:19 +0000 | [diff] [blame] | 396 | def local(self, sys=sys): |
| 397 | return sys.modules[__name__] |
| 398 | threading = MockThreading() |
| 399 | del sys, MockThreading |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 400 | |
Raymond Hettinger | ef66deb | 2004-07-14 21:04:27 +0000 | [diff] [blame] | 401 | try: |
| 402 | threading.local |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 403 | |
Raymond Hettinger | ef66deb | 2004-07-14 21:04:27 +0000 | [diff] [blame] | 404 | except AttributeError: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 405 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 406 | # To fix reloading, force it to create a new context |
| 407 | # Old contexts have different exceptions in their dicts, making problems. |
Raymond Hettinger | ef66deb | 2004-07-14 21:04:27 +0000 | [diff] [blame] | 408 | if hasattr(threading.currentThread(), '__decimal_context__'): |
| 409 | del threading.currentThread().__decimal_context__ |
| 410 | |
| 411 | def setcontext(context): |
| 412 | """Set this thread's context to context.""" |
| 413 | if context in (DefaultContext, BasicContext, ExtendedContext): |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 414 | context = context.copy() |
Raymond Hettinger | 61992ef | 2004-08-06 23:42:16 +0000 | [diff] [blame] | 415 | context.clear_flags() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 416 | threading.currentThread().__decimal_context__ = context |
Raymond Hettinger | ef66deb | 2004-07-14 21:04:27 +0000 | [diff] [blame] | 417 | |
| 418 | def getcontext(): |
| 419 | """Returns this thread's context. |
| 420 | |
| 421 | If this thread does not yet have a context, returns |
| 422 | a new context and sets this thread's context. |
| 423 | New contexts are copies of DefaultContext. |
| 424 | """ |
| 425 | try: |
| 426 | return threading.currentThread().__decimal_context__ |
| 427 | except AttributeError: |
| 428 | context = Context() |
| 429 | threading.currentThread().__decimal_context__ = context |
| 430 | return context |
| 431 | |
| 432 | else: |
| 433 | |
| 434 | local = threading.local() |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 435 | if hasattr(local, '__decimal_context__'): |
| 436 | del local.__decimal_context__ |
Raymond Hettinger | ef66deb | 2004-07-14 21:04:27 +0000 | [diff] [blame] | 437 | |
| 438 | def getcontext(_local=local): |
| 439 | """Returns this thread's context. |
| 440 | |
| 441 | If this thread does not yet have a context, returns |
| 442 | a new context and sets this thread's context. |
| 443 | New contexts are copies of DefaultContext. |
| 444 | """ |
| 445 | try: |
| 446 | return _local.__decimal_context__ |
| 447 | except AttributeError: |
| 448 | context = Context() |
| 449 | _local.__decimal_context__ = context |
| 450 | return context |
| 451 | |
| 452 | def setcontext(context, _local=local): |
| 453 | """Set this thread's context to context.""" |
| 454 | if context in (DefaultContext, BasicContext, ExtendedContext): |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 455 | context = context.copy() |
Raymond Hettinger | 61992ef | 2004-08-06 23:42:16 +0000 | [diff] [blame] | 456 | context.clear_flags() |
Raymond Hettinger | ef66deb | 2004-07-14 21:04:27 +0000 | [diff] [blame] | 457 | _local.__decimal_context__ = context |
| 458 | |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 459 | del threading, local # Don't contaminate the namespace |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 460 | |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 461 | def localcontext(ctx=None): |
| 462 | """Return a context manager for a copy of the supplied context |
| 463 | |
| 464 | Uses a copy of the current context if no context is specified |
| 465 | The returned context manager creates a local decimal context |
| 466 | in a with statement: |
| 467 | def sin(x): |
| 468 | with localcontext() as ctx: |
| 469 | ctx.prec += 2 |
| 470 | # Rest of sin calculation algorithm |
| 471 | # uses a precision 2 greater than normal |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 472 | return +s # Convert result to normal precision |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 473 | |
| 474 | def sin(x): |
| 475 | with localcontext(ExtendedContext): |
| 476 | # Rest of sin calculation algorithm |
| 477 | # uses the Extended Context from the |
| 478 | # General Decimal Arithmetic Specification |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 479 | return +s # Convert result to normal context |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 480 | |
| 481 | """ |
Neal Norwitz | 681d867 | 2006-09-02 18:51:34 +0000 | [diff] [blame] | 482 | # The string below can't be included in the docstring until Python 2.6 |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 483 | # as the doctest module doesn't understand __future__ statements |
| 484 | """ |
| 485 | >>> from __future__ import with_statement |
| 486 | >>> print getcontext().prec |
| 487 | 28 |
| 488 | >>> with localcontext(): |
| 489 | ... ctx = getcontext() |
Raymond Hettinger | 495df47 | 2007-02-08 01:42:35 +0000 | [diff] [blame] | 490 | ... ctx.prec += 2 |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 491 | ... print ctx.prec |
| 492 | ... |
| 493 | 30 |
| 494 | >>> with localcontext(ExtendedContext): |
| 495 | ... print getcontext().prec |
| 496 | ... |
| 497 | 9 |
| 498 | >>> print getcontext().prec |
| 499 | 28 |
| 500 | """ |
Nick Coghlan | ced1218 | 2006-09-02 03:54:17 +0000 | [diff] [blame] | 501 | if ctx is None: ctx = getcontext() |
| 502 | return _ContextManager(ctx) |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 503 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 504 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 505 | ##### Decimal class ####################################################### |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 506 | |
| 507 | class Decimal(object): |
| 508 | """Floating point class for decimal arithmetic.""" |
| 509 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 510 | __slots__ = ('_exp','_int','_sign', '_is_special') |
| 511 | # Generally, the value of the Decimal instance is given by |
| 512 | # (-1)**_sign * _int * 10**_exp |
| 513 | # Special values are signified by _is_special == True |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 514 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 515 | # We're immutable, so use __new__ not __init__ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 516 | def __new__(cls, value="0", context=None): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 517 | """Create a decimal point instance. |
| 518 | |
| 519 | >>> Decimal('3.14') # string input |
| 520 | Decimal("3.14") |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 521 | >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 522 | Decimal("3.14") |
| 523 | >>> Decimal(314) # int or long |
| 524 | Decimal("314") |
| 525 | >>> Decimal(Decimal(314)) # another decimal instance |
| 526 | Decimal("314") |
| 527 | """ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 528 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 529 | self = object.__new__(cls) |
| 530 | self._is_special = False |
| 531 | |
| 532 | # From an internal working value |
| 533 | if isinstance(value, _WorkRep): |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 534 | self._sign = value.sign |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 535 | self._int = tuple(map(int, str(value.int))) |
| 536 | self._exp = int(value.exp) |
| 537 | return self |
| 538 | |
| 539 | # From another decimal |
| 540 | if isinstance(value, Decimal): |
| 541 | self._exp = value._exp |
| 542 | self._sign = value._sign |
| 543 | self._int = value._int |
| 544 | self._is_special = value._is_special |
| 545 | return self |
| 546 | |
| 547 | # From an integer |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 548 | if isinstance(value, (int,long)): |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 549 | if value >= 0: |
| 550 | self._sign = 0 |
| 551 | else: |
| 552 | self._sign = 1 |
| 553 | self._exp = 0 |
| 554 | self._int = tuple(map(int, str(abs(value)))) |
| 555 | return self |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 556 | |
| 557 | # tuple/list conversion (possibly from as_tuple()) |
| 558 | if isinstance(value, (list,tuple)): |
| 559 | if len(value) != 3: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 560 | raise ValueError('Invalid arguments') |
Raymond Hettinger | dbecd93 | 2005-02-06 06:57:08 +0000 | [diff] [blame] | 561 | if value[0] not in (0,1): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 562 | raise ValueError('Invalid sign') |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 563 | for digit in value[1]: |
| 564 | if not isinstance(digit, (int,long)) or digit < 0: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 565 | raise ValueError("The second value in the tuple must be" |
| 566 | "composed of non negative integer elements.") |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 567 | self._sign = value[0] |
| 568 | self._int = tuple(value[1]) |
| 569 | if value[2] in ('F','n','N'): |
| 570 | self._exp = value[2] |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 571 | self._is_special = True |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 572 | else: |
| 573 | self._exp = int(value[2]) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 574 | return self |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 575 | |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 576 | if isinstance(value, float): |
| 577 | raise TypeError("Cannot convert float to Decimal. " + |
| 578 | "First convert the float to a string") |
| 579 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 580 | # Other argument types may require the context during interpretation |
| 581 | if context is None: |
| 582 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 583 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 584 | # From a string |
| 585 | # REs insist on real strings, so we can too. |
| 586 | if isinstance(value, basestring): |
| 587 | if _isinfinity(value): |
| 588 | self._exp = 'F' |
| 589 | self._int = (0,) |
| 590 | self._is_special = True |
| 591 | if _isinfinity(value) == 1: |
| 592 | self._sign = 0 |
| 593 | else: |
| 594 | self._sign = 1 |
| 595 | return self |
| 596 | if _isnan(value): |
| 597 | sig, sign, diag = _isnan(value) |
| 598 | self._is_special = True |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 599 | if len(diag) > context.prec: # Diagnostic info too long |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 600 | self._sign, self._int, self._exp = \ |
| 601 | context._raise_error(ConversionSyntax) |
| 602 | return self |
| 603 | if sig == 1: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 604 | self._exp = 'n' # qNaN |
| 605 | else: # sig == 2 |
| 606 | self._exp = 'N' # sNaN |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 607 | self._sign = sign |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 608 | self._int = tuple(map(int, diag)) # Diagnostic info |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 609 | return self |
| 610 | try: |
| 611 | self._sign, self._int, self._exp = _string2exact(value) |
| 612 | except ValueError: |
| 613 | self._is_special = True |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 614 | self._sign, self._int, self._exp = \ |
Facundo Batista | 8496939 | 2007-08-15 15:13:09 +0000 | [diff] [blame] | 615 | context._raise_error(ConversionSyntax, |
| 616 | "Invalid literal for Decimal: %r" % value) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 617 | return self |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 618 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 619 | raise TypeError("Cannot convert %r to Decimal" % value) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 620 | |
| 621 | def _isnan(self): |
| 622 | """Returns whether the number is not actually one. |
| 623 | |
| 624 | 0 if a number |
| 625 | 1 if NaN |
| 626 | 2 if sNaN |
| 627 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 628 | if self._is_special: |
| 629 | exp = self._exp |
| 630 | if exp == 'n': |
| 631 | return 1 |
| 632 | elif exp == 'N': |
| 633 | return 2 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 634 | return 0 |
| 635 | |
| 636 | def _isinfinity(self): |
| 637 | """Returns whether the number is infinite |
| 638 | |
| 639 | 0 if finite or not a number |
| 640 | 1 if +INF |
| 641 | -1 if -INF |
| 642 | """ |
| 643 | if self._exp == 'F': |
| 644 | if self._sign: |
| 645 | return -1 |
| 646 | return 1 |
| 647 | return 0 |
| 648 | |
| 649 | def _check_nans(self, other = None, context=None): |
| 650 | """Returns whether the number is not actually one. |
| 651 | |
| 652 | if self, other are sNaN, signal |
| 653 | if self, other are NaN return nan |
| 654 | return 0 |
| 655 | |
| 656 | Done before operations. |
| 657 | """ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 658 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 659 | self_is_nan = self._isnan() |
| 660 | if other is None: |
| 661 | other_is_nan = False |
| 662 | else: |
| 663 | other_is_nan = other._isnan() |
| 664 | |
| 665 | if self_is_nan or other_is_nan: |
| 666 | if context is None: |
| 667 | context = getcontext() |
| 668 | |
| 669 | if self_is_nan == 2: |
| 670 | return context._raise_error(InvalidOperation, 'sNaN', |
| 671 | 1, self) |
| 672 | if other_is_nan == 2: |
| 673 | return context._raise_error(InvalidOperation, 'sNaN', |
| 674 | 1, other) |
| 675 | if self_is_nan: |
| 676 | return self |
| 677 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 678 | return other |
| 679 | return 0 |
| 680 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 681 | def __nonzero__(self): |
| 682 | """Is the number non-zero? |
| 683 | |
| 684 | 0 if self == 0 |
| 685 | 1 if self != 0 |
| 686 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 687 | if self._is_special: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 688 | return 1 |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 689 | return sum(self._int) != 0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 690 | |
| 691 | def __cmp__(self, other, context=None): |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 692 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 693 | if other is NotImplemented: |
| 694 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 695 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 696 | if self._is_special or other._is_special: |
| 697 | ans = self._check_nans(other, context) |
| 698 | if ans: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 699 | return 1 # Comparison involving NaN's always reports self > other |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 700 | |
| 701 | # INF = INF |
| 702 | return cmp(self._isinfinity(), other._isinfinity()) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 703 | |
| 704 | if not self and not other: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 705 | return 0 # If both 0, sign comparison isn't certain. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 706 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 707 | # If different signs, neg one is less |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 708 | if other._sign < self._sign: |
| 709 | return -1 |
| 710 | if self._sign < other._sign: |
| 711 | return 1 |
| 712 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 713 | self_adjusted = self.adjusted() |
| 714 | other_adjusted = other.adjusted() |
| 715 | if self_adjusted == other_adjusted and \ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 716 | self._int + (0,)*(self._exp - other._exp) == \ |
| 717 | other._int + (0,)*(other._exp - self._exp): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 718 | return 0 # equal, except in precision. ([0]*(-x) = []) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 719 | elif self_adjusted > other_adjusted and self._int[0] != 0: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 720 | return (-1)**self._sign |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 721 | elif self_adjusted < other_adjusted and other._int[0] != 0: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 722 | return -((-1)**self._sign) |
| 723 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 724 | # Need to round, so make sure we have a valid context |
| 725 | if context is None: |
| 726 | context = getcontext() |
| 727 | |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 728 | context = context._shallow_copy() |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 729 | rounding = context._set_rounding(ROUND_UP) # round away from 0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 730 | |
| 731 | flags = context._ignore_all_flags() |
| 732 | res = self.__sub__(other, context=context) |
| 733 | |
| 734 | context._regard_flags(*flags) |
| 735 | |
| 736 | context.rounding = rounding |
| 737 | |
| 738 | if not res: |
| 739 | return 0 |
| 740 | elif res._sign: |
| 741 | return -1 |
| 742 | return 1 |
| 743 | |
Raymond Hettinger | 0aeac10 | 2004-07-05 22:53:03 +0000 | [diff] [blame] | 744 | def __eq__(self, other): |
| 745 | if not isinstance(other, (Decimal, int, long)): |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 746 | return NotImplemented |
Raymond Hettinger | 0aeac10 | 2004-07-05 22:53:03 +0000 | [diff] [blame] | 747 | return self.__cmp__(other) == 0 |
| 748 | |
| 749 | def __ne__(self, other): |
| 750 | if not isinstance(other, (Decimal, int, long)): |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 751 | return NotImplemented |
Raymond Hettinger | 0aeac10 | 2004-07-05 22:53:03 +0000 | [diff] [blame] | 752 | return self.__cmp__(other) != 0 |
| 753 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 754 | def compare(self, other, context=None): |
| 755 | """Compares one to another. |
| 756 | |
| 757 | -1 => a < b |
| 758 | 0 => a = b |
| 759 | 1 => a > b |
| 760 | NaN => one is NaN |
| 761 | Like __cmp__, but returns Decimal instances. |
| 762 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 763 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 764 | if other is NotImplemented: |
| 765 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 766 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 767 | # Compare(NaN, NaN) = NaN |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 768 | if (self._is_special or other and other._is_special): |
| 769 | ans = self._check_nans(other, context) |
| 770 | if ans: |
| 771 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 772 | |
| 773 | return Decimal(self.__cmp__(other, context)) |
| 774 | |
| 775 | def __hash__(self): |
| 776 | """x.__hash__() <==> hash(x)""" |
| 777 | # Decimal integers must hash the same as the ints |
| 778 | # Non-integer decimals are normalized and hashed as strings |
Georg Brandl | 1fb9f52 | 2006-05-11 19:57:09 +0000 | [diff] [blame] | 779 | # Normalization assures that hash(100E-1) == hash(10) |
Raymond Hettinger | bea3f6f | 2005-03-15 04:59:17 +0000 | [diff] [blame] | 780 | if self._is_special: |
| 781 | if self._isnan(): |
| 782 | raise TypeError('Cannot hash a NaN value.') |
| 783 | return hash(str(self)) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 784 | i = int(self) |
| 785 | if self == Decimal(i): |
| 786 | return hash(i) |
| 787 | assert self.__nonzero__() # '-0' handled by integer case |
| 788 | return hash(str(self.normalize())) |
| 789 | |
| 790 | def as_tuple(self): |
| 791 | """Represents the number as a triple tuple. |
| 792 | |
| 793 | To show the internals exactly as they are. |
| 794 | """ |
| 795 | return (self._sign, self._int, self._exp) |
| 796 | |
| 797 | def __repr__(self): |
| 798 | """Represents the number as an instance of Decimal.""" |
| 799 | # Invariant: eval(repr(d)) == d |
| 800 | return 'Decimal("%s")' % str(self) |
| 801 | |
| 802 | def __str__(self, eng = 0, context=None): |
| 803 | """Return string representation of the number in scientific notation. |
| 804 | |
| 805 | Captures all of the information in the underlying representation. |
| 806 | """ |
| 807 | |
Raymond Hettinger | e5a0a96 | 2005-06-20 09:49:42 +0000 | [diff] [blame] | 808 | if self._is_special: |
| 809 | if self._isnan(): |
| 810 | minus = '-'*self._sign |
| 811 | if self._int == (0,): |
| 812 | info = '' |
| 813 | else: |
| 814 | info = ''.join(map(str, self._int)) |
| 815 | if self._isnan() == 2: |
| 816 | return minus + 'sNaN' + info |
| 817 | return minus + 'NaN' + info |
| 818 | if self._isinfinity(): |
| 819 | minus = '-'*self._sign |
| 820 | return minus + 'Infinity' |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 821 | |
| 822 | if context is None: |
| 823 | context = getcontext() |
| 824 | |
| 825 | tmp = map(str, self._int) |
| 826 | numdigits = len(self._int) |
| 827 | leftdigits = self._exp + numdigits |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 828 | if eng and not self: # self = 0eX wants 0[.0[0]]eY, not [[0]0]0eY |
| 829 | if self._exp < 0 and self._exp >= -6: # short, no need for e/E |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 830 | s = '-'*self._sign + '0.' + '0'*(abs(self._exp)) |
| 831 | return s |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 832 | # exp is closest mult. of 3 >= self._exp |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 833 | exp = ((self._exp - 1)// 3 + 1) * 3 |
| 834 | if exp != self._exp: |
| 835 | s = '0.'+'0'*(exp - self._exp) |
| 836 | else: |
| 837 | s = '0' |
| 838 | if exp != 0: |
| 839 | if context.capitals: |
| 840 | s += 'E' |
| 841 | else: |
| 842 | s += 'e' |
| 843 | if exp > 0: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 844 | s += '+' # 0.0e+3, not 0.0e3 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 845 | s += str(exp) |
| 846 | s = '-'*self._sign + s |
| 847 | return s |
| 848 | if eng: |
| 849 | dotplace = (leftdigits-1)%3+1 |
| 850 | adjexp = leftdigits -1 - (leftdigits-1)%3 |
| 851 | else: |
| 852 | adjexp = leftdigits-1 |
| 853 | dotplace = 1 |
| 854 | if self._exp == 0: |
| 855 | pass |
| 856 | elif self._exp < 0 and adjexp >= 0: |
| 857 | tmp.insert(leftdigits, '.') |
| 858 | elif self._exp < 0 and adjexp >= -6: |
| 859 | tmp[0:0] = ['0'] * int(-leftdigits) |
| 860 | tmp.insert(0, '0.') |
| 861 | else: |
| 862 | if numdigits > dotplace: |
| 863 | tmp.insert(dotplace, '.') |
| 864 | elif numdigits < dotplace: |
| 865 | tmp.extend(['0']*(dotplace-numdigits)) |
| 866 | if adjexp: |
| 867 | if not context.capitals: |
| 868 | tmp.append('e') |
| 869 | else: |
| 870 | tmp.append('E') |
| 871 | if adjexp > 0: |
| 872 | tmp.append('+') |
| 873 | tmp.append(str(adjexp)) |
| 874 | if eng: |
| 875 | while tmp[0:1] == ['0']: |
| 876 | tmp[0:1] = [] |
| 877 | if len(tmp) == 0 or tmp[0] == '.' or tmp[0].lower() == 'e': |
| 878 | tmp[0:0] = ['0'] |
| 879 | if self._sign: |
| 880 | tmp.insert(0, '-') |
| 881 | |
| 882 | return ''.join(tmp) |
| 883 | |
| 884 | def to_eng_string(self, context=None): |
| 885 | """Convert to engineering-type string. |
| 886 | |
| 887 | Engineering notation has an exponent which is a multiple of 3, so there |
| 888 | are up to 3 digits left of the decimal place. |
| 889 | |
| 890 | Same rules for when in exponential and when as a value as in __str__. |
| 891 | """ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 892 | return self.__str__(eng=1, context=context) |
| 893 | |
| 894 | def __neg__(self, context=None): |
| 895 | """Returns a copy with the sign switched. |
| 896 | |
| 897 | Rounds, if it has reason. |
| 898 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 899 | if self._is_special: |
| 900 | ans = self._check_nans(context=context) |
| 901 | if ans: |
| 902 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 903 | |
| 904 | if not self: |
| 905 | # -Decimal('0') is Decimal('0'), not Decimal('-0') |
| 906 | sign = 0 |
| 907 | elif self._sign: |
| 908 | sign = 0 |
| 909 | else: |
| 910 | sign = 1 |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 911 | |
| 912 | if context is None: |
| 913 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 914 | if context._rounding_decision == ALWAYS_ROUND: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 915 | return Decimal((sign, self._int, self._exp))._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 916 | return Decimal( (sign, self._int, self._exp)) |
| 917 | |
| 918 | def __pos__(self, context=None): |
| 919 | """Returns a copy, unless it is a sNaN. |
| 920 | |
| 921 | Rounds the number (if more then precision digits) |
| 922 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 923 | if self._is_special: |
| 924 | ans = self._check_nans(context=context) |
| 925 | if ans: |
| 926 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 927 | |
| 928 | sign = self._sign |
| 929 | if not self: |
| 930 | # + (-0) = 0 |
| 931 | sign = 0 |
| 932 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 933 | if context is None: |
| 934 | context = getcontext() |
| 935 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 936 | if context._rounding_decision == ALWAYS_ROUND: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 937 | ans = self._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 938 | else: |
| 939 | ans = Decimal(self) |
| 940 | ans._sign = sign |
| 941 | return ans |
| 942 | |
| 943 | def __abs__(self, round=1, context=None): |
| 944 | """Returns the absolute value of self. |
| 945 | |
| 946 | If the second argument is 0, do not round. |
| 947 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 948 | if self._is_special: |
| 949 | ans = self._check_nans(context=context) |
| 950 | if ans: |
| 951 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 952 | |
| 953 | if not round: |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 954 | if context is None: |
| 955 | context = getcontext() |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 956 | context = context._shallow_copy() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 957 | context._set_rounding_decision(NEVER_ROUND) |
| 958 | |
| 959 | if self._sign: |
| 960 | ans = self.__neg__(context=context) |
| 961 | else: |
| 962 | ans = self.__pos__(context=context) |
| 963 | |
| 964 | return ans |
| 965 | |
| 966 | def __add__(self, other, context=None): |
| 967 | """Returns self + other. |
| 968 | |
| 969 | -INF + INF (or the reverse) cause InvalidOperation errors. |
| 970 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 971 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 972 | if other is NotImplemented: |
| 973 | return other |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 974 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 975 | if context is None: |
| 976 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 977 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 978 | if self._is_special or other._is_special: |
| 979 | ans = self._check_nans(other, context) |
| 980 | if ans: |
| 981 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 982 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 983 | if self._isinfinity(): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 984 | # If both INF, same sign => same as both, opposite => error. |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 985 | if self._sign != other._sign and other._isinfinity(): |
| 986 | return context._raise_error(InvalidOperation, '-INF + INF') |
| 987 | return Decimal(self) |
| 988 | if other._isinfinity(): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 989 | return Decimal(other) # Can't both be infinity here |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 990 | |
| 991 | shouldround = context._rounding_decision == ALWAYS_ROUND |
| 992 | |
| 993 | exp = min(self._exp, other._exp) |
| 994 | negativezero = 0 |
| 995 | if context.rounding == ROUND_FLOOR and self._sign != other._sign: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 996 | # If the answer is 0, the sign should be negative, in this case. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 997 | negativezero = 1 |
| 998 | |
| 999 | if not self and not other: |
| 1000 | sign = min(self._sign, other._sign) |
| 1001 | if negativezero: |
| 1002 | sign = 1 |
| 1003 | return Decimal( (sign, (0,), exp)) |
| 1004 | if not self: |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1005 | exp = max(exp, other._exp - context.prec-1) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1006 | ans = other._rescale(exp, watchexp=0, context=context) |
| 1007 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1008 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1009 | return ans |
| 1010 | if not other: |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1011 | exp = max(exp, self._exp - context.prec-1) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1012 | ans = self._rescale(exp, watchexp=0, context=context) |
| 1013 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1014 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1015 | return ans |
| 1016 | |
| 1017 | op1 = _WorkRep(self) |
| 1018 | op2 = _WorkRep(other) |
| 1019 | op1, op2 = _normalize(op1, op2, shouldround, context.prec) |
| 1020 | |
| 1021 | result = _WorkRep() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1022 | if op1.sign != op2.sign: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1023 | # Equal and opposite |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1024 | if op1.int == op2.int: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1025 | if exp < context.Etiny(): |
| 1026 | exp = context.Etiny() |
| 1027 | context._raise_error(Clamped) |
| 1028 | return Decimal((negativezero, (0,), exp)) |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1029 | if op1.int < op2.int: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1030 | op1, op2 = op2, op1 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1031 | # OK, now abs(op1) > abs(op2) |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1032 | if op1.sign == 1: |
| 1033 | result.sign = 1 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1034 | op1.sign, op2.sign = op2.sign, op1.sign |
| 1035 | else: |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1036 | result.sign = 0 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1037 | # So we know the sign, and op1 > 0. |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1038 | elif op1.sign == 1: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1039 | result.sign = 1 |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1040 | op1.sign, op2.sign = (0, 0) |
| 1041 | else: |
| 1042 | result.sign = 0 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1043 | # Now, op1 > abs(op2) > 0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1044 | |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1045 | if op2.sign == 0: |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1046 | result.int = op1.int + op2.int |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1047 | else: |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1048 | result.int = op1.int - op2.int |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1049 | |
| 1050 | result.exp = op1.exp |
| 1051 | ans = Decimal(result) |
| 1052 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1053 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1054 | return ans |
| 1055 | |
| 1056 | __radd__ = __add__ |
| 1057 | |
| 1058 | def __sub__(self, other, context=None): |
| 1059 | """Return self + (-other)""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1060 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1061 | if other is NotImplemented: |
| 1062 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1063 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1064 | if self._is_special or other._is_special: |
| 1065 | ans = self._check_nans(other, context=context) |
| 1066 | if ans: |
| 1067 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1068 | |
| 1069 | # -Decimal(0) = Decimal(0), which we don't want since |
| 1070 | # (-0 - 0 = -0 + (-0) = -0, but -0 + 0 = 0.) |
| 1071 | # so we change the sign directly to a copy |
| 1072 | tmp = Decimal(other) |
| 1073 | tmp._sign = 1-tmp._sign |
| 1074 | |
| 1075 | return self.__add__(tmp, context=context) |
| 1076 | |
| 1077 | def __rsub__(self, other, context=None): |
| 1078 | """Return other + (-self)""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1079 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1080 | if other is NotImplemented: |
| 1081 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1082 | |
| 1083 | tmp = Decimal(self) |
| 1084 | tmp._sign = 1 - tmp._sign |
| 1085 | return other.__add__(tmp, context=context) |
| 1086 | |
| 1087 | def _increment(self, round=1, context=None): |
| 1088 | """Special case of add, adding 1eExponent |
| 1089 | |
| 1090 | Since it is common, (rounding, for example) this adds |
| 1091 | (sign)*one E self._exp to the number more efficiently than add. |
| 1092 | |
| 1093 | For example: |
| 1094 | Decimal('5.624e10')._increment() == Decimal('5.625e10') |
| 1095 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1096 | if self._is_special: |
| 1097 | ans = self._check_nans(context=context) |
| 1098 | if ans: |
| 1099 | return ans |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 1100 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1101 | # Must be infinite, and incrementing makes no difference |
| 1102 | return Decimal(self) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1103 | |
| 1104 | L = list(self._int) |
| 1105 | L[-1] += 1 |
| 1106 | spot = len(L)-1 |
| 1107 | while L[spot] == 10: |
| 1108 | L[spot] = 0 |
| 1109 | if spot == 0: |
| 1110 | L[0:0] = [1] |
| 1111 | break |
| 1112 | L[spot-1] += 1 |
| 1113 | spot -= 1 |
| 1114 | ans = Decimal((self._sign, L, self._exp)) |
| 1115 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1116 | if context is None: |
| 1117 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1118 | if round and context._rounding_decision == ALWAYS_ROUND: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1119 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1120 | return ans |
| 1121 | |
| 1122 | def __mul__(self, other, context=None): |
| 1123 | """Return self * other. |
| 1124 | |
| 1125 | (+-) INF * 0 (or its reverse) raise InvalidOperation. |
| 1126 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1127 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1128 | if other is NotImplemented: |
| 1129 | return other |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1130 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1131 | if context is None: |
| 1132 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1133 | |
Raymond Hettinger | d87ac8f | 2004-07-09 10:52:54 +0000 | [diff] [blame] | 1134 | resultsign = self._sign ^ other._sign |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1135 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1136 | if self._is_special or other._is_special: |
| 1137 | ans = self._check_nans(other, context) |
| 1138 | if ans: |
| 1139 | return ans |
| 1140 | |
| 1141 | if self._isinfinity(): |
| 1142 | if not other: |
| 1143 | return context._raise_error(InvalidOperation, '(+-)INF * 0') |
| 1144 | return Infsign[resultsign] |
| 1145 | |
| 1146 | if other._isinfinity(): |
| 1147 | if not self: |
| 1148 | return context._raise_error(InvalidOperation, '0 * (+-)INF') |
| 1149 | return Infsign[resultsign] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1150 | |
| 1151 | resultexp = self._exp + other._exp |
| 1152 | shouldround = context._rounding_decision == ALWAYS_ROUND |
| 1153 | |
| 1154 | # Special case for multiplying by zero |
| 1155 | if not self or not other: |
| 1156 | ans = Decimal((resultsign, (0,), resultexp)) |
| 1157 | if shouldround: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1158 | # Fixing in case the exponent is out of bounds |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1159 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1160 | return ans |
| 1161 | |
| 1162 | # Special case for multiplying by power of 10 |
| 1163 | if self._int == (1,): |
| 1164 | ans = Decimal((resultsign, other._int, resultexp)) |
| 1165 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1166 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1167 | return ans |
| 1168 | if other._int == (1,): |
| 1169 | ans = Decimal((resultsign, self._int, resultexp)) |
| 1170 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1171 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1172 | return ans |
| 1173 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1174 | op1 = _WorkRep(self) |
| 1175 | op2 = _WorkRep(other) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1176 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1177 | ans = Decimal((resultsign, map(int, str(op1.int * op2.int)), resultexp)) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1178 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1179 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1180 | |
| 1181 | return ans |
| 1182 | __rmul__ = __mul__ |
| 1183 | |
| 1184 | def __div__(self, other, context=None): |
| 1185 | """Return self / other.""" |
| 1186 | return self._divide(other, context=context) |
| 1187 | __truediv__ = __div__ |
| 1188 | |
| 1189 | def _divide(self, other, divmod = 0, context=None): |
| 1190 | """Return a / b, to context.prec precision. |
| 1191 | |
| 1192 | divmod: |
| 1193 | 0 => true division |
| 1194 | 1 => (a //b, a%b) |
| 1195 | 2 => a //b |
| 1196 | 3 => a%b |
| 1197 | |
| 1198 | Actually, if divmod is 2 or 3 a tuple is returned, but errors for |
| 1199 | computing the other value are not raised. |
| 1200 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1201 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1202 | if other is NotImplemented: |
| 1203 | if divmod in (0, 1): |
| 1204 | return NotImplemented |
| 1205 | return (NotImplemented, NotImplemented) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1206 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1207 | if context is None: |
| 1208 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1209 | |
Raymond Hettinger | d87ac8f | 2004-07-09 10:52:54 +0000 | [diff] [blame] | 1210 | sign = self._sign ^ other._sign |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1211 | |
| 1212 | if self._is_special or other._is_special: |
| 1213 | ans = self._check_nans(other, context) |
| 1214 | if ans: |
| 1215 | if divmod: |
| 1216 | return (ans, ans) |
| 1217 | return ans |
| 1218 | |
| 1219 | if self._isinfinity() and other._isinfinity(): |
| 1220 | if divmod: |
| 1221 | return (context._raise_error(InvalidOperation, |
| 1222 | '(+-)INF // (+-)INF'), |
| 1223 | context._raise_error(InvalidOperation, |
| 1224 | '(+-)INF % (+-)INF')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1225 | return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF') |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1226 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1227 | if self._isinfinity(): |
| 1228 | if divmod == 1: |
| 1229 | return (Infsign[sign], |
| 1230 | context._raise_error(InvalidOperation, 'INF % x')) |
| 1231 | elif divmod == 2: |
| 1232 | return (Infsign[sign], NaN) |
| 1233 | elif divmod == 3: |
| 1234 | return (Infsign[sign], |
| 1235 | context._raise_error(InvalidOperation, 'INF % x')) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1236 | return Infsign[sign] |
| 1237 | |
| 1238 | if other._isinfinity(): |
| 1239 | if divmod: |
| 1240 | return (Decimal((sign, (0,), 0)), Decimal(self)) |
| 1241 | context._raise_error(Clamped, 'Division by infinity') |
| 1242 | return Decimal((sign, (0,), context.Etiny())) |
| 1243 | |
| 1244 | # Special cases for zeroes |
| 1245 | if not self and not other: |
| 1246 | if divmod: |
| 1247 | return context._raise_error(DivisionUndefined, '0 / 0', 1) |
| 1248 | return context._raise_error(DivisionUndefined, '0 / 0') |
| 1249 | |
| 1250 | if not self: |
| 1251 | if divmod: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1252 | otherside = Decimal(self) |
| 1253 | otherside._exp = min(self._exp, other._exp) |
| 1254 | return (Decimal((sign, (0,), 0)), otherside) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1255 | exp = self._exp - other._exp |
| 1256 | if exp < context.Etiny(): |
| 1257 | exp = context.Etiny() |
| 1258 | context._raise_error(Clamped, '0e-x / y') |
| 1259 | if exp > context.Emax: |
| 1260 | exp = context.Emax |
| 1261 | context._raise_error(Clamped, '0e+x / y') |
| 1262 | return Decimal( (sign, (0,), exp) ) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1263 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1264 | if not other: |
| 1265 | if divmod: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1266 | return context._raise_error(DivisionByZero, 'divmod(x,0)', |
| 1267 | sign, 1) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1268 | return context._raise_error(DivisionByZero, 'x / 0', sign) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1269 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1270 | # OK, so neither = 0, INF or NaN |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1271 | shouldround = context._rounding_decision == ALWAYS_ROUND |
| 1272 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1273 | # If we're dividing into ints, and self < other, stop. |
| 1274 | # self.__abs__(0) does not round. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1275 | if divmod and (self.__abs__(0, context) < other.__abs__(0, context)): |
| 1276 | |
| 1277 | if divmod == 1 or divmod == 3: |
| 1278 | exp = min(self._exp, other._exp) |
| 1279 | ans2 = self._rescale(exp, context=context, watchexp=0) |
| 1280 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1281 | ans2 = ans2._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1282 | return (Decimal( (sign, (0,), 0) ), |
| 1283 | ans2) |
| 1284 | |
| 1285 | elif divmod == 2: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1286 | # Don't round the mod part, if we don't need it. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1287 | return (Decimal( (sign, (0,), 0) ), Decimal(self)) |
| 1288 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1289 | op1 = _WorkRep(self) |
| 1290 | op2 = _WorkRep(other) |
| 1291 | op1, op2, adjust = _adjust_coefficients(op1, op2) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1292 | res = _WorkRep( (sign, 0, (op1.exp - op2.exp)) ) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1293 | if divmod and res.exp > context.prec + 1: |
| 1294 | return context._raise_error(DivisionImpossible) |
| 1295 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1296 | prec_limit = 10 ** context.prec |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1297 | while 1: |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1298 | while op2.int <= op1.int: |
| 1299 | res.int += 1 |
| 1300 | op1.int -= op2.int |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1301 | if res.exp == 0 and divmod: |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1302 | if res.int >= prec_limit and shouldround: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1303 | return context._raise_error(DivisionImpossible) |
| 1304 | otherside = Decimal(op1) |
| 1305 | frozen = context._ignore_all_flags() |
| 1306 | |
| 1307 | exp = min(self._exp, other._exp) |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 1308 | otherside = otherside._rescale(exp, context=context, watchexp=0) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1309 | context._regard_flags(*frozen) |
| 1310 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1311 | otherside = otherside._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1312 | return (Decimal(res), otherside) |
| 1313 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1314 | if op1.int == 0 and adjust >= 0 and not divmod: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1315 | break |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1316 | if res.int >= prec_limit and shouldround: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1317 | if divmod: |
| 1318 | return context._raise_error(DivisionImpossible) |
| 1319 | shouldround=1 |
| 1320 | # Really, the answer is a bit higher, so adding a one to |
| 1321 | # the end will make sure the rounding is right. |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1322 | if op1.int != 0: |
| 1323 | res.int *= 10 |
| 1324 | res.int += 1 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1325 | res.exp -= 1 |
| 1326 | |
| 1327 | break |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1328 | res.int *= 10 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1329 | res.exp -= 1 |
| 1330 | adjust += 1 |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1331 | op1.int *= 10 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1332 | op1.exp -= 1 |
| 1333 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1334 | if res.exp == 0 and divmod and op2.int > op1.int: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1335 | # Solves an error in precision. Same as a previous block. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1336 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1337 | if res.int >= prec_limit and shouldround: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1338 | return context._raise_error(DivisionImpossible) |
| 1339 | otherside = Decimal(op1) |
| 1340 | frozen = context._ignore_all_flags() |
| 1341 | |
| 1342 | exp = min(self._exp, other._exp) |
| 1343 | otherside = otherside._rescale(exp, context=context) |
| 1344 | |
| 1345 | context._regard_flags(*frozen) |
| 1346 | |
| 1347 | return (Decimal(res), otherside) |
| 1348 | |
| 1349 | ans = Decimal(res) |
| 1350 | if shouldround: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1351 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1352 | return ans |
| 1353 | |
| 1354 | def __rdiv__(self, other, context=None): |
| 1355 | """Swaps self/other and returns __div__.""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1356 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1357 | if other is NotImplemented: |
| 1358 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1359 | return other.__div__(self, context=context) |
| 1360 | __rtruediv__ = __rdiv__ |
| 1361 | |
| 1362 | def __divmod__(self, other, context=None): |
| 1363 | """ |
| 1364 | (self // other, self % other) |
| 1365 | """ |
| 1366 | return self._divide(other, 1, context) |
| 1367 | |
| 1368 | def __rdivmod__(self, other, context=None): |
| 1369 | """Swaps self/other and returns __divmod__.""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1370 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1371 | if other is NotImplemented: |
| 1372 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1373 | return other.__divmod__(self, context=context) |
| 1374 | |
| 1375 | def __mod__(self, other, context=None): |
| 1376 | """ |
| 1377 | self % other |
| 1378 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1379 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1380 | if other is NotImplemented: |
| 1381 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1382 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1383 | if self._is_special or other._is_special: |
| 1384 | ans = self._check_nans(other, context) |
| 1385 | if ans: |
| 1386 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1387 | |
| 1388 | if self and not other: |
| 1389 | return context._raise_error(InvalidOperation, 'x % 0') |
| 1390 | |
| 1391 | return self._divide(other, 3, context)[1] |
| 1392 | |
| 1393 | def __rmod__(self, other, context=None): |
| 1394 | """Swaps self/other and returns __mod__.""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1395 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1396 | if other is NotImplemented: |
| 1397 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1398 | return other.__mod__(self, context=context) |
| 1399 | |
| 1400 | def remainder_near(self, other, context=None): |
| 1401 | """ |
| 1402 | Remainder nearest to 0- abs(remainder-near) <= other/2 |
| 1403 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1404 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1405 | if other is NotImplemented: |
| 1406 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1407 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1408 | if self._is_special or other._is_special: |
| 1409 | ans = self._check_nans(other, context) |
| 1410 | if ans: |
| 1411 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1412 | if self and not other: |
| 1413 | return context._raise_error(InvalidOperation, 'x % 0') |
| 1414 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1415 | if context is None: |
| 1416 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1417 | # If DivisionImpossible causes an error, do not leave Rounded/Inexact |
| 1418 | # ignored in the calling function. |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 1419 | context = context._shallow_copy() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1420 | flags = context._ignore_flags(Rounded, Inexact) |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1421 | # Keep DivisionImpossible flags |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1422 | (side, r) = self.__divmod__(other, context=context) |
| 1423 | |
| 1424 | if r._isnan(): |
| 1425 | context._regard_flags(*flags) |
| 1426 | return r |
| 1427 | |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 1428 | context = context._shallow_copy() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1429 | rounding = context._set_rounding_decision(NEVER_ROUND) |
| 1430 | |
| 1431 | if other._sign: |
| 1432 | comparison = other.__div__(Decimal(-2), context=context) |
| 1433 | else: |
| 1434 | comparison = other.__div__(Decimal(2), context=context) |
| 1435 | |
| 1436 | context._set_rounding_decision(rounding) |
| 1437 | context._regard_flags(*flags) |
| 1438 | |
| 1439 | s1, s2 = r._sign, comparison._sign |
| 1440 | r._sign, comparison._sign = 0, 0 |
| 1441 | |
| 1442 | if r < comparison: |
| 1443 | r._sign, comparison._sign = s1, s2 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1444 | # Get flags now |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1445 | self.__divmod__(other, context=context) |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1446 | return r._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1447 | r._sign, comparison._sign = s1, s2 |
| 1448 | |
| 1449 | rounding = context._set_rounding_decision(NEVER_ROUND) |
| 1450 | |
| 1451 | (side, r) = self.__divmod__(other, context=context) |
| 1452 | context._set_rounding_decision(rounding) |
| 1453 | if r._isnan(): |
| 1454 | return r |
| 1455 | |
| 1456 | decrease = not side._iseven() |
| 1457 | rounding = context._set_rounding_decision(NEVER_ROUND) |
| 1458 | side = side.__abs__(context=context) |
| 1459 | context._set_rounding_decision(rounding) |
| 1460 | |
| 1461 | s1, s2 = r._sign, comparison._sign |
| 1462 | r._sign, comparison._sign = 0, 0 |
| 1463 | if r > comparison or decrease and r == comparison: |
| 1464 | r._sign, comparison._sign = s1, s2 |
| 1465 | context.prec += 1 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1466 | numbsquant = len(side.__add__(Decimal(1), context=context)._int) |
| 1467 | if numbsquant >= context.prec: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1468 | context.prec -= 1 |
| 1469 | return context._raise_error(DivisionImpossible)[1] |
| 1470 | context.prec -= 1 |
| 1471 | if self._sign == other._sign: |
| 1472 | r = r.__sub__(other, context=context) |
| 1473 | else: |
| 1474 | r = r.__add__(other, context=context) |
| 1475 | else: |
| 1476 | r._sign, comparison._sign = s1, s2 |
| 1477 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1478 | return r._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1479 | |
| 1480 | def __floordiv__(self, other, context=None): |
| 1481 | """self // other""" |
| 1482 | return self._divide(other, 2, context)[0] |
| 1483 | |
| 1484 | def __rfloordiv__(self, other, context=None): |
| 1485 | """Swaps self/other and returns __floordiv__.""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1486 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1487 | if other is NotImplemented: |
| 1488 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1489 | return other.__floordiv__(self, context=context) |
| 1490 | |
| 1491 | def __float__(self): |
| 1492 | """Float representation.""" |
| 1493 | return float(str(self)) |
| 1494 | |
| 1495 | def __int__(self): |
Brett Cannon | 46b0802 | 2005-03-01 03:12:26 +0000 | [diff] [blame] | 1496 | """Converts self to an int, truncating if necessary.""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1497 | if self._is_special: |
| 1498 | if self._isnan(): |
| 1499 | context = getcontext() |
| 1500 | return context._raise_error(InvalidContext) |
| 1501 | elif self._isinfinity(): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1502 | raise OverflowError("Cannot convert infinity to long") |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1503 | if self._exp >= 0: |
Raymond Hettinger | 605ed02 | 2004-11-24 07:28:48 +0000 | [diff] [blame] | 1504 | s = ''.join(map(str, self._int)) + '0'*self._exp |
| 1505 | else: |
| 1506 | s = ''.join(map(str, self._int))[:self._exp] |
| 1507 | if s == '': |
| 1508 | s = '0' |
| 1509 | sign = '-'*self._sign |
| 1510 | return int(sign + s) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1511 | |
| 1512 | def __long__(self): |
| 1513 | """Converts to a long. |
| 1514 | |
| 1515 | Equivalent to long(int(self)) |
| 1516 | """ |
| 1517 | return long(self.__int__()) |
| 1518 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1519 | def _fix(self, context): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1520 | """Round if it is necessary to keep self within prec precision. |
| 1521 | |
| 1522 | Rounds and fixes the exponent. Does not raise on a sNaN. |
| 1523 | |
| 1524 | Arguments: |
| 1525 | self - Decimal instance |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1526 | context - context used. |
| 1527 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1528 | if self._is_special: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1529 | return self |
| 1530 | if context is None: |
| 1531 | context = getcontext() |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1532 | prec = context.prec |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1533 | ans = self._fixexponents(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1534 | if len(ans._int) > prec: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1535 | ans = ans._round(prec, context=context) |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1536 | ans = ans._fixexponents(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1537 | return ans |
| 1538 | |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1539 | def _fixexponents(self, context): |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1540 | """Fix the exponents and return a copy with the exponent in bounds. |
| 1541 | Only call if known to not be a special value. |
| 1542 | """ |
| 1543 | folddown = context._clamp |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1544 | Emin = context.Emin |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1545 | ans = self |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1546 | ans_adjusted = ans.adjusted() |
| 1547 | if ans_adjusted < Emin: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1548 | Etiny = context.Etiny() |
| 1549 | if ans._exp < Etiny: |
| 1550 | if not ans: |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1551 | ans = Decimal(self) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1552 | ans._exp = Etiny |
| 1553 | context._raise_error(Clamped) |
| 1554 | return ans |
| 1555 | ans = ans._rescale(Etiny, context=context) |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1556 | # It isn't zero, and exp < Emin => subnormal |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1557 | context._raise_error(Subnormal) |
| 1558 | if context.flags[Inexact]: |
| 1559 | context._raise_error(Underflow) |
| 1560 | else: |
| 1561 | if ans: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1562 | # Only raise subnormal if non-zero. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1563 | context._raise_error(Subnormal) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1564 | else: |
| 1565 | Etop = context.Etop() |
| 1566 | if folddown and ans._exp > Etop: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1567 | context._raise_error(Clamped) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1568 | ans = ans._rescale(Etop, context=context) |
| 1569 | else: |
| 1570 | Emax = context.Emax |
| 1571 | if ans_adjusted > Emax: |
| 1572 | if not ans: |
Facundo Batista | 99b5548 | 2004-10-26 23:38:46 +0000 | [diff] [blame] | 1573 | ans = Decimal(self) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1574 | ans._exp = Emax |
| 1575 | context._raise_error(Clamped) |
| 1576 | return ans |
| 1577 | context._raise_error(Inexact) |
| 1578 | context._raise_error(Rounded) |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1579 | c = context._raise_error(Overflow, 'above Emax', ans._sign) |
| 1580 | return c |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1581 | return ans |
| 1582 | |
| 1583 | def _round(self, prec=None, rounding=None, context=None): |
| 1584 | """Returns a rounded version of self. |
| 1585 | |
| 1586 | You can specify the precision or rounding method. Otherwise, the |
| 1587 | context determines it. |
| 1588 | """ |
| 1589 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1590 | if self._is_special: |
| 1591 | ans = self._check_nans(context=context) |
| 1592 | if ans: |
| 1593 | return ans |
| 1594 | |
| 1595 | if self._isinfinity(): |
| 1596 | return Decimal(self) |
| 1597 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1598 | if context is None: |
| 1599 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1600 | |
| 1601 | if rounding is None: |
| 1602 | rounding = context.rounding |
| 1603 | if prec is None: |
| 1604 | prec = context.prec |
| 1605 | |
| 1606 | if not self: |
| 1607 | if prec <= 0: |
| 1608 | dig = (0,) |
| 1609 | exp = len(self._int) - prec + self._exp |
| 1610 | else: |
| 1611 | dig = (0,) * prec |
| 1612 | exp = len(self._int) + self._exp - prec |
| 1613 | ans = Decimal((self._sign, dig, exp)) |
| 1614 | context._raise_error(Rounded) |
| 1615 | return ans |
| 1616 | |
| 1617 | if prec == 0: |
| 1618 | temp = Decimal(self) |
| 1619 | temp._int = (0,)+temp._int |
| 1620 | prec = 1 |
| 1621 | elif prec < 0: |
| 1622 | exp = self._exp + len(self._int) - prec - 1 |
| 1623 | temp = Decimal( (self._sign, (0, 1), exp)) |
| 1624 | prec = 1 |
| 1625 | else: |
| 1626 | temp = Decimal(self) |
| 1627 | |
| 1628 | numdigits = len(temp._int) |
| 1629 | if prec == numdigits: |
| 1630 | return temp |
| 1631 | |
| 1632 | # See if we need to extend precision |
| 1633 | expdiff = prec - numdigits |
| 1634 | if expdiff > 0: |
| 1635 | tmp = list(temp._int) |
| 1636 | tmp.extend([0] * expdiff) |
| 1637 | ans = Decimal( (temp._sign, tmp, temp._exp - expdiff)) |
| 1638 | return ans |
| 1639 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1640 | # OK, but maybe all the lost digits are 0. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1641 | lostdigits = self._int[expdiff:] |
| 1642 | if lostdigits == (0,) * len(lostdigits): |
| 1643 | ans = Decimal( (temp._sign, temp._int[:prec], temp._exp - expdiff)) |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1644 | # Rounded, but not Inexact |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1645 | context._raise_error(Rounded) |
| 1646 | return ans |
| 1647 | |
| 1648 | # Okay, let's round and lose data |
| 1649 | |
| 1650 | this_function = getattr(temp, self._pick_rounding_function[rounding]) |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1651 | # Now we've got the rounding function |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1652 | |
| 1653 | if prec != context.prec: |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 1654 | context = context._shallow_copy() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1655 | context.prec = prec |
| 1656 | ans = this_function(prec, expdiff, context) |
| 1657 | context._raise_error(Rounded) |
| 1658 | context._raise_error(Inexact, 'Changed in rounding') |
| 1659 | |
| 1660 | return ans |
| 1661 | |
| 1662 | _pick_rounding_function = {} |
| 1663 | |
| 1664 | def _round_down(self, prec, expdiff, context): |
| 1665 | """Also known as round-towards-0, truncate.""" |
| 1666 | return Decimal( (self._sign, self._int[:prec], self._exp - expdiff) ) |
| 1667 | |
| 1668 | def _round_half_up(self, prec, expdiff, context, tmp = None): |
| 1669 | """Rounds 5 up (away from 0)""" |
| 1670 | |
| 1671 | if tmp is None: |
| 1672 | tmp = Decimal( (self._sign,self._int[:prec], self._exp - expdiff)) |
| 1673 | if self._int[prec] >= 5: |
| 1674 | tmp = tmp._increment(round=0, context=context) |
| 1675 | if len(tmp._int) > prec: |
| 1676 | return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1)) |
| 1677 | return tmp |
| 1678 | |
| 1679 | def _round_half_even(self, prec, expdiff, context): |
| 1680 | """Round 5 to even, rest to nearest.""" |
| 1681 | |
| 1682 | tmp = Decimal( (self._sign, self._int[:prec], self._exp - expdiff)) |
| 1683 | half = (self._int[prec] == 5) |
| 1684 | if half: |
| 1685 | for digit in self._int[prec+1:]: |
| 1686 | if digit != 0: |
| 1687 | half = 0 |
| 1688 | break |
| 1689 | if half: |
Raymond Hettinger | 61992ef | 2004-08-06 23:42:16 +0000 | [diff] [blame] | 1690 | if self._int[prec-1] & 1 == 0: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1691 | return tmp |
| 1692 | return self._round_half_up(prec, expdiff, context, tmp) |
| 1693 | |
| 1694 | def _round_half_down(self, prec, expdiff, context): |
| 1695 | """Round 5 down""" |
| 1696 | |
| 1697 | tmp = Decimal( (self._sign, self._int[:prec], self._exp - expdiff)) |
| 1698 | half = (self._int[prec] == 5) |
| 1699 | if half: |
| 1700 | for digit in self._int[prec+1:]: |
| 1701 | if digit != 0: |
| 1702 | half = 0 |
| 1703 | break |
| 1704 | if half: |
| 1705 | return tmp |
| 1706 | return self._round_half_up(prec, expdiff, context, tmp) |
| 1707 | |
| 1708 | def _round_up(self, prec, expdiff, context): |
| 1709 | """Rounds away from 0.""" |
| 1710 | tmp = Decimal( (self._sign, self._int[:prec], self._exp - expdiff) ) |
| 1711 | for digit in self._int[prec:]: |
| 1712 | if digit != 0: |
| 1713 | tmp = tmp._increment(round=1, context=context) |
| 1714 | if len(tmp._int) > prec: |
| 1715 | return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1)) |
| 1716 | else: |
| 1717 | return tmp |
| 1718 | return tmp |
| 1719 | |
| 1720 | def _round_ceiling(self, prec, expdiff, context): |
| 1721 | """Rounds up (not away from 0 if negative.)""" |
| 1722 | if self._sign: |
| 1723 | return self._round_down(prec, expdiff, context) |
| 1724 | else: |
| 1725 | return self._round_up(prec, expdiff, context) |
| 1726 | |
| 1727 | def _round_floor(self, prec, expdiff, context): |
| 1728 | """Rounds down (not towards 0 if negative)""" |
| 1729 | if not self._sign: |
| 1730 | return self._round_down(prec, expdiff, context) |
| 1731 | else: |
| 1732 | return self._round_up(prec, expdiff, context) |
| 1733 | |
| 1734 | def __pow__(self, n, modulo = None, context=None): |
| 1735 | """Return self ** n (mod modulo) |
| 1736 | |
| 1737 | If modulo is None (default), don't take it mod modulo. |
| 1738 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1739 | n = _convert_other(n) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1740 | if n is NotImplemented: |
| 1741 | return n |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1742 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1743 | if context is None: |
| 1744 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1745 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1746 | if self._is_special or n._is_special or n.adjusted() > 8: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1747 | # Because the spot << doesn't work with really big exponents |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1748 | if n._isinfinity() or n.adjusted() > 8: |
| 1749 | return context._raise_error(InvalidOperation, 'x ** INF') |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1750 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1751 | ans = self._check_nans(n, context) |
| 1752 | if ans: |
| 1753 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1754 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1755 | if not n._isinteger(): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1756 | return context._raise_error(InvalidOperation, 'x ** (non-integer)') |
| 1757 | |
| 1758 | if not self and not n: |
| 1759 | return context._raise_error(InvalidOperation, '0 ** 0') |
| 1760 | |
| 1761 | if not n: |
| 1762 | return Decimal(1) |
| 1763 | |
| 1764 | if self == Decimal(1): |
| 1765 | return Decimal(1) |
| 1766 | |
| 1767 | sign = self._sign and not n._iseven() |
| 1768 | n = int(n) |
| 1769 | |
| 1770 | if self._isinfinity(): |
| 1771 | if modulo: |
| 1772 | return context._raise_error(InvalidOperation, 'INF % x') |
| 1773 | if n > 0: |
| 1774 | return Infsign[sign] |
| 1775 | return Decimal( (sign, (0,), 0) ) |
| 1776 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1777 | # With ludicrously large exponent, just raise an overflow |
| 1778 | # and return inf. |
| 1779 | if not modulo and n > 0 and \ |
| 1780 | (self._exp + len(self._int) - 1) * n > context.Emax and self: |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 1781 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1782 | tmp = Decimal('inf') |
| 1783 | tmp._sign = sign |
| 1784 | context._raise_error(Rounded) |
| 1785 | context._raise_error(Inexact) |
| 1786 | context._raise_error(Overflow, 'Big power', sign) |
| 1787 | return tmp |
| 1788 | |
| 1789 | elength = len(str(abs(n))) |
| 1790 | firstprec = context.prec |
| 1791 | |
Raymond Hettinger | 99148e7 | 2004-07-14 19:56:56 +0000 | [diff] [blame] | 1792 | if not modulo and firstprec + elength + 1 > DefaultContext.Emax: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1793 | return context._raise_error(Overflow, 'Too much precision.', sign) |
| 1794 | |
| 1795 | mul = Decimal(self) |
| 1796 | val = Decimal(1) |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 1797 | context = context._shallow_copy() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1798 | context.prec = firstprec + elength + 1 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1799 | if n < 0: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1800 | # n is a long now, not Decimal instance |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1801 | n = -n |
| 1802 | mul = Decimal(1).__div__(mul, context=context) |
| 1803 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1804 | spot = 1 |
| 1805 | while spot <= n: |
| 1806 | spot <<= 1 |
| 1807 | |
| 1808 | spot >>= 1 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1809 | # spot is the highest power of 2 less than n |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1810 | while spot: |
| 1811 | val = val.__mul__(val, context=context) |
| 1812 | if val._isinfinity(): |
| 1813 | val = Infsign[sign] |
| 1814 | break |
| 1815 | if spot & n: |
| 1816 | val = val.__mul__(mul, context=context) |
| 1817 | if modulo is not None: |
| 1818 | val = val.__mod__(modulo, context=context) |
| 1819 | spot >>= 1 |
| 1820 | context.prec = firstprec |
| 1821 | |
Raymond Hettinger | 76e60d6 | 2004-10-20 06:58:28 +0000 | [diff] [blame] | 1822 | if context._rounding_decision == ALWAYS_ROUND: |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1823 | return val._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1824 | return val |
| 1825 | |
| 1826 | def __rpow__(self, other, context=None): |
| 1827 | """Swaps self/other and returns __pow__.""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1828 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 1829 | if other is NotImplemented: |
| 1830 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1831 | return other.__pow__(self, context=context) |
| 1832 | |
| 1833 | def normalize(self, context=None): |
| 1834 | """Normalize- strip trailing 0s, change anything equal to 0 to 0e0""" |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1835 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1836 | if self._is_special: |
| 1837 | ans = self._check_nans(context=context) |
| 1838 | if ans: |
| 1839 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1840 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1841 | dup = self._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1842 | if dup._isinfinity(): |
| 1843 | return dup |
| 1844 | |
| 1845 | if not dup: |
| 1846 | return Decimal( (dup._sign, (0,), 0) ) |
| 1847 | end = len(dup._int) |
| 1848 | exp = dup._exp |
| 1849 | while dup._int[end-1] == 0: |
| 1850 | exp += 1 |
| 1851 | end -= 1 |
| 1852 | return Decimal( (dup._sign, dup._int[:end], exp) ) |
| 1853 | |
| 1854 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1855 | def quantize(self, exp, rounding=None, context=None, watchexp=1): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1856 | """Quantize self so its exponent is the same as that of exp. |
| 1857 | |
| 1858 | Similar to self._rescale(exp._exp) but with error checking. |
| 1859 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1860 | if self._is_special or exp._is_special: |
| 1861 | ans = self._check_nans(exp, context) |
| 1862 | if ans: |
| 1863 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1864 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1865 | if exp._isinfinity() or self._isinfinity(): |
| 1866 | if exp._isinfinity() and self._isinfinity(): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1867 | return self # if both are inf, it is OK |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1868 | if context is None: |
| 1869 | context = getcontext() |
| 1870 | return context._raise_error(InvalidOperation, |
| 1871 | 'quantize with one INF') |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1872 | return self._rescale(exp._exp, rounding, context, watchexp) |
| 1873 | |
| 1874 | def same_quantum(self, other): |
| 1875 | """Test whether self and other have the same exponent. |
| 1876 | |
| 1877 | same as self._exp == other._exp, except NaN == sNaN |
| 1878 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1879 | if self._is_special or other._is_special: |
| 1880 | if self._isnan() or other._isnan(): |
| 1881 | return self._isnan() and other._isnan() and True |
| 1882 | if self._isinfinity() or other._isinfinity(): |
| 1883 | return self._isinfinity() and other._isinfinity() and True |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1884 | return self._exp == other._exp |
| 1885 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1886 | def _rescale(self, exp, rounding=None, context=None, watchexp=1): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1887 | """Rescales so that the exponent is exp. |
| 1888 | |
| 1889 | exp = exp to scale to (an integer) |
| 1890 | rounding = rounding version |
| 1891 | watchexp: if set (default) an error is returned if exp is greater |
| 1892 | than Emax or less than Etiny. |
| 1893 | """ |
| 1894 | if context is None: |
| 1895 | context = getcontext() |
| 1896 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1897 | if self._is_special: |
| 1898 | if self._isinfinity(): |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 1899 | return context._raise_error(InvalidOperation, 'rescale with an INF') |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1900 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1901 | ans = self._check_nans(context=context) |
| 1902 | if ans: |
| 1903 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1904 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1905 | if watchexp and (context.Emax < exp or context.Etiny() > exp): |
| 1906 | return context._raise_error(InvalidOperation, 'rescale(a, INF)') |
| 1907 | |
| 1908 | if not self: |
| 1909 | ans = Decimal(self) |
| 1910 | ans._int = (0,) |
| 1911 | ans._exp = exp |
| 1912 | return ans |
| 1913 | |
| 1914 | diff = self._exp - exp |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1915 | digits = len(self._int) + diff |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1916 | |
| 1917 | if watchexp and digits > context.prec: |
| 1918 | return context._raise_error(InvalidOperation, 'Rescale > prec') |
| 1919 | |
| 1920 | tmp = Decimal(self) |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1921 | tmp._int = (0,) + tmp._int |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1922 | digits += 1 |
| 1923 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1924 | if digits < 0: |
| 1925 | tmp._exp = -digits + tmp._exp |
| 1926 | tmp._int = (0,1) |
| 1927 | digits = 1 |
| 1928 | tmp = tmp._round(digits, rounding, context=context) |
| 1929 | |
| 1930 | if tmp._int[0] == 0 and len(tmp._int) > 1: |
| 1931 | tmp._int = tmp._int[1:] |
| 1932 | tmp._exp = exp |
| 1933 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1934 | tmp_adjusted = tmp.adjusted() |
| 1935 | if tmp and tmp_adjusted < context.Emin: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1936 | context._raise_error(Subnormal) |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1937 | elif tmp and tmp_adjusted > context.Emax: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1938 | return context._raise_error(InvalidOperation, 'rescale(a, INF)') |
| 1939 | return tmp |
| 1940 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 1941 | def to_integral(self, rounding=None, context=None): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1942 | """Rounds to the nearest integer, without raising inexact, rounded.""" |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1943 | if self._is_special: |
| 1944 | ans = self._check_nans(context=context) |
| 1945 | if ans: |
| 1946 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1947 | if self._exp >= 0: |
| 1948 | return self |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1949 | if context is None: |
| 1950 | context = getcontext() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1951 | flags = context._ignore_flags(Rounded, Inexact) |
| 1952 | ans = self._rescale(0, rounding, context=context) |
| 1953 | context._regard_flags(flags) |
| 1954 | return ans |
| 1955 | |
| 1956 | def sqrt(self, context=None): |
| 1957 | """Return the square root of self. |
| 1958 | |
| 1959 | Uses a converging algorithm (Xn+1 = 0.5*(Xn + self / Xn)) |
| 1960 | Should quadratically approach the right answer. |
| 1961 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1962 | if self._is_special: |
| 1963 | ans = self._check_nans(context=context) |
| 1964 | if ans: |
| 1965 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1966 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1967 | if self._isinfinity() and self._sign == 0: |
| 1968 | return Decimal(self) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1969 | |
| 1970 | if not self: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1971 | # exponent = self._exp / 2, using round_down. |
| 1972 | # if self._exp < 0: |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 1973 | # exp = (self._exp+1) // 2 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1974 | # else: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1975 | exp = (self._exp) // 2 |
| 1976 | if self._sign == 1: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 1977 | # sqrt(-0) = -0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1978 | return Decimal( (1, (0,), exp)) |
| 1979 | else: |
| 1980 | return Decimal( (0, (0,), exp)) |
| 1981 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 1982 | if context is None: |
| 1983 | context = getcontext() |
| 1984 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1985 | if self._sign == 1: |
| 1986 | return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0') |
| 1987 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1988 | tmp = Decimal(self) |
| 1989 | |
Raymond Hettinger | 4837a22 | 2004-09-27 14:23:40 +0000 | [diff] [blame] | 1990 | expadd = tmp._exp // 2 |
Raymond Hettinger | 61992ef | 2004-08-06 23:42:16 +0000 | [diff] [blame] | 1991 | if tmp._exp & 1: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1992 | tmp._int += (0,) |
| 1993 | tmp._exp = 0 |
| 1994 | else: |
| 1995 | tmp._exp = 0 |
| 1996 | |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 1997 | context = context._shallow_copy() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 1998 | flags = context._ignore_all_flags() |
| 1999 | firstprec = context.prec |
| 2000 | context.prec = 3 |
Raymond Hettinger | 61992ef | 2004-08-06 23:42:16 +0000 | [diff] [blame] | 2001 | if tmp.adjusted() & 1 == 0: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2002 | ans = Decimal( (0, (8,1,9), tmp.adjusted() - 2) ) |
| 2003 | ans = ans.__add__(tmp.__mul__(Decimal((0, (2,5,9), -2)), |
| 2004 | context=context), context=context) |
Raymond Hettinger | 4837a22 | 2004-09-27 14:23:40 +0000 | [diff] [blame] | 2005 | ans._exp -= 1 + tmp.adjusted() // 2 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2006 | else: |
| 2007 | ans = Decimal( (0, (2,5,9), tmp._exp + len(tmp._int)- 3) ) |
| 2008 | ans = ans.__add__(tmp.__mul__(Decimal((0, (8,1,9), -3)), |
| 2009 | context=context), context=context) |
Raymond Hettinger | 4837a22 | 2004-09-27 14:23:40 +0000 | [diff] [blame] | 2010 | ans._exp -= 1 + tmp.adjusted() // 2 |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2011 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2012 | # ans is now a linear approximation. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2013 | Emax, Emin = context.Emax, context.Emin |
Raymond Hettinger | 99148e7 | 2004-07-14 19:56:56 +0000 | [diff] [blame] | 2014 | context.Emax, context.Emin = DefaultContext.Emax, DefaultContext.Emin |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2015 | |
| 2016 | half = Decimal('0.5') |
| 2017 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2018 | maxp = firstprec + 2 |
| 2019 | rounding = context._set_rounding(ROUND_HALF_EVEN) |
| 2020 | while 1: |
| 2021 | context.prec = min(2*context.prec - 2, maxp) |
| 2022 | ans = half.__mul__(ans.__add__(tmp.__div__(ans, context=context), |
| 2023 | context=context), context=context) |
| 2024 | if context.prec == maxp: |
| 2025 | break |
| 2026 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2027 | # Round to the answer's precision-- the only error can be 1 ulp. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2028 | context.prec = firstprec |
| 2029 | prevexp = ans.adjusted() |
| 2030 | ans = ans._round(context=context) |
| 2031 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2032 | # Now, check if the other last digits are better. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2033 | context.prec = firstprec + 1 |
| 2034 | # In case we rounded up another digit and we should actually go lower. |
| 2035 | if prevexp != ans.adjusted(): |
| 2036 | ans._int += (0,) |
| 2037 | ans._exp -= 1 |
| 2038 | |
| 2039 | |
| 2040 | lower = ans.__sub__(Decimal((0, (5,), ans._exp-1)), context=context) |
| 2041 | context._set_rounding(ROUND_UP) |
| 2042 | if lower.__mul__(lower, context=context) > (tmp): |
| 2043 | ans = ans.__sub__(Decimal((0, (1,), ans._exp)), context=context) |
| 2044 | |
| 2045 | else: |
| 2046 | upper = ans.__add__(Decimal((0, (5,), ans._exp-1)),context=context) |
| 2047 | context._set_rounding(ROUND_DOWN) |
| 2048 | if upper.__mul__(upper, context=context) < tmp: |
| 2049 | ans = ans.__add__(Decimal((0, (1,), ans._exp)),context=context) |
| 2050 | |
| 2051 | ans._exp += expadd |
| 2052 | |
| 2053 | context.prec = firstprec |
| 2054 | context.rounding = rounding |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 2055 | ans = ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2056 | |
| 2057 | rounding = context._set_rounding_decision(NEVER_ROUND) |
| 2058 | if not ans.__mul__(ans, context=context) == self: |
| 2059 | # Only rounded/inexact if here. |
| 2060 | context._regard_flags(flags) |
| 2061 | context._raise_error(Rounded) |
| 2062 | context._raise_error(Inexact) |
| 2063 | else: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2064 | # Exact answer, so let's set the exponent right. |
| 2065 | # if self._exp < 0: |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2066 | # exp = (self._exp +1)// 2 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2067 | # else: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2068 | exp = self._exp // 2 |
| 2069 | context.prec += ans._exp - exp |
| 2070 | ans = ans._rescale(exp, context=context) |
| 2071 | context.prec = firstprec |
| 2072 | context._regard_flags(flags) |
| 2073 | context.Emax, context.Emin = Emax, Emin |
| 2074 | |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 2075 | return ans._fix(context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2076 | |
| 2077 | def max(self, other, context=None): |
| 2078 | """Returns the larger value. |
| 2079 | |
| 2080 | like max(self, other) except if one is not a number, returns |
| 2081 | NaN (and signals if one is sNaN). Also rounds. |
| 2082 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2083 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 2084 | if other is NotImplemented: |
| 2085 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2086 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2087 | if self._is_special or other._is_special: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2088 | # If one operand is a quiet NaN and the other is number, then the |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2089 | # number is always returned |
| 2090 | sn = self._isnan() |
| 2091 | on = other._isnan() |
| 2092 | if sn or on: |
| 2093 | if on == 1 and sn != 2: |
| 2094 | return self |
| 2095 | if sn == 1 and on != 2: |
| 2096 | return other |
| 2097 | return self._check_nans(other, context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2098 | |
| 2099 | ans = self |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2100 | c = self.__cmp__(other) |
| 2101 | if c == 0: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2102 | # If both operands are finite and equal in numerical value |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2103 | # then an ordering is applied: |
| 2104 | # |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2105 | # If the signs differ then max returns the operand with the |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2106 | # positive sign and min returns the operand with the negative sign |
| 2107 | # |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2108 | # If the signs are the same then the exponent is used to select |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2109 | # the result. |
| 2110 | if self._sign != other._sign: |
| 2111 | if self._sign: |
| 2112 | ans = other |
| 2113 | elif self._exp < other._exp and not self._sign: |
| 2114 | ans = other |
| 2115 | elif self._exp > other._exp and self._sign: |
| 2116 | ans = other |
| 2117 | elif c == -1: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2118 | ans = other |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2119 | |
| 2120 | if context is None: |
| 2121 | context = getcontext() |
Raymond Hettinger | 76e60d6 | 2004-10-20 06:58:28 +0000 | [diff] [blame] | 2122 | if context._rounding_decision == ALWAYS_ROUND: |
| 2123 | return ans._fix(context) |
| 2124 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2125 | |
| 2126 | def min(self, other, context=None): |
| 2127 | """Returns the smaller value. |
| 2128 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2129 | Like min(self, other) except if one is not a number, returns |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2130 | NaN (and signals if one is sNaN). Also rounds. |
| 2131 | """ |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2132 | other = _convert_other(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 2133 | if other is NotImplemented: |
| 2134 | return other |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2135 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2136 | if self._is_special or other._is_special: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2137 | # If one operand is a quiet NaN and the other is number, then the |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2138 | # number is always returned |
| 2139 | sn = self._isnan() |
| 2140 | on = other._isnan() |
| 2141 | if sn or on: |
| 2142 | if on == 1 and sn != 2: |
| 2143 | return self |
| 2144 | if sn == 1 and on != 2: |
| 2145 | return other |
| 2146 | return self._check_nans(other, context) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2147 | |
| 2148 | ans = self |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2149 | c = self.__cmp__(other) |
| 2150 | if c == 0: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2151 | # If both operands are finite and equal in numerical value |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2152 | # then an ordering is applied: |
| 2153 | # |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2154 | # If the signs differ then max returns the operand with the |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2155 | # positive sign and min returns the operand with the negative sign |
| 2156 | # |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2157 | # If the signs are the same then the exponent is used to select |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2158 | # the result. |
| 2159 | if self._sign != other._sign: |
| 2160 | if other._sign: |
| 2161 | ans = other |
| 2162 | elif self._exp > other._exp and not self._sign: |
| 2163 | ans = other |
| 2164 | elif self._exp < other._exp and self._sign: |
| 2165 | ans = other |
| 2166 | elif c == 1: |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2167 | ans = other |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2168 | |
| 2169 | if context is None: |
| 2170 | context = getcontext() |
Raymond Hettinger | 76e60d6 | 2004-10-20 06:58:28 +0000 | [diff] [blame] | 2171 | if context._rounding_decision == ALWAYS_ROUND: |
| 2172 | return ans._fix(context) |
| 2173 | return ans |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2174 | |
| 2175 | def _isinteger(self): |
| 2176 | """Returns whether self is an integer""" |
| 2177 | if self._exp >= 0: |
| 2178 | return True |
| 2179 | rest = self._int[self._exp:] |
| 2180 | return rest == (0,)*len(rest) |
| 2181 | |
| 2182 | def _iseven(self): |
| 2183 | """Returns 1 if self is even. Assumes self is an integer.""" |
| 2184 | if self._exp > 0: |
| 2185 | return 1 |
Raymond Hettinger | 61992ef | 2004-08-06 23:42:16 +0000 | [diff] [blame] | 2186 | return self._int[-1+self._exp] & 1 == 0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2187 | |
| 2188 | def adjusted(self): |
| 2189 | """Return the adjusted exponent of self""" |
| 2190 | try: |
| 2191 | return self._exp + len(self._int) - 1 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2192 | # If NaN or Infinity, self._exp is string |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2193 | except TypeError: |
| 2194 | return 0 |
| 2195 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2196 | # Support for pickling, copy, and deepcopy |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2197 | def __reduce__(self): |
| 2198 | return (self.__class__, (str(self),)) |
| 2199 | |
| 2200 | def __copy__(self): |
| 2201 | if type(self) == Decimal: |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2202 | return self # I'm immutable; therefore I am my own clone |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2203 | return self.__class__(str(self)) |
| 2204 | |
| 2205 | def __deepcopy__(self, memo): |
| 2206 | if type(self) == Decimal: |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2207 | return self # My components are also immutable |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2208 | return self.__class__(str(self)) |
| 2209 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2210 | ##### Context class ####################################################### |
Raymond Hettinger | d9c0a7a | 2004-07-03 10:02:28 +0000 | [diff] [blame] | 2211 | |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2212 | |
| 2213 | # get rounding method function: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2214 | rounding_functions = [name for name in Decimal.__dict__.keys() |
| 2215 | if name.startswith('_round_')] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2216 | for name in rounding_functions: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2217 | # name is like _round_half_even, goes to the global ROUND_HALF_EVEN value. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2218 | globalname = name[1:].upper() |
| 2219 | val = globals()[globalname] |
| 2220 | Decimal._pick_rounding_function[val] = name |
| 2221 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2222 | del name, val, globalname, rounding_functions |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2223 | |
Nick Coghlan | ced1218 | 2006-09-02 03:54:17 +0000 | [diff] [blame] | 2224 | class _ContextManager(object): |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 2225 | """Context manager class to support localcontext(). |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2226 | |
Nick Coghlan | ced1218 | 2006-09-02 03:54:17 +0000 | [diff] [blame] | 2227 | Sets a copy of the supplied context in __enter__() and restores |
Nick Coghlan | 8b6999b | 2006-08-31 12:00:43 +0000 | [diff] [blame] | 2228 | the previous decimal context in __exit__() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2229 | """ |
| 2230 | def __init__(self, new_context): |
Nick Coghlan | ced1218 | 2006-09-02 03:54:17 +0000 | [diff] [blame] | 2231 | self.new_context = new_context.copy() |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2232 | def __enter__(self): |
| 2233 | self.saved_context = getcontext() |
| 2234 | setcontext(self.new_context) |
| 2235 | return self.new_context |
| 2236 | def __exit__(self, t, v, tb): |
| 2237 | setcontext(self.saved_context) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2238 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2239 | class Context(object): |
| 2240 | """Contains the context for a Decimal instance. |
| 2241 | |
| 2242 | Contains: |
| 2243 | prec - precision (for use in rounding, division, square roots..) |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2244 | rounding - rounding type (how you round) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2245 | _rounding_decision - ALWAYS_ROUND, NEVER_ROUND -- do you round? |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 2246 | traps - If traps[exception] = 1, then the exception is |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2247 | raised when it is caused. Otherwise, a value is |
| 2248 | substituted in. |
| 2249 | flags - When an exception is caused, flags[exception] is incremented. |
| 2250 | (Whether or not the trap_enabler is set) |
| 2251 | Should be reset by user of Decimal instance. |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 2252 | Emin - Minimum exponent |
| 2253 | Emax - Maximum exponent |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2254 | capitals - If 1, 1*10^1 is printed as 1E+1. |
| 2255 | If 0, printed as 1e1 |
Raymond Hettinger | e0f1581 | 2004-07-05 05:36:39 +0000 | [diff] [blame] | 2256 | _clamp - If 1, change exponents if too high (Default 0) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2257 | """ |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2258 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2259 | def __init__(self, prec=None, rounding=None, |
Raymond Hettinger | abf8a56 | 2004-10-12 09:12:16 +0000 | [diff] [blame] | 2260 | traps=None, flags=None, |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2261 | _rounding_decision=None, |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 2262 | Emin=None, Emax=None, |
Raymond Hettinger | e0f1581 | 2004-07-05 05:36:39 +0000 | [diff] [blame] | 2263 | capitals=None, _clamp=0, |
Raymond Hettinger | abf8a56 | 2004-10-12 09:12:16 +0000 | [diff] [blame] | 2264 | _ignored_flags=None): |
| 2265 | if flags is None: |
| 2266 | flags = [] |
| 2267 | if _ignored_flags is None: |
| 2268 | _ignored_flags = [] |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 2269 | if not isinstance(flags, dict): |
Raymond Hettinger | fed5296 | 2004-07-14 15:41:57 +0000 | [diff] [blame] | 2270 | flags = dict([(s,s in flags) for s in _signals]) |
Raymond Hettinger | b91af52 | 2004-07-14 16:35:30 +0000 | [diff] [blame] | 2271 | del s |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 2272 | if traps is not None and not isinstance(traps, dict): |
Raymond Hettinger | fed5296 | 2004-07-14 15:41:57 +0000 | [diff] [blame] | 2273 | traps = dict([(s,s in traps) for s in _signals]) |
Raymond Hettinger | b91af52 | 2004-07-14 16:35:30 +0000 | [diff] [blame] | 2274 | del s |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2275 | for name, val in locals().items(): |
| 2276 | if val is None: |
Raymond Hettinger | eb26084 | 2005-06-07 18:52:34 +0000 | [diff] [blame] | 2277 | setattr(self, name, _copy.copy(getattr(DefaultContext, name))) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2278 | else: |
| 2279 | setattr(self, name, val) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2280 | del self.self |
| 2281 | |
Raymond Hettinger | b1b605e | 2004-07-04 01:55:39 +0000 | [diff] [blame] | 2282 | def __repr__(self): |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 2283 | """Show the current context.""" |
Raymond Hettinger | b1b605e | 2004-07-04 01:55:39 +0000 | [diff] [blame] | 2284 | s = [] |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2285 | s.append('Context(prec=%(prec)d, rounding=%(rounding)s, ' |
| 2286 | 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d' |
| 2287 | % vars(self)) |
| 2288 | names = [f.__name__ for f, v in self.flags.items() if v] |
| 2289 | s.append('flags=[' + ', '.join(names) + ']') |
| 2290 | names = [t.__name__ for t, v in self.traps.items() if v] |
| 2291 | s.append('traps=[' + ', '.join(names) + ']') |
Raymond Hettinger | b1b605e | 2004-07-04 01:55:39 +0000 | [diff] [blame] | 2292 | return ', '.join(s) + ')' |
| 2293 | |
Raymond Hettinger | d9c0a7a | 2004-07-03 10:02:28 +0000 | [diff] [blame] | 2294 | def clear_flags(self): |
| 2295 | """Reset all flags to zero""" |
| 2296 | for flag in self.flags: |
Raymond Hettinger | b1b605e | 2004-07-04 01:55:39 +0000 | [diff] [blame] | 2297 | self.flags[flag] = 0 |
Raymond Hettinger | d9c0a7a | 2004-07-03 10:02:28 +0000 | [diff] [blame] | 2298 | |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 2299 | def _shallow_copy(self): |
| 2300 | """Returns a shallow copy from self.""" |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 2301 | nc = Context(self.prec, self.rounding, self.traps, self.flags, |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2302 | self._rounding_decision, self.Emin, self.Emax, |
| 2303 | self.capitals, self._clamp, self._ignored_flags) |
| 2304 | return nc |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 2305 | |
| 2306 | def copy(self): |
| 2307 | """Returns a deep copy from self.""" |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2308 | nc = Context(self.prec, self.rounding, self.traps.copy(), |
| 2309 | self.flags.copy(), self._rounding_decision, self.Emin, |
| 2310 | self.Emax, self.capitals, self._clamp, self._ignored_flags) |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 2311 | return nc |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2312 | __copy__ = copy |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2313 | |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 2314 | def _raise_error(self, condition, explanation = None, *args): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2315 | """Handles an error |
| 2316 | |
| 2317 | If the flag is in _ignored_flags, returns the default response. |
| 2318 | Otherwise, it increments the flag, then, if the corresponding |
| 2319 | trap_enabler is set, it reaises the exception. Otherwise, it returns |
| 2320 | the default value after incrementing the flag. |
| 2321 | """ |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 2322 | error = _condition_map.get(condition, condition) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2323 | if error in self._ignored_flags: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2324 | # Don't touch the flag |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2325 | return error().handle(self, *args) |
| 2326 | |
| 2327 | self.flags[error] += 1 |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 2328 | if not self.traps[error]: |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2329 | # The errors define how to handle themselves. |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 2330 | return condition().handle(self, *args) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2331 | |
| 2332 | # Errors should only be risked on copies of the context |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2333 | # self._ignored_flags = [] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2334 | raise error, explanation |
| 2335 | |
| 2336 | def _ignore_all_flags(self): |
| 2337 | """Ignore all flags, if they are raised""" |
Raymond Hettinger | fed5296 | 2004-07-14 15:41:57 +0000 | [diff] [blame] | 2338 | return self._ignore_flags(*_signals) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2339 | |
| 2340 | def _ignore_flags(self, *flags): |
| 2341 | """Ignore the flags, if they are raised""" |
| 2342 | # Do not mutate-- This way, copies of a context leave the original |
| 2343 | # alone. |
| 2344 | self._ignored_flags = (self._ignored_flags + list(flags)) |
| 2345 | return list(flags) |
| 2346 | |
| 2347 | def _regard_flags(self, *flags): |
| 2348 | """Stop ignoring the flags, if they are raised""" |
| 2349 | if flags and isinstance(flags[0], (tuple,list)): |
| 2350 | flags = flags[0] |
| 2351 | for flag in flags: |
| 2352 | self._ignored_flags.remove(flag) |
| 2353 | |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 2354 | def __hash__(self): |
| 2355 | """A Context cannot be hashed.""" |
| 2356 | # We inherit object.__hash__, so we must deny this explicitly |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2357 | raise TypeError("Cannot hash a Context.") |
Raymond Hettinger | 5aa478b | 2004-07-09 10:02:53 +0000 | [diff] [blame] | 2358 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2359 | def Etiny(self): |
| 2360 | """Returns Etiny (= Emin - prec + 1)""" |
| 2361 | return int(self.Emin - self.prec + 1) |
| 2362 | |
| 2363 | def Etop(self): |
Raymond Hettinger | e0f1581 | 2004-07-05 05:36:39 +0000 | [diff] [blame] | 2364 | """Returns maximum exponent (= Emax - prec + 1)""" |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2365 | return int(self.Emax - self.prec + 1) |
| 2366 | |
| 2367 | def _set_rounding_decision(self, type): |
| 2368 | """Sets the rounding decision. |
| 2369 | |
| 2370 | Sets the rounding decision, and returns the current (previous) |
| 2371 | rounding decision. Often used like: |
| 2372 | |
Raymond Hettinger | 9fce44b | 2004-08-08 04:03:24 +0000 | [diff] [blame] | 2373 | context = context._shallow_copy() |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2374 | # That so you don't change the calling context |
| 2375 | # if an error occurs in the middle (say DivisionImpossible is raised). |
| 2376 | |
| 2377 | rounding = context._set_rounding_decision(NEVER_ROUND) |
| 2378 | instance = instance / Decimal(2) |
| 2379 | context._set_rounding_decision(rounding) |
| 2380 | |
| 2381 | This will make it not round for that operation. |
| 2382 | """ |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2383 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2384 | rounding = self._rounding_decision |
| 2385 | self._rounding_decision = type |
| 2386 | return rounding |
| 2387 | |
| 2388 | def _set_rounding(self, type): |
| 2389 | """Sets the rounding type. |
| 2390 | |
| 2391 | Sets the rounding type, and returns the current (previous) |
| 2392 | rounding type. Often used like: |
| 2393 | |
| 2394 | context = context.copy() |
| 2395 | # so you don't change the calling context |
| 2396 | # if an error occurs in the middle. |
| 2397 | rounding = context._set_rounding(ROUND_UP) |
| 2398 | val = self.__sub__(other, context=context) |
| 2399 | context._set_rounding(rounding) |
| 2400 | |
| 2401 | This will make it round up for that operation. |
| 2402 | """ |
| 2403 | rounding = self.rounding |
| 2404 | self.rounding= type |
| 2405 | return rounding |
| 2406 | |
Raymond Hettinger | fed5296 | 2004-07-14 15:41:57 +0000 | [diff] [blame] | 2407 | def create_decimal(self, num='0'): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2408 | """Creates a new Decimal instance but using self as context.""" |
| 2409 | d = Decimal(num, context=self) |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 2410 | return d._fix(self) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2411 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2412 | # Methods |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2413 | def abs(self, a): |
| 2414 | """Returns the absolute value of the operand. |
| 2415 | |
| 2416 | If the operand is negative, the result is the same as using the minus |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2417 | operation on the operand. Otherwise, the result is the same as using |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2418 | the plus operation on the operand. |
| 2419 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2420 | >>> ExtendedContext.abs(Decimal('2.1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2421 | Decimal("2.1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2422 | >>> ExtendedContext.abs(Decimal('-100')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2423 | Decimal("100") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2424 | >>> ExtendedContext.abs(Decimal('101.5')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2425 | Decimal("101.5") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2426 | >>> ExtendedContext.abs(Decimal('-101.5')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2427 | Decimal("101.5") |
| 2428 | """ |
| 2429 | return a.__abs__(context=self) |
| 2430 | |
| 2431 | def add(self, a, b): |
| 2432 | """Return the sum of the two operands. |
| 2433 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2434 | >>> ExtendedContext.add(Decimal('12'), Decimal('7.00')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2435 | Decimal("19.00") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2436 | >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2437 | Decimal("1.02E+4") |
| 2438 | """ |
| 2439 | return a.__add__(b, context=self) |
| 2440 | |
| 2441 | def _apply(self, a): |
Raymond Hettinger | dab988d | 2004-10-09 07:10:44 +0000 | [diff] [blame] | 2442 | return str(a._fix(self)) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2443 | |
| 2444 | def compare(self, a, b): |
| 2445 | """Compares values numerically. |
| 2446 | |
| 2447 | If the signs of the operands differ, a value representing each operand |
| 2448 | ('-1' if the operand is less than zero, '0' if the operand is zero or |
| 2449 | negative zero, or '1' if the operand is greater than zero) is used in |
| 2450 | place of that operand for the comparison instead of the actual |
| 2451 | operand. |
| 2452 | |
| 2453 | The comparison is then effected by subtracting the second operand from |
| 2454 | the first and then returning a value according to the result of the |
| 2455 | subtraction: '-1' if the result is less than zero, '0' if the result is |
| 2456 | zero or negative zero, or '1' if the result is greater than zero. |
| 2457 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2458 | >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2459 | Decimal("-1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2460 | >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2461 | Decimal("0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2462 | >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2463 | Decimal("0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2464 | >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2465 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2466 | >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2467 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2468 | >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2469 | Decimal("-1") |
| 2470 | """ |
| 2471 | return a.compare(b, context=self) |
| 2472 | |
| 2473 | def divide(self, a, b): |
| 2474 | """Decimal division in a specified context. |
| 2475 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2476 | >>> ExtendedContext.divide(Decimal('1'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2477 | Decimal("0.333333333") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2478 | >>> ExtendedContext.divide(Decimal('2'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2479 | Decimal("0.666666667") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2480 | >>> ExtendedContext.divide(Decimal('5'), Decimal('2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2481 | Decimal("2.5") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2482 | >>> ExtendedContext.divide(Decimal('1'), Decimal('10')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2483 | Decimal("0.1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2484 | >>> ExtendedContext.divide(Decimal('12'), Decimal('12')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2485 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2486 | >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2487 | Decimal("4.00") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2488 | >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2489 | Decimal("1.20") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2490 | >>> ExtendedContext.divide(Decimal('1000'), Decimal('100')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2491 | Decimal("10") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2492 | >>> ExtendedContext.divide(Decimal('1000'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2493 | Decimal("1000") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2494 | >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2495 | Decimal("1.20E+6") |
| 2496 | """ |
| 2497 | return a.__div__(b, context=self) |
| 2498 | |
| 2499 | def divide_int(self, a, b): |
| 2500 | """Divides two numbers and returns the integer part of the result. |
| 2501 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2502 | >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2503 | Decimal("0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2504 | >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2505 | Decimal("3") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2506 | >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2507 | Decimal("3") |
| 2508 | """ |
| 2509 | return a.__floordiv__(b, context=self) |
| 2510 | |
| 2511 | def divmod(self, a, b): |
| 2512 | return a.__divmod__(b, context=self) |
| 2513 | |
| 2514 | def max(self, a,b): |
| 2515 | """max compares two values numerically and returns the maximum. |
| 2516 | |
| 2517 | If either operand is a NaN then the general rules apply. |
| 2518 | Otherwise, the operands are compared as as though by the compare |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2519 | operation. If they are numerically equal then the left-hand operand |
| 2520 | is chosen as the result. Otherwise the maximum (closer to positive |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2521 | infinity) of the two operands is chosen as the result. |
| 2522 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2523 | >>> ExtendedContext.max(Decimal('3'), Decimal('2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2524 | Decimal("3") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2525 | >>> ExtendedContext.max(Decimal('-10'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2526 | Decimal("3") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2527 | >>> ExtendedContext.max(Decimal('1.0'), Decimal('1')) |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2528 | Decimal("1") |
| 2529 | >>> ExtendedContext.max(Decimal('7'), Decimal('NaN')) |
| 2530 | Decimal("7") |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2531 | """ |
| 2532 | return a.max(b, context=self) |
| 2533 | |
| 2534 | def min(self, a,b): |
| 2535 | """min compares two values numerically and returns the minimum. |
| 2536 | |
| 2537 | If either operand is a NaN then the general rules apply. |
| 2538 | Otherwise, the operands are compared as as though by the compare |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2539 | operation. If they are numerically equal then the left-hand operand |
| 2540 | is chosen as the result. Otherwise the minimum (closer to negative |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2541 | infinity) of the two operands is chosen as the result. |
| 2542 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2543 | >>> ExtendedContext.min(Decimal('3'), Decimal('2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2544 | Decimal("2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2545 | >>> ExtendedContext.min(Decimal('-10'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2546 | Decimal("-10") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2547 | >>> ExtendedContext.min(Decimal('1.0'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2548 | Decimal("1.0") |
Raymond Hettinger | d6c700a | 2004-08-17 06:39:37 +0000 | [diff] [blame] | 2549 | >>> ExtendedContext.min(Decimal('7'), Decimal('NaN')) |
| 2550 | Decimal("7") |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2551 | """ |
| 2552 | return a.min(b, context=self) |
| 2553 | |
| 2554 | def minus(self, a): |
| 2555 | """Minus corresponds to unary prefix minus in Python. |
| 2556 | |
| 2557 | The operation is evaluated using the same rules as subtract; the |
| 2558 | operation minus(a) is calculated as subtract('0', a) where the '0' |
| 2559 | has the same exponent as the operand. |
| 2560 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2561 | >>> ExtendedContext.minus(Decimal('1.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2562 | Decimal("-1.3") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2563 | >>> ExtendedContext.minus(Decimal('-1.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2564 | Decimal("1.3") |
| 2565 | """ |
| 2566 | return a.__neg__(context=self) |
| 2567 | |
| 2568 | def multiply(self, a, b): |
| 2569 | """multiply multiplies two operands. |
| 2570 | |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2571 | If either operand is a special value then the general rules apply. |
| 2572 | Otherwise, the operands are multiplied together ('long multiplication'), |
| 2573 | resulting in a number which may be as long as the sum of the lengths |
| 2574 | of the two operands. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2575 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2576 | >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2577 | Decimal("3.60") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2578 | >>> ExtendedContext.multiply(Decimal('7'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2579 | Decimal("21") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2580 | >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2581 | Decimal("0.72") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2582 | >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2583 | Decimal("-0.0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2584 | >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2585 | Decimal("4.28135971E+11") |
| 2586 | """ |
| 2587 | return a.__mul__(b, context=self) |
| 2588 | |
| 2589 | def normalize(self, a): |
Raymond Hettinger | e0f1581 | 2004-07-05 05:36:39 +0000 | [diff] [blame] | 2590 | """normalize reduces an operand to its simplest form. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2591 | |
| 2592 | Essentially a plus operation with all trailing zeros removed from the |
| 2593 | result. |
| 2594 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2595 | >>> ExtendedContext.normalize(Decimal('2.1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2596 | Decimal("2.1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2597 | >>> ExtendedContext.normalize(Decimal('-2.0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2598 | Decimal("-2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2599 | >>> ExtendedContext.normalize(Decimal('1.200')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2600 | Decimal("1.2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2601 | >>> ExtendedContext.normalize(Decimal('-120')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2602 | Decimal("-1.2E+2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2603 | >>> ExtendedContext.normalize(Decimal('120.00')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2604 | Decimal("1.2E+2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2605 | >>> ExtendedContext.normalize(Decimal('0.00')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2606 | Decimal("0") |
| 2607 | """ |
| 2608 | return a.normalize(context=self) |
| 2609 | |
| 2610 | def plus(self, a): |
| 2611 | """Plus corresponds to unary prefix plus in Python. |
| 2612 | |
| 2613 | The operation is evaluated using the same rules as add; the |
| 2614 | operation plus(a) is calculated as add('0', a) where the '0' |
| 2615 | has the same exponent as the operand. |
| 2616 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2617 | >>> ExtendedContext.plus(Decimal('1.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2618 | Decimal("1.3") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2619 | >>> ExtendedContext.plus(Decimal('-1.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2620 | Decimal("-1.3") |
| 2621 | """ |
| 2622 | return a.__pos__(context=self) |
| 2623 | |
| 2624 | def power(self, a, b, modulo=None): |
| 2625 | """Raises a to the power of b, to modulo if given. |
| 2626 | |
| 2627 | The right-hand operand must be a whole number whose integer part (after |
| 2628 | any exponent has been applied) has no more than 9 digits and whose |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2629 | fractional part (if any) is all zeros before any rounding. The operand |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2630 | may be positive, negative, or zero; if negative, the absolute value of |
| 2631 | the power is used, and the left-hand operand is inverted (divided into |
| 2632 | 1) before use. |
| 2633 | |
| 2634 | If the increased precision needed for the intermediate calculations |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 2635 | exceeds the capabilities of the implementation then an Invalid |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2636 | operation condition is raised. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2637 | |
| 2638 | If, when raising to a negative power, an underflow occurs during the |
| 2639 | division into 1, the operation is not halted at that point but |
| 2640 | continues. |
| 2641 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2642 | >>> ExtendedContext.power(Decimal('2'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2643 | Decimal("8") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2644 | >>> ExtendedContext.power(Decimal('2'), Decimal('-3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2645 | Decimal("0.125") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2646 | >>> ExtendedContext.power(Decimal('1.7'), Decimal('8')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2647 | Decimal("69.7575744") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2648 | >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2649 | Decimal("0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2650 | >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2651 | Decimal("0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2652 | >>> ExtendedContext.power(Decimal('Infinity'), Decimal('0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2653 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2654 | >>> ExtendedContext.power(Decimal('Infinity'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2655 | Decimal("Infinity") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2656 | >>> ExtendedContext.power(Decimal('Infinity'), Decimal('2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2657 | Decimal("Infinity") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2658 | >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2659 | Decimal("0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2660 | >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2661 | Decimal("-0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2662 | >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2663 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2664 | >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2665 | Decimal("-Infinity") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2666 | >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2667 | Decimal("Infinity") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2668 | >>> ExtendedContext.power(Decimal('0'), Decimal('0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2669 | Decimal("NaN") |
| 2670 | """ |
| 2671 | return a.__pow__(b, modulo, context=self) |
| 2672 | |
| 2673 | def quantize(self, a, b): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2674 | """Returns a value equal to 'a' (rounded), having the exponent of 'b'. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2675 | |
| 2676 | The coefficient of the result is derived from that of the left-hand |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2677 | operand. It may be rounded using the current rounding setting (if the |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2678 | exponent is being increased), multiplied by a positive power of ten (if |
| 2679 | the exponent is being decreased), or is unchanged (if the exponent is |
| 2680 | already equal to that of the right-hand operand). |
| 2681 | |
| 2682 | Unlike other operations, if the length of the coefficient after the |
| 2683 | quantize operation would be greater than precision then an Invalid |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2684 | operation condition is raised. This guarantees that, unless there is |
| 2685 | an error condition, the exponent of the result of a quantize is always |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2686 | equal to that of the right-hand operand. |
| 2687 | |
| 2688 | Also unlike other operations, quantize will never raise Underflow, even |
| 2689 | if the result is subnormal and inexact. |
| 2690 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2691 | >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2692 | Decimal("2.170") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2693 | >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2694 | Decimal("2.17") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2695 | >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2696 | Decimal("2.2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2697 | >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2698 | Decimal("2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2699 | >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2700 | Decimal("0E+1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2701 | >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2702 | Decimal("-Infinity") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2703 | >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2704 | Decimal("NaN") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2705 | >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2706 | Decimal("-0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2707 | >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2708 | Decimal("-0E+5") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2709 | >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2710 | Decimal("NaN") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2711 | >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2712 | Decimal("NaN") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2713 | >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2714 | Decimal("217.0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2715 | >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2716 | Decimal("217") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2717 | >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2718 | Decimal("2.2E+2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2719 | >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2720 | Decimal("2E+2") |
| 2721 | """ |
| 2722 | return a.quantize(b, context=self) |
| 2723 | |
| 2724 | def remainder(self, a, b): |
| 2725 | """Returns the remainder from integer division. |
| 2726 | |
| 2727 | The result is the residue of the dividend after the operation of |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2728 | calculating integer division as described for divide-integer, rounded |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 2729 | to precision digits if necessary. The sign of the result, if |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2730 | non-zero, is the same as that of the original dividend. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2731 | |
| 2732 | This operation will fail under the same conditions as integer division |
| 2733 | (that is, if integer division on the same two operands would fail, the |
| 2734 | remainder cannot be calculated). |
| 2735 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2736 | >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2737 | Decimal("2.1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2738 | >>> ExtendedContext.remainder(Decimal('10'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2739 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2740 | >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2741 | Decimal("-1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2742 | >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2743 | Decimal("0.2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2744 | >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2745 | Decimal("0.1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2746 | >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2747 | Decimal("1.0") |
| 2748 | """ |
| 2749 | return a.__mod__(b, context=self) |
| 2750 | |
| 2751 | def remainder_near(self, a, b): |
| 2752 | """Returns to be "a - b * n", where n is the integer nearest the exact |
| 2753 | value of "x / b" (if two integers are equally near then the even one |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2754 | is chosen). If the result is equal to 0 then its sign will be the |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2755 | sign of a. |
| 2756 | |
| 2757 | This operation will fail under the same conditions as integer division |
| 2758 | (that is, if integer division on the same two operands would fail, the |
| 2759 | remainder cannot be calculated). |
| 2760 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2761 | >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2762 | Decimal("-0.9") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2763 | >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2764 | Decimal("-2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2765 | >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2766 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2767 | >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2768 | Decimal("-1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2769 | >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2770 | Decimal("0.2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2771 | >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2772 | Decimal("0.1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2773 | >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2774 | Decimal("-0.3") |
| 2775 | """ |
| 2776 | return a.remainder_near(b, context=self) |
| 2777 | |
| 2778 | def same_quantum(self, a, b): |
| 2779 | """Returns True if the two operands have the same exponent. |
| 2780 | |
| 2781 | The result is never affected by either the sign or the coefficient of |
| 2782 | either operand. |
| 2783 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2784 | >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2785 | False |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2786 | >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2787 | True |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2788 | >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2789 | False |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2790 | >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2791 | True |
| 2792 | """ |
| 2793 | return a.same_quantum(b) |
| 2794 | |
| 2795 | def sqrt(self, a): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2796 | """Square root of a non-negative number to context precision. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2797 | |
| 2798 | If the result must be inexact, it is rounded using the round-half-even |
| 2799 | algorithm. |
| 2800 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2801 | >>> ExtendedContext.sqrt(Decimal('0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2802 | Decimal("0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2803 | >>> ExtendedContext.sqrt(Decimal('-0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2804 | Decimal("-0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2805 | >>> ExtendedContext.sqrt(Decimal('0.39')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2806 | Decimal("0.624499800") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2807 | >>> ExtendedContext.sqrt(Decimal('100')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2808 | Decimal("10") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2809 | >>> ExtendedContext.sqrt(Decimal('1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2810 | Decimal("1") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2811 | >>> ExtendedContext.sqrt(Decimal('1.0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2812 | Decimal("1.0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2813 | >>> ExtendedContext.sqrt(Decimal('1.00')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2814 | Decimal("1.0") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2815 | >>> ExtendedContext.sqrt(Decimal('7')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2816 | Decimal("2.64575131") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2817 | >>> ExtendedContext.sqrt(Decimal('10')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2818 | Decimal("3.16227766") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2819 | >>> ExtendedContext.prec |
Raymond Hettinger | 6ea4845 | 2004-07-03 12:26:21 +0000 | [diff] [blame] | 2820 | 9 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2821 | """ |
| 2822 | return a.sqrt(context=self) |
| 2823 | |
| 2824 | def subtract(self, a, b): |
Georg Brandl | f33d01d | 2005-08-22 19:35:18 +0000 | [diff] [blame] | 2825 | """Return the difference between the two operands. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2826 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2827 | >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2828 | Decimal("0.23") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2829 | >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2830 | Decimal("0.00") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2831 | >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2832 | Decimal("-0.77") |
| 2833 | """ |
| 2834 | return a.__sub__(b, context=self) |
| 2835 | |
| 2836 | def to_eng_string(self, a): |
| 2837 | """Converts a number to a string, using scientific notation. |
| 2838 | |
| 2839 | The operation is not affected by the context. |
| 2840 | """ |
| 2841 | return a.to_eng_string(context=self) |
| 2842 | |
| 2843 | def to_sci_string(self, a): |
| 2844 | """Converts a number to a string, using scientific notation. |
| 2845 | |
| 2846 | The operation is not affected by the context. |
| 2847 | """ |
| 2848 | return a.__str__(context=self) |
| 2849 | |
| 2850 | def to_integral(self, a): |
| 2851 | """Rounds to an integer. |
| 2852 | |
| 2853 | When the operand has a negative exponent, the result is the same |
| 2854 | as using the quantize() operation using the given operand as the |
| 2855 | left-hand-operand, 1E+0 as the right-hand-operand, and the precision |
| 2856 | of the operand as the precision setting, except that no flags will |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2857 | be set. The rounding mode is taken from the context. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2858 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2859 | >>> ExtendedContext.to_integral(Decimal('2.1')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2860 | Decimal("2") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2861 | >>> ExtendedContext.to_integral(Decimal('100')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2862 | Decimal("100") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2863 | >>> ExtendedContext.to_integral(Decimal('100.0')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2864 | Decimal("100") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2865 | >>> ExtendedContext.to_integral(Decimal('101.5')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2866 | Decimal("102") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2867 | >>> ExtendedContext.to_integral(Decimal('-101.5')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2868 | Decimal("-102") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2869 | >>> ExtendedContext.to_integral(Decimal('10E+5')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2870 | Decimal("1.0E+6") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2871 | >>> ExtendedContext.to_integral(Decimal('7.89E+77')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2872 | Decimal("7.89E+77") |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 2873 | >>> ExtendedContext.to_integral(Decimal('-Inf')) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2874 | Decimal("-Infinity") |
| 2875 | """ |
| 2876 | return a.to_integral(context=self) |
| 2877 | |
| 2878 | class _WorkRep(object): |
| 2879 | __slots__ = ('sign','int','exp') |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 2880 | # sign: 0 or 1 |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2881 | # int: int or long |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2882 | # exp: None, int, or string |
| 2883 | |
| 2884 | def __init__(self, value=None): |
| 2885 | if value is None: |
| 2886 | self.sign = None |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2887 | self.int = 0 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2888 | self.exp = None |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 2889 | elif isinstance(value, Decimal): |
| 2890 | self.sign = value._sign |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2891 | cum = 0 |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 2892 | for digit in value._int: |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2893 | cum = cum * 10 + digit |
| 2894 | self.int = cum |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2895 | self.exp = value._exp |
Raymond Hettinger | 17931de | 2004-10-27 06:21:46 +0000 | [diff] [blame] | 2896 | else: |
| 2897 | # assert isinstance(value, tuple) |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2898 | self.sign = value[0] |
| 2899 | self.int = value[1] |
| 2900 | self.exp = value[2] |
| 2901 | |
| 2902 | def __repr__(self): |
| 2903 | return "(%r, %r, %r)" % (self.sign, self.int, self.exp) |
| 2904 | |
| 2905 | __str__ = __repr__ |
| 2906 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2907 | |
| 2908 | |
| 2909 | def _normalize(op1, op2, shouldround = 0, prec = 0): |
| 2910 | """Normalizes op1, op2 to have the same exp and length of coefficient. |
| 2911 | |
| 2912 | Done during addition. |
| 2913 | """ |
| 2914 | # Yes, the exponent is a long, but the difference between exponents |
| 2915 | # must be an int-- otherwise you'd get a big memory problem. |
| 2916 | numdigits = int(op1.exp - op2.exp) |
| 2917 | if numdigits < 0: |
| 2918 | numdigits = -numdigits |
| 2919 | tmp = op2 |
| 2920 | other = op1 |
| 2921 | else: |
| 2922 | tmp = op1 |
| 2923 | other = op2 |
| 2924 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2925 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2926 | if shouldround and numdigits > prec + 1: |
| 2927 | # Big difference in exponents - check the adjusted exponents |
| 2928 | tmp_len = len(str(tmp.int)) |
| 2929 | other_len = len(str(other.int)) |
| 2930 | if numdigits > (other_len + prec + 1 - tmp_len): |
| 2931 | # If the difference in adjusted exps is > prec+1, we know |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2932 | # other is insignificant, so might as well put a 1 after the |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 2933 | # precision (since this is only for addition). Also stops |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2934 | # use of massive longs. |
Martin v. Löwis | cfe3128 | 2006-07-19 17:18:32 +0000 | [diff] [blame] | 2935 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2936 | extend = prec + 2 - tmp_len |
| 2937 | if extend <= 0: |
| 2938 | extend = 1 |
| 2939 | tmp.int *= 10 ** extend |
| 2940 | tmp.exp -= extend |
| 2941 | other.int = 1 |
| 2942 | other.exp = tmp.exp |
| 2943 | return op1, op2 |
| 2944 | |
| 2945 | tmp.int *= 10 ** numdigits |
| 2946 | tmp.exp -= numdigits |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2947 | return op1, op2 |
| 2948 | |
| 2949 | def _adjust_coefficients(op1, op2): |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2950 | """Adjust op1, op2 so that op2.int * 10 > op1.int >= op2.int. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2951 | |
| 2952 | Returns the adjusted op1, op2 as well as the change in op1.exp-op2.exp. |
| 2953 | |
| 2954 | Used on _WorkRep instances during division. |
| 2955 | """ |
| 2956 | adjust = 0 |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2957 | # If op1 is smaller, make it larger |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2958 | while op2.int > op1.int: |
| 2959 | op1.int *= 10 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2960 | op1.exp -= 1 |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2961 | adjust += 1 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2962 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2963 | # If op2 is too small, make it larger |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2964 | while op1.int >= (10 * op2.int): |
| 2965 | op2.int *= 10 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2966 | op2.exp -= 1 |
| 2967 | adjust -= 1 |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2968 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2969 | return op1, op2, adjust |
| 2970 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 2971 | ##### Helper Functions #################################################### |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2972 | |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2973 | def _convert_other(other): |
| 2974 | """Convert other to Decimal. |
| 2975 | |
| 2976 | Verifies that it's ok to use in an implicit construction. |
| 2977 | """ |
| 2978 | if isinstance(other, Decimal): |
| 2979 | return other |
| 2980 | if isinstance(other, (int, long)): |
| 2981 | return Decimal(other) |
Raymond Hettinger | 267b868 | 2005-03-27 10:47:39 +0000 | [diff] [blame] | 2982 | return NotImplemented |
Raymond Hettinger | 636a6b1 | 2004-09-19 01:54:09 +0000 | [diff] [blame] | 2983 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2984 | _infinity_map = { |
| 2985 | 'inf' : 1, |
| 2986 | 'infinity' : 1, |
| 2987 | '+inf' : 1, |
| 2988 | '+infinity' : 1, |
| 2989 | '-inf' : -1, |
| 2990 | '-infinity' : -1 |
| 2991 | } |
| 2992 | |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 2993 | def _isinfinity(num): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2994 | """Determines whether a string or float is infinity. |
| 2995 | |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 2996 | +1 for negative infinity; 0 for finite ; +1 for positive infinity |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 2997 | """ |
| 2998 | num = str(num).lower() |
| 2999 | return _infinity_map.get(num, 0) |
| 3000 | |
Raymond Hettinger | 0ea241e | 2004-07-04 13:53:24 +0000 | [diff] [blame] | 3001 | def _isnan(num): |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3002 | """Determines whether a string or float is NaN |
| 3003 | |
| 3004 | (1, sign, diagnostic info as string) => NaN |
| 3005 | (2, sign, diagnostic info as string) => sNaN |
| 3006 | 0 => not a NaN |
| 3007 | """ |
| 3008 | num = str(num).lower() |
| 3009 | if not num: |
| 3010 | return 0 |
| 3011 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3012 | # Get the sign, get rid of trailing [+-] |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3013 | sign = 0 |
| 3014 | if num[0] == '+': |
| 3015 | num = num[1:] |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3016 | elif num[0] == '-': # elif avoids '+-nan' |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3017 | num = num[1:] |
| 3018 | sign = 1 |
| 3019 | |
| 3020 | if num.startswith('nan'): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3021 | if len(num) > 3 and not num[3:].isdigit(): # diagnostic info |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3022 | return 0 |
| 3023 | return (1, sign, num[3:].lstrip('0')) |
| 3024 | if num.startswith('snan'): |
| 3025 | if len(num) > 4 and not num[4:].isdigit(): |
| 3026 | return 0 |
| 3027 | return (2, sign, num[4:].lstrip('0')) |
| 3028 | return 0 |
| 3029 | |
| 3030 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3031 | ##### Setup Specific Contexts ############################################ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3032 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3033 | # The default context prototype used by Context() |
Raymond Hettinger | fed5296 | 2004-07-14 15:41:57 +0000 | [diff] [blame] | 3034 | # Is mutable, so that new contexts can have different default values |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3035 | |
| 3036 | DefaultContext = Context( |
Raymond Hettinger | 6ea4845 | 2004-07-03 12:26:21 +0000 | [diff] [blame] | 3037 | prec=28, rounding=ROUND_HALF_EVEN, |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 3038 | traps=[DivisionByZero, Overflow, InvalidOperation], |
| 3039 | flags=[], |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3040 | _rounding_decision=ALWAYS_ROUND, |
Raymond Hettinger | 99148e7 | 2004-07-14 19:56:56 +0000 | [diff] [blame] | 3041 | Emax=999999999, |
| 3042 | Emin=-999999999, |
Raymond Hettinger | e0f1581 | 2004-07-05 05:36:39 +0000 | [diff] [blame] | 3043 | capitals=1 |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3044 | ) |
| 3045 | |
| 3046 | # Pre-made alternate contexts offered by the specification |
| 3047 | # Don't change these; the user should be able to select these |
| 3048 | # contexts and be able to reproduce results from other implementations |
| 3049 | # of the spec. |
| 3050 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 3051 | BasicContext = Context( |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3052 | prec=9, rounding=ROUND_HALF_UP, |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 3053 | traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow], |
| 3054 | flags=[], |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3055 | ) |
| 3056 | |
Raymond Hettinger | 9ec3e3b | 2004-07-03 13:48:56 +0000 | [diff] [blame] | 3057 | ExtendedContext = Context( |
Raymond Hettinger | 6ea4845 | 2004-07-03 12:26:21 +0000 | [diff] [blame] | 3058 | prec=9, rounding=ROUND_HALF_EVEN, |
Raymond Hettinger | bf44069 | 2004-07-10 14:14:37 +0000 | [diff] [blame] | 3059 | traps=[], |
| 3060 | flags=[], |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3061 | ) |
| 3062 | |
| 3063 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3064 | ##### Useful Constants (internal use only) ################################ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3065 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3066 | # Reusable defaults |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3067 | Inf = Decimal('Inf') |
| 3068 | negInf = Decimal('-Inf') |
| 3069 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3070 | # Infsign[sign] is infinity w/ that sign |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3071 | Infsign = (Inf, negInf) |
| 3072 | |
| 3073 | NaN = Decimal('NaN') |
| 3074 | |
| 3075 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3076 | ##### crud for parsing strings ############################################# |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3077 | import re |
| 3078 | |
| 3079 | # There's an optional sign at the start, and an optional exponent |
| 3080 | # at the end. The exponent has an optional sign and at least one |
| 3081 | # digit. In between, must have either at least one digit followed |
| 3082 | # by an optional fraction, or a decimal point followed by at least |
| 3083 | # one digit. Yuck. |
| 3084 | |
| 3085 | _parser = re.compile(r""" |
| 3086 | # \s* |
| 3087 | (?P<sign>[-+])? |
| 3088 | ( |
| 3089 | (?P<int>\d+) (\. (?P<frac>\d*))? |
| 3090 | | |
| 3091 | \. (?P<onlyfrac>\d+) |
| 3092 | ) |
| 3093 | ([eE](?P<exp>[-+]? \d+))? |
| 3094 | # \s* |
| 3095 | $ |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3096 | """, re.VERBOSE).match # Uncomment the \s* to allow leading or trailing spaces. |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3097 | |
| 3098 | del re |
| 3099 | |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3100 | def _string2exact(s): |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3101 | """Return sign, n, p s.t. |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 3102 | |
Facundo Batista | 59c5884 | 2007-04-10 12:58:45 +0000 | [diff] [blame] | 3103 | Float string value == -1**sign * n * 10**p exactly |
| 3104 | """ |
Raymond Hettinger | 7c85fa4 | 2004-07-01 11:01:35 +0000 | [diff] [blame] | 3105 | m = _parser(s) |
| 3106 | if m is None: |
| 3107 | raise ValueError("invalid literal for Decimal: %r" % s) |
| 3108 | |
| 3109 | if m.group('sign') == "-": |
| 3110 | sign = 1 |
| 3111 | else: |
| 3112 | sign = 0 |
| 3113 | |
| 3114 | exp = m.group('exp') |
| 3115 | if exp is None: |
| 3116 | exp = 0 |
| 3117 | else: |
| 3118 | exp = int(exp) |
| 3119 | |
| 3120 | intpart = m.group('int') |
| 3121 | if intpart is None: |
| 3122 | intpart = "" |
| 3123 | fracpart = m.group('onlyfrac') |
| 3124 | else: |
| 3125 | fracpart = m.group('frac') |
| 3126 | if fracpart is None: |
| 3127 | fracpart = "" |
| 3128 | |
| 3129 | exp -= len(fracpart) |
| 3130 | |
| 3131 | mantissa = intpart + fracpart |
| 3132 | tmp = map(int, mantissa) |
| 3133 | backup = tmp |
| 3134 | while tmp and tmp[0] == 0: |
| 3135 | del tmp[0] |
| 3136 | |
| 3137 | # It's a zero |
| 3138 | if not tmp: |
| 3139 | if backup: |
| 3140 | return (sign, tuple(backup), exp) |
| 3141 | return (sign, (0,), exp) |
| 3142 | mantissa = tuple(tmp) |
| 3143 | |
| 3144 | return (sign, mantissa, exp) |
| 3145 | |
| 3146 | |
| 3147 | if __name__ == '__main__': |
| 3148 | import doctest, sys |
| 3149 | doctest.testmod(sys.modules[__name__]) |