17c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# Copyright (c) 2004 Python Software Foundation. 27c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# All rights reserved. 37c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 47c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# Written by Eric Price <eprice at tjhsst.edu> 57c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# and Facundo Batista <facundo at taniquetil.com.ar> 67c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# and Raymond Hettinger <python at rcn.com> 71f34eb17b501687c1251678bcffab9accd432591Fred Drake# and Aahz <aahz at pobox.com> 87c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# and Tim Peters 97c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1027dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# This module is currently Py2.3 compatible and should be kept that way 1127dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# unless a major compelling advantage arises. IOW, 2.3 compatibility is 1227dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# strongly preferred, but not guaranteed. 1327dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger 1427dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# Also, this module should be kept in sync with the latest updates of 1527dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# the IBM specification as it evolves. Those updates will be treated 1627dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# as bug fixes (deviation from the spec is a compatibility, usability 1727dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# bug) and will be backported. At this point the spec is stabilizing 1827dbcf2162c61d5ba248c57c89ff4fa3d85a21c7Raymond Hettinger# and the updates are becoming fewer, smaller, and less significant. 197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger""" 217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerThis is a Py2.3 implementation of decimal floating point arithmetic based on 227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerthe General Decimal Arithmetic Specification: 237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 248a9369bfa4fa01add9b19dced20467cf6803a8d7Raymond Hettinger http://speleotrove.com/decimal/decarith.html 257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 260ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettingerand IEEE standard 854-1987: 277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 28023c3e74a60493c71818a85f7e66fab5b1fdc3c5Senthil Kumaran http://en.wikipedia.org/wiki/IEEE_854-1987 297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDecimal floating point has finite precision with arbitrarily large bounds. 317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo BatistaThe purpose of this module is to support arithmetic using familiar 3359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista"schoolhouse" rules and to avoid some of the tricky representation 347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerissues associated with binary floating point. The package is especially 357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingeruseful for financial applications or for contexts where users have 367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerexpectations that are at odds with binary floating point (for instance, 377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerin binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead 38abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettingerof the expected Decimal('0.00') returned by decimal floating point). 397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerHere are some examples of using the decimal module: 417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> from decimal import * 43bd7f76dd04780304c397323ae994092ce759d5a2Raymond Hettinger>>> setcontext(ExtendedContext) 447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal(0) 45abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('0') 46abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger>>> Decimal('1') 47abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('1') 48abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger>>> Decimal('-.0123') 49abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('-0.0123') 507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal(123456) 51abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('123456') 52abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger>>> Decimal('123.45e12345678901234567890') 53abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('1.2345E+12345678901234567892') 54abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger>>> Decimal('1.33') + Decimal('1.27') 55abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('2.60') 56abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger>>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41') 57abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('-2.20') 587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> dig = Decimal(1) 597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print dig / Decimal(3) 607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger0.333333333 617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> getcontext().prec = 18 627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print dig / Decimal(3) 637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger0.333333333333333333 647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print dig.sqrt() 657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger1 667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print Decimal(3).sqrt() 677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger1.73205080756887729 687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print Decimal(3) ** 123 697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger4.85192780976896427E+58 707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> inf = Decimal(1) / Decimal(0) 717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print inf 727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerInfinity 737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> neginf = Decimal(-1) / Decimal(0) 747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print neginf 757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger-Infinity 767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print neginf + inf 777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerNaN 787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print neginf * inf 797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger-Infinity 807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print dig / 0 817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerInfinity 82bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger>>> getcontext().traps[DivisionByZero] = 1 837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print dig / 0 847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerTraceback (most recent call last): 857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ... 867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ... 877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ... 887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDivisionByZero: x / 0 897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> c = Context() 90bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger>>> c.traps[InvalidOperation] = 0 915aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger>>> print c.flags[InvalidOperation] 927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger0 937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> c.divide(Decimal(0), Decimal(0)) 94abe32371878dcaea31c835e10144fdaa2eca6492Raymond HettingerDecimal('NaN') 95bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger>>> c.traps[InvalidOperation] = 1 965aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger>>> print c.flags[InvalidOperation] 977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger1 985aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger>>> c.flags[InvalidOperation] = 0 995aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger>>> print c.flags[InvalidOperation] 1007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger0 1017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print c.divide(Decimal(0), Decimal(0)) 1027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerTraceback (most recent call last): 1037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ... 1047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ... 1057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ... 1065aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond HettingerInvalidOperation: 0 / 0 1075aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger>>> print c.flags[InvalidOperation] 1087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger1 1095aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger>>> c.flags[InvalidOperation] = 0 110bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger>>> c.traps[InvalidOperation] = 0 1117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> print c.divide(Decimal(0), Decimal(0)) 1127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerNaN 1135aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger>>> print c.flags[InvalidOperation] 1147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger1 1157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> 1167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger""" 1177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger__all__ = [ 1197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Two major classes 1207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 'Decimal', 'Context', 1217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Contexts 1239ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger 'DefaultContext', 'BasicContext', 'ExtendedContext', 1247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Exceptions 126d87ac8f24da5dce860b559b69e6af59edd2c3eecRaymond Hettinger 'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero', 127d87ac8f24da5dce860b559b69e6af59edd2c3eecRaymond Hettinger 'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow', 1287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Constants for use in setting up contexts 1307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING', 131353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', 'ROUND_05UP', 1327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Functions for manipulating contexts 1348b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 'setcontext', 'getcontext', 'localcontext' 1357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger] 1367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 137a016debad07f11c3b85131b4205305d190ac9ecaRaymond Hettinger__version__ = '1.70' # Highest version of the spec this complies with 138daeceb2de80047f25aedbf16bf40dc5d619e64ddRaymond Hettinger 139f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettingerimport math as _math 1402c8585b0afb6a39c215a71963e2fadea96f2c6aeRaymond Hettingerimport numbers as _numbers 1417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 142097a1903035f9ee2efb1953306123f183124125dRaymond Hettingertry: 143097a1903035f9ee2efb1953306123f183124125dRaymond Hettinger from collections import namedtuple as _namedtuple 144097a1903035f9ee2efb1953306123f183124125dRaymond Hettinger DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent') 145097a1903035f9ee2efb1953306123f183124125dRaymond Hettingerexcept ImportError: 146097a1903035f9ee2efb1953306123f183124125dRaymond Hettinger DecimalTuple = lambda *args: args 147097a1903035f9ee2efb1953306123f183124125dRaymond Hettinger 14859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista# Rounding 1490ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_DOWN = 'ROUND_DOWN' 1500ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_HALF_UP = 'ROUND_HALF_UP' 1510ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_HALF_EVEN = 'ROUND_HALF_EVEN' 1520ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_CEILING = 'ROUND_CEILING' 1530ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_FLOOR = 'ROUND_FLOOR' 1540ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_UP = 'ROUND_UP' 1550ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_HALF_DOWN = 'ROUND_HALF_DOWN' 156353750c405c9099d0be69c6af1d17037b38c4ddfFacundo BatistaROUND_05UP = 'ROUND_05UP' 1577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 15859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista# Errors 1597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DecimalException(ArithmeticError): 1615aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger """Base exception class. 1627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Used exceptions derive from this. 1647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If an exception derives from another exception besides this (such as 1657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Underflow (Inexact, Rounded, Subnormal) that indicates that it is only 1667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger called if the others are present. This isn't actually used for 1677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger anything, though. 1687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 169cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis handle -- Called when context._raise_error is called and the 1708a6f3fe3b5588da1e2803a308628ed909ea8bcfbStefan Krah trap_enabler is not set. First argument is self, second is the 171cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis context. More arguments can be given, those being after 172cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis the explanation in _raise_error (For example, 173cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis context._raise_error(NewError, '(-x)!', self._sign) would 174cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis call NewError().handle(context, self._sign).) 175cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 1767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger To define a new exception, it should be sufficient to have it derive 1777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger from DecimalException. 1787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 1807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger pass 1817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Clamped(DecimalException): 1847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Exponent of a 0 changed to fit bounds. 1857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals clamped if the exponent of a result has been 1877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger altered in order to fit the constraints of a specific concrete 18859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista representation. This may occur when the exponent of a zero result would 18959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista be outside the bounds of a representation, or when a large normal 19059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista number would have an encoded exponent that cannot be represented. In 1917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger this latter case, the exponent is reduced to fit and the corresponding 1927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger number of zero digits are appended to the coefficient ("fold-down"). 1937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass InvalidOperation(DecimalException): 1967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """An invalid operation was performed. 1977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Various bad things cause this: 1997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Something creates a signaling NaN 2017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -INF + INF 20259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista 0 * (+-)INF 20359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista (+-)INF / (+-)INF 2047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x % 0 2057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (+-)INF % x 2067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x._rescale( non-integer ) 2077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sqrt(-x) , x > 0 2087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 ** 0 2097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x ** (non-integer) 2107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x ** (+-)INF 2117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger An operand is invalid 212353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 213353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result of the operation after these is a quiet positive NaN, 214353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista except when the cause is a signaling NaN, in which case the result is 215353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista also a quiet NaN, but with the original sign, and an optional 216353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista diagnostic information. 2177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 2197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if args: 2200f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True) 2210f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista return ans._fix_nan(context) 222c5de0969ca2a82c66efd30bb675f03c2324a3b27Mark Dickinson return _NaN 2237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass ConversionSyntax(InvalidOperation): 2257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Trying to convert badly formed string. 2267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2279a118f1dc3f23ead28f31fdc5144ad5ce01e5b7fSerhiy Storchaka This occurs and signals invalid-operation if a string is being 2287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger converted to a number and it does not conform to the numeric string 22959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista syntax. The result is [0,qNaN]. 2307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 231cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis def handle(self, context, *args): 232c5de0969ca2a82c66efd30bb675f03c2324a3b27Mark Dickinson return _NaN 2337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DivisionByZero(DecimalException, ZeroDivisionError): 2357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Division by 0. 2367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals division-by-zero if division of a finite number 2387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger by zero was attempted (during a divide-integer or divide operation, or a 2397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger power operation with negative right-hand operand), and the dividend was 2407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger not zero. 2417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result of the operation is [sign,inf], where sign is the exclusive 2437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger or of the signs of the operands for divide, or is 1 for an odd power of 2447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -0, for power. 2457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 246cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 247cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista def handle(self, context, sign, *args): 248b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[sign] 2497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DivisionImpossible(InvalidOperation): 2517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Cannot perform the division adequately. 2527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals invalid-operation if the integer result of a 2547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger divide-integer or remainder operation had too many digits (would be 25559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista longer than precision). The result is [0,qNaN]. 2567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 257cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 2587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 259c5de0969ca2a82c66efd30bb675f03c2324a3b27Mark Dickinson return _NaN 2607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DivisionUndefined(InvalidOperation, ZeroDivisionError): 2627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Undefined result of division. 2637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals invalid-operation if division by zero was 2657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger attempted (during a divide-integer, divide, or remainder operation), and 26659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista the dividend is also zero. The result is [0,qNaN]. 2677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 268cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 269cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista def handle(self, context, *args): 270c5de0969ca2a82c66efd30bb675f03c2324a3b27Mark Dickinson return _NaN 2717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Inexact(DecimalException): 2737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Had to round, losing information. 2747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals inexact whenever the result of an operation is 2767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger not exact (that is, it needed to be rounded and any discarded digits 27759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista were non-zero), or if an overflow or underflow condition occurs. The 2787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result in all cases is unchanged. 2797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The inexact signal may be tested (or trapped) to determine if a given 2817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation (or sequence of operations) was inexact. 2827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass InvalidContext(InvalidOperation): 2857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Invalid context. Unknown rounding, for example. 2867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals invalid-operation if an invalid context was 28859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista detected during an operation. This can occur if contexts are not checked 2897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger on creation and either the precision exceeds the capability of the 2907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger underlying concrete representation or an unknown or unsupported rounding 29159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista was specified. These aspects of the context need only be checked when 29259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista the values are required to be used. The result is [0,qNaN]. 2937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 294cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 2957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 296c5de0969ca2a82c66efd30bb675f03c2324a3b27Mark Dickinson return _NaN 2977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Rounded(DecimalException): 2997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Number got rounded (not necessarily changed during rounding). 3007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals rounded whenever the result of an operation is 3027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounded (that is, some zero or non-zero digits were discarded from the 30359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista coefficient), or if an overflow or underflow condition occurs. The 3047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result in all cases is unchanged. 3057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The rounded signal may be tested (or trapped) to determine if a given 3077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation (or sequence of operations) caused a loss of precision. 3087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 3097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Subnormal(DecimalException): 3117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Exponent < Emin before rounding. 3127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals subnormal whenever the result of a conversion or 3147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation is subnormal (that is, its adjusted exponent is less than 31559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista Emin, before any rounding). The result in all cases is unchanged. 3167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The subnormal signal may be tested (or trapped) to determine if a given 3187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger or operation (or sequence of operations) yielded a subnormal result. 3197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 3207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Overflow(Inexact, Rounded): 3227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Numerical overflow. 3237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals overflow if the adjusted exponent of a result 3257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (from a conversion or from an operation that is not an attempt to divide 3267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger by zero), after rounding, would be greater than the largest value that 3277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger can be handled by the implementation (the value Emax). 3287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result depends on the rounding mode: 3307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger For round-half-up and round-half-even (and for round-half-down and 3327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger round-up, if implemented), the result of the operation is [sign,inf], 33359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista where sign is the sign of the intermediate result. For round-down, the 3347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result is the largest finite number that can be represented in the 33559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista current precision, with the sign of the intermediate result. For 3367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger round-ceiling, the result is the same as for round-down if the sign of 33759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista the intermediate result is 1, or is [0,inf] otherwise. For round-floor, 3387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the result is the same as for round-down if the sign of the intermediate 33959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded 3407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger will also be raised. 3410f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista """ 342cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 3437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, sign, *args): 3447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN, 345353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ROUND_HALF_DOWN, ROUND_UP): 346b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[sign] 3477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if sign == 0: 3487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding == ROUND_CEILING: 349b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[sign] 35072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, '9'*context.prec, 35172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context.Emax-context.prec+1) 3527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if sign == 1: 3537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding == ROUND_FLOOR: 354b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[sign] 35572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, '9'*context.prec, 35672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context.Emax-context.prec+1) 3577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Underflow(Inexact, Rounded, Subnormal): 3607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Numerical underflow with result rounded to 0. 3617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals underflow if a result is inexact and the 3637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger adjusted exponent of the result would be smaller (more negative) than 3647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the smallest value that can be handled by the implementation (the value 36559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista Emin). That is, the result is both inexact and subnormal. 3667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result after an underflow will be a subnormal number rounded, if 36859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista necessary, so that its exponent is not less than Etiny. This may result 3697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger in 0 with the sign of the intermediate result and an exponent of Etiny. 3707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger In all cases, Inexact, Rounded, and Subnormal will also be raised. 3727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 3737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3745aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger# List of public traps and flags 375fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger_signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded, 376cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis Underflow, InvalidOperation, Subnormal] 3777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3785aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger# Map conditions (per the spec) to signals 3795aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger_condition_map = {ConversionSyntax:InvalidOperation, 3805aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger DivisionImpossible:InvalidOperation, 3815aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger DivisionUndefined:InvalidOperation, 3825aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger InvalidContext:InvalidOperation} 3837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Context Functions ################################################## 3857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 386ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger# The getcontext() and setcontext() function manage access to a thread-local 387ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger# current context. Py2.4 offers direct support for thread locals. If that 388ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger# is not available, use threading.currentThread() which is slower but will 3897e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger# work for older Pythons. If threads are not part of the build, create a 390cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis# mock threading object with threading.local() returning the module namespace. 3917e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger 3927e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettingertry: 3937e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger import threading 3947e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettingerexcept ImportError: 3957e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger # Python was compiled without threads; create a mock object instead 3967e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger import sys 39759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista class MockThreading(object): 3987e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger def local(self, sys=sys): 3997e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger return sys.modules[__name__] 4007e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger threading = MockThreading() 4017e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger del sys, MockThreading 402ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 403ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettingertry: 404ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger threading.local 405ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 406ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettingerexcept AttributeError: 407ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 40859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # To fix reloading, force it to create a new context 40959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Old contexts have different exceptions in their dicts, making problems. 410ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger if hasattr(threading.currentThread(), '__decimal_context__'): 411ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger del threading.currentThread().__decimal_context__ 412ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 413ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def setcontext(context): 414ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Set this thread's context to context.""" 415ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger if context in (DefaultContext, BasicContext, ExtendedContext): 4169fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger context = context.copy() 41761992efc4bb413ae7a19752215eca5af09be6b6dRaymond Hettinger context.clear_flags() 4187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger threading.currentThread().__decimal_context__ = context 419ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 420ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def getcontext(): 421ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Returns this thread's context. 422ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 423ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger If this thread does not yet have a context, returns 424ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger a new context and sets this thread's context. 425ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger New contexts are copies of DefaultContext. 426ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """ 427ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger try: 428ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return threading.currentThread().__decimal_context__ 429ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger except AttributeError: 430ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger context = Context() 431ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger threading.currentThread().__decimal_context__ = context 432ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return context 433ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 434ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettingerelse: 435ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 436ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger local = threading.local() 4379fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger if hasattr(local, '__decimal_context__'): 4389fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger del local.__decimal_context__ 439ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 440ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def getcontext(_local=local): 441ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Returns this thread's context. 442ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 443ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger If this thread does not yet have a context, returns 444ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger a new context and sets this thread's context. 445ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger New contexts are copies of DefaultContext. 446ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """ 447ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger try: 448ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return _local.__decimal_context__ 449ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger except AttributeError: 450ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger context = Context() 451ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger _local.__decimal_context__ = context 452ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return context 453ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 454ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def setcontext(context, _local=local): 455ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Set this thread's context to context.""" 456ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger if context in (DefaultContext, BasicContext, ExtendedContext): 4579fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger context = context.copy() 45861992efc4bb413ae7a19752215eca5af09be6b6dRaymond Hettinger context.clear_flags() 459ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger _local.__decimal_context__ = context 460ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 461cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis del threading, local # Don't contaminate the namespace 4627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4638b6999b4c5fab174090be263ba90f193bdede141Nick Coghlandef localcontext(ctx=None): 4648b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """Return a context manager for a copy of the supplied context 4658b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 4668b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan Uses a copy of the current context if no context is specified 4678b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan The returned context manager creates a local decimal context 4688b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan in a with statement: 4698b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan def sin(x): 4708b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan with localcontext() as ctx: 4718b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ctx.prec += 2 4728b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # Rest of sin calculation algorithm 4738b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # uses a precision 2 greater than normal 47459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista return +s # Convert result to normal precision 4758b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 4768b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan def sin(x): 4778b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan with localcontext(ExtendedContext): 4788b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # Rest of sin calculation algorithm 4798b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # uses the Extended Context from the 4808b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # General Decimal Arithmetic Specification 48159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista return +s # Convert result to normal context 4828b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 483ee340e501d2a59d70a91748ebd948787bfac8044Facundo Batista >>> setcontext(DefaultContext) 4848b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> print getcontext().prec 4858b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 28 4868b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> with localcontext(): 4878b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... ctx = getcontext() 488495df4716fce4156c1eb110f9342297164eecbb6Raymond Hettinger ... ctx.prec += 2 4898b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... print ctx.prec 4908b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... 4918b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 30 4928b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> with localcontext(ExtendedContext): 4938b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... print getcontext().prec 4948b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... 4958b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 9 4968b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> print getcontext().prec 4978b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 28 4988b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """ 499ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan if ctx is None: ctx = getcontext() 500ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan return _ContextManager(ctx) 5018b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 5027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Decimal class ####################################################### 5047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Decimal(object): 5067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Floating point class for decimal arithmetic.""" 5077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 508636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger __slots__ = ('_exp','_int','_sign', '_is_special') 509636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # Generally, the value of the Decimal instance is given by 510636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # (-1)**_sign * _int * 10**_exp 511636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # Special values are signified by _is_special == True 5127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 513dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger # We're immutable, so use __new__ not __init__ 514636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger def __new__(cls, value="0", context=None): 5157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Create a decimal point instance. 5167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger >>> Decimal('3.14') # string input 518abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3.14') 51959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) 520abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3.14') 5217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger >>> Decimal(314) # int or long 522abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('314') 5237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger >>> Decimal(Decimal(314)) # another decimal instance 524abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('314') 52559bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay 526abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3.14') 5277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 5287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 52972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # Note that the coefficient, self._int, is actually stored as 53072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # a string rather than as a tuple of digits. This speeds up 53172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # the "digits to integer" and "integer to digits" conversions 53272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # that are used in almost every arithmetic operation on 53372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # Decimals. This is an internal detail: the as_tuple function 53472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # and the Decimal constructor still deal with tuples of 53572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # digits. 53672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 537636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self = object.__new__(cls) 538636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 5390d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # From a string 5400d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # REs insist on real strings, so we can too. 5410d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if isinstance(value, basestring): 54259bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson m = _parser(value.strip()) 5430d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if m is None: 5440d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if context is None: 5450d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista context = getcontext() 5460d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista return context._raise_error(ConversionSyntax, 5470d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista "Invalid literal for Decimal: %r" % value) 548636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 5490d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if m.group('sign') == "-": 5500d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = 1 5510d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5520d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = 0 5530d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista intpart = m.group('int') 5540d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if intpart is not None: 5550d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # finite number 5564326ad8f72053140aa658a0392a509c9da382670Mark Dickinson fracpart = m.group('frac') or '' 5570d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista exp = int(m.group('exp') or '0') 5584326ad8f72053140aa658a0392a509c9da382670Mark Dickinson self._int = str(int(intpart+fracpart)) 5594326ad8f72053140aa658a0392a509c9da382670Mark Dickinson self._exp = exp - len(fracpart) 5600d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = False 5610d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5620d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista diag = m.group('diag') 5630d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if diag is not None: 5640d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # NaN 5654326ad8f72053140aa658a0392a509c9da382670Mark Dickinson self._int = str(int(diag or '0')).lstrip('0') 5660d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if m.group('signal'): 5670d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = 'N' 5680d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5690d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = 'n' 5700d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5710d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # infinity 5720d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = '0' 5730d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = 'F' 5740d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = True 575636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self 576636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 577636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # From an integer 5787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if isinstance(value, (int,long)): 579636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if value >= 0: 580636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._sign = 0 581636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger else: 582636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._sign = 1 583636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._exp = 0 58472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = str(abs(value)) 5850d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = False 5860d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista return self 5870d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista 5880d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # From another decimal 5890d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if isinstance(value, Decimal): 5900d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = value._exp 5910d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = value._sign 5920d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = value._int 5930d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = value._is_special 5940d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista return self 5950d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista 5960d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # From an internal working value 5970d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if isinstance(value, _WorkRep): 5980d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = value.sign 5990d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = str(value.int) 6000d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = int(value.exp) 6010d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = False 602636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self 603636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 604636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # tuple/list conversion (possibly from as_tuple()) 605636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(value, (list,tuple)): 606636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if len(value) != 3: 6079b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError('Invalid tuple size in creation of Decimal ' 6089b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista 'from list or tuple. The list or tuple ' 6099b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista 'should have exactly three elements.') 6109b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # process sign. The isinstance test rejects floats 6119b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if not (isinstance(value[0], (int, long)) and value[0] in (0,1)): 6129b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError("Invalid sign. The first value in the tuple " 6139b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "should be an integer; either 0 for a " 6149b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "positive number or 1 for a negative number.") 615636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._sign = value[0] 6169b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if value[2] == 'F': 6179b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # infinity: value[1] is ignored 61872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = '0' 619636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._exp = value[2] 620636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._is_special = True 621636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger else: 6229b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # process and validate the digits in value[1] 6239b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista digits = [] 6249b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista for digit in value[1]: 6259b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if isinstance(digit, (int, long)) and 0 <= digit <= 9: 6269b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # skip leading zeros 6279b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if digits or digit != 0: 6289b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista digits.append(digit) 6299b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista else: 6309b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError("The second value in the tuple must " 6319b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "be composed of integers in the range " 6329b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "0 through 9.") 6339b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if value[2] in ('n', 'N'): 6349b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # NaN: digits form the diagnostic 63572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = ''.join(map(str, digits)) 6369b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._exp = value[2] 6379b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._is_special = True 6389b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista elif isinstance(value[2], (int, long)): 6399b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # finite number: digits give the coefficient 64072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = ''.join(map(str, digits or [0])) 6419b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._exp = value[2] 6429b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._is_special = False 6439b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista else: 6449b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError("The third value in the tuple must " 6459b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "be an integer, or one of the " 6469b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "strings 'F', 'n', 'N'.") 647636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self 648636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 649636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(value, float): 650ed171abd93c7a507cb26339aa2d68a7e980378f6Raymond Hettinger value = Decimal.from_float(value) 651ed171abd93c7a507cb26339aa2d68a7e980378f6Raymond Hettinger self._exp = value._exp 652ed171abd93c7a507cb26339aa2d68a7e980378f6Raymond Hettinger self._sign = value._sign 653ed171abd93c7a507cb26339aa2d68a7e980378f6Raymond Hettinger self._int = value._int 654ed171abd93c7a507cb26339aa2d68a7e980378f6Raymond Hettinger self._is_special = value._is_special 655ed171abd93c7a507cb26339aa2d68a7e980378f6Raymond Hettinger return self 6567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 657636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger raise TypeError("Cannot convert %r to Decimal" % value) 6587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 6596a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson # @classmethod, but @decorator is not valid Python 2.3 syntax, so 6606a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson # don't use it (see notes on Py2.3 compatibility at top of file) 661f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger def from_float(cls, f): 662f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger """Converts a float to a decimal number, exactly. 663f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 664f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Note that Decimal.from_float(0.1) is not the same as Decimal('0.1'). 665f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Since 0.1 is not exactly representable in binary floating point, the 666f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger value is stored as the nearest representable value which is 667f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 0x1.999999999999ap-4. The exact equivalent of the value in decimal 668f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger is 0.1000000000000000055511151231257827021181583404541015625. 669f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 670f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> Decimal.from_float(0.1) 671f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Decimal('0.1000000000000000055511151231257827021181583404541015625') 672f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> Decimal.from_float(float('nan')) 673f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Decimal('NaN') 674f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> Decimal.from_float(float('inf')) 675f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Decimal('Infinity') 676f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> Decimal.from_float(-float('inf')) 677f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Decimal('-Infinity') 678f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> Decimal.from_float(-0.0) 679f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Decimal('-0') 680f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 681f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger """ 682f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger if isinstance(f, (int, long)): # handle integer inputs 683f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger return cls(f) 684f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger if _math.isinf(f) or _math.isnan(f): # raises TypeError if not a float 685f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger return cls(repr(f)) 6866a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson if _math.copysign(1.0, f) == 1.0: 6876a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson sign = 0 6886a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson else: 6896a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson sign = 1 690f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger n, d = abs(f).as_integer_ratio() 691f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger k = d.bit_length() - 1 692f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger result = _dec_from_triple(sign, str(n*5**k), -k) 6936a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson if cls is Decimal: 6946a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson return result 6956a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson else: 6966a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson return cls(result) 6976a961637a826ab6293293866a43d5924cf1a77e7Mark Dickinson from_float = classmethod(from_float) 698f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 6997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _isnan(self): 7007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether the number is not actually one. 7017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 if a number 7030f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista 1 if NaN 7047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2 if sNaN 7057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 706636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 707636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger exp = self._exp 708636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if exp == 'n': 709636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return 1 710636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger elif exp == 'N': 711636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return 2 7127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 7137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _isinfinity(self): 7157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether the number is infinite 7167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 if finite or not a number 7187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1 if +INF 7197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -1 if -INF 7207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 7217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp == 'F': 7227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign: 7237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return -1 7247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 1 7257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 7267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 727353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _check_nans(self, other=None, context=None): 7287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether the number is not actually one. 7297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self, other are sNaN, signal 7317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self, other are NaN return nan 7327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 7337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Done before operations. 7357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 7367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 737636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self_is_nan = self._isnan() 738636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other is None: 739636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other_is_nan = False 740636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger else: 741636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other_is_nan = other._isnan() 742636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 743636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self_is_nan or other_is_nan: 744636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 745636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 746636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 747636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self_is_nan == 2: 748636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, 'sNaN', 7490f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista self) 750636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other_is_nan == 2: 751636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, 'sNaN', 7520f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista other) 753636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self_is_nan: 754353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._fix_nan(context) 755636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 756353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other._fix_nan(context) 7577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 7587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7592fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson def _compare_check_nans(self, other, context): 7602fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson """Version of _check_nans used for the signaling comparisons 7612fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson compare_signal, __le__, __lt__, __ge__, __gt__. 7622fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 7632fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson Signal InvalidOperation if either self or other is a (quiet 7642fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson or signaling) NaN. Signaling NaNs take precedence over quiet 7652fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson NaNs. 7662fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 7672fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson Return 0 if neither operand is a NaN. 7682fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 7692fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson """ 7702fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if context is None: 7712fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson context = getcontext() 7722fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 7732fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if self._is_special or other._is_special: 7742fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if self.is_snan(): 7752fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return context._raise_error(InvalidOperation, 7762fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 'comparison involving sNaN', 7772fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson self) 7782fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson elif other.is_snan(): 7792fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return context._raise_error(InvalidOperation, 7802fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 'comparison involving sNaN', 7812fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson other) 7822fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson elif self.is_qnan(): 7832fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return context._raise_error(InvalidOperation, 7842fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 'comparison involving NaN', 7852fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson self) 7862fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson elif other.is_qnan(): 7872fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return context._raise_error(InvalidOperation, 7882fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 'comparison involving NaN', 7892fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson other) 7902fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return 0 7912fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 7927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __nonzero__(self): 7931a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is nonzero; otherwise return False. 7947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7951a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista NaNs and infinities are considered nonzero. 7967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 79772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self._is_special or self._int != '0' 7987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7992fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson def _cmp(self, other): 8002fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson """Compare the two non-NaN decimal instances self and other. 8017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8022fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson Returns -1 if self < other, 0 if self == other and 1 8032fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if self > other. This routine is for internal use only.""" 804636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 8052fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if self._is_special or other._is_special: 806e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson self_inf = self._isinfinity() 807e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson other_inf = other._isinfinity() 808e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson if self_inf == other_inf: 809e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson return 0 810e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson elif self_inf < other_inf: 811e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson return -1 812e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson else: 813e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson return 1 8147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 815e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson # check for zeros; Decimal('0') == Decimal('-0') 816353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return 0 819353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return -((-1)**other._sign) 821353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return (-1)**self._sign 8237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 82459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If different signs, neg one is less 8257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if other._sign < self._sign: 8267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return -1 8277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign < other._sign: 8287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 1 8297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 830636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self_adjusted = self.adjusted() 831636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other_adjusted = other.adjusted() 832353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_adjusted == other_adjusted: 83372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self_padded = self._int + '0'*(self._exp - other._exp) 83472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista other_padded = other._int + '0'*(other._exp - self._exp) 835e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson if self_padded == other_padded: 836e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson return 0 837e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson elif self_padded < other_padded: 838e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson return -(-1)**self._sign 839e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson else: 840e52c31450dfbe649b559b3fa96630d348b838c19Mark Dickinson return (-1)**self._sign 841353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self_adjusted > other_adjusted: 8427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return (-1)**self._sign 843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: # self_adjusted < other_adjusted 8447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return -((-1)**self._sign) 8457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8462fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # Note: The Decimal standard doesn't cover rich comparisons for 8472fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # Decimals. In particular, the specification is silent on the 8482fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # subject of what should happen for a comparison involving a NaN. 8492fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # We take the following approach: 8502fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # 851e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson # == comparisons involving a quiet NaN always return False 852e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson # != comparisons involving a quiet NaN always return True 853e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson # == or != comparisons involving a signaling NaN signal 854e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson # InvalidOperation, and return False or True as above if the 855e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson # InvalidOperation is not trapped. 8562fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # <, >, <= and >= comparisons involving a (quiet or signaling) 8572fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # NaN signal InvalidOperation, and return False if the 8583a94ee05f77d0200cfb7988d02f3fdf292932a94Mark Dickinson # InvalidOperation is not trapped. 8592fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # 8602fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # This behavior is designed to conform as closely as possible to 8612fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson # that specified by IEEE 754. 8622fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 863e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson def __eq__(self, other, context=None): 86499d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson other = _convert_other(other, allow_float=True) 8652fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if other is NotImplemented: 8662fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return other 867e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson if self._check_nans(other, context): 8682fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return False 8692fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return self._cmp(other) == 0 8700aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger 871e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson def __ne__(self, other, context=None): 87299d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson other = _convert_other(other, allow_float=True) 8732fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if other is NotImplemented: 8742fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return other 875e096e82e827f6092706c7349fd4944c275382eb5Mark Dickinson if self._check_nans(other, context): 8762fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return True 8772fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return self._cmp(other) != 0 8782fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 8792fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson def __lt__(self, other, context=None): 88099d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson other = _convert_other(other, allow_float=True) 8812fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if other is NotImplemented: 8822fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return other 8832fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson ans = self._compare_check_nans(other, context) 8842fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if ans: 8852fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return False 8862fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return self._cmp(other) < 0 8872fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 8882fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson def __le__(self, other, context=None): 88999d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson other = _convert_other(other, allow_float=True) 8902fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if other is NotImplemented: 8912fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return other 8922fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson ans = self._compare_check_nans(other, context) 8932fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if ans: 8942fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return False 8952fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return self._cmp(other) <= 0 8962fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 8972fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson def __gt__(self, other, context=None): 89899d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson other = _convert_other(other, allow_float=True) 8992fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if other is NotImplemented: 9002fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return other 9012fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson ans = self._compare_check_nans(other, context) 9022fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if ans: 9032fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return False 9042fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return self._cmp(other) > 0 9052fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson 9062fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson def __ge__(self, other, context=None): 90799d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson other = _convert_other(other, allow_float=True) 9082fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if other is NotImplemented: 9092fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return other 9102fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson ans = self._compare_check_nans(other, context) 9112fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if ans: 9122fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return False 9132fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return self._cmp(other) >= 0 9140aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger 9157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def compare(self, other, context=None): 9167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Compares one to another. 9177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -1 => a < b 9197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 => a = b 9207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1 => a > b 9217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger NaN => one is NaN 9227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Like __cmp__, but returns Decimal instances. 9237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 924353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 9257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 92659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Compare(NaN, NaN) = NaN 927636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if (self._is_special or other and other._is_special): 928636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 929636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 930636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 9317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9322fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return Decimal(self._cmp(other)) 9337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __hash__(self): 9357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """x.__hash__() <==> hash(x)""" 9367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Decimal integers must hash the same as the ints 93752b25795c02442fc40f8932d05e5d728266339a4Facundo Batista # 93852b25795c02442fc40f8932d05e5d728266339a4Facundo Batista # The hash of a nonspecial noninteger Decimal must depend only 93952b25795c02442fc40f8932d05e5d728266339a4Facundo Batista # on the value of that Decimal, and not on its representation. 940abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger # For example: hash(Decimal('100E-1')) == hash(Decimal('10')). 941f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson 942f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # Equality comparisons involving signaling nans can raise an 943f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # exception; since equality checks are implicitly and 944f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # unpredictably used when checking set and dict membership, we 945f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # prevent signaling nans from being used as set elements or 946f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # dict keys by making __hash__ raise an exception. 947f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson if self._is_special: 948f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson if self.is_snan(): 949f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson raise TypeError('Cannot hash a signaling NaN value.') 950f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson elif self.is_nan(): 951f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # 0 to match hash(float('nan')) 952f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson return 0 953f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson else: 954f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # values chosen to match hash(float('inf')) and 955f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # hash(float('-inf')). 956f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson if self._sign: 957f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson return -271828 958f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson else: 959f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson return 314159 96099d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson 96199d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson # In Python 2.7, we're allowing comparisons (but not 96299d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson # arithmetic operations) between floats and Decimals; so if 96399d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson # a Decimal instance is exactly representable as a float then 964f3eeca16cbadd7da5836ed781572343863b1a074Mark Dickinson # its hash should match that of the float. 96599d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson self_as_float = float(self) 96699d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson if Decimal.from_float(self_as_float) == self: 96799d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson return hash(self_as_float) 96899d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson 9698c202440699cef19602acc24822366d0d7c32083Facundo Batista if self._isinteger(): 9708c202440699cef19602acc24822366d0d7c32083Facundo Batista op = _WorkRep(self.to_integral_value()) 9718c202440699cef19602acc24822366d0d7c32083Facundo Batista # to make computation feasible for Decimals with large 9728c202440699cef19602acc24822366d0d7c32083Facundo Batista # exponent, we use the fact that hash(n) == hash(m) for 9738c202440699cef19602acc24822366d0d7c32083Facundo Batista # any two nonzero integers n and m such that (i) n and m 9748c202440699cef19602acc24822366d0d7c32083Facundo Batista # have the same sign, and (ii) n is congruent to m modulo 9758c202440699cef19602acc24822366d0d7c32083Facundo Batista # 2**64-1. So we can replace hash((-1)**s*c*10**e) with 9768c202440699cef19602acc24822366d0d7c32083Facundo Batista # hash((-1)**s*c*pow(10, e, 2**64-1). 9778c202440699cef19602acc24822366d0d7c32083Facundo Batista return hash((-1)**op.sign*op.int*pow(10, op.exp, 2**64-1)) 97852b25795c02442fc40f8932d05e5d728266339a4Facundo Batista # The value of a nonzero nonspecial Decimal instance is 97952b25795c02442fc40f8932d05e5d728266339a4Facundo Batista # faithfully represented by the triple consisting of its sign, 98052b25795c02442fc40f8932d05e5d728266339a4Facundo Batista # its adjusted exponent, and its coefficient with trailing 98152b25795c02442fc40f8932d05e5d728266339a4Facundo Batista # zeros removed. 98252b25795c02442fc40f8932d05e5d728266339a4Facundo Batista return hash((self._sign, 98352b25795c02442fc40f8932d05e5d728266339a4Facundo Batista self._exp+len(self._int), 98452b25795c02442fc40f8932d05e5d728266339a4Facundo Batista self._int.rstrip('0'))) 9857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def as_tuple(self): 9877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Represents the number as a triple tuple. 9887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger To show the internals exactly as they are. 9907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 991097a1903035f9ee2efb1953306123f183124125dRaymond Hettinger return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp) 9927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __repr__(self): 9947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Represents the number as an instance of Decimal.""" 9957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Invariant: eval(repr(d)) == d 996abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger return "Decimal('%s')" % str(self) 9977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 998353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def __str__(self, eng=False, context=None): 9997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return string representation of the number in scientific notation. 10007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Captures all of the information in the underlying representation. 10027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 10037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 100462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista sign = ['', '-'][self._sign] 1005e5a0a9609faea9afdc6f81f1f6dfa07b1437e3aeRaymond Hettinger if self._is_special: 100662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if self._exp == 'F': 100762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + 'Infinity' 100862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif self._exp == 'n': 100962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + 'NaN' + self._int 101062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista else: # self._exp == 'N' 101162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + 'sNaN' + self._int 101262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 101362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # number of digits of self._int to left of decimal point 101462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista leftdigits = self._exp + len(self._int) 101562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 101662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # dotplace is number of digits of self._int to the left of the 101762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # decimal point in the mantissa of the output string (that is, 101862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # after adjusting the exponent) 101962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if self._exp <= 0 and leftdigits > -6: 102062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # no exponent required 102162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista dotplace = leftdigits 102262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif not eng: 102362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # usual scientific notation: 1 digit on left of the point 10247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger dotplace = 1 102562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif self._int == '0': 102662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # engineering notation, zero 102762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista dotplace = (leftdigits + 1) % 3 - 1 10287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 102962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # engineering notation, nonzero 103062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista dotplace = (leftdigits - 1) % 3 + 1 103162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 103262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if dotplace <= 0: 103362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista intpart = '0' 103462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista fracpart = '.' + '0'*(-dotplace) + self._int 103562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif dotplace >= len(self._int): 103662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista intpart = self._int+'0'*(dotplace-len(self._int)) 103762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista fracpart = '' 103862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista else: 103962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista intpart = self._int[:dotplace] 104062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista fracpart = '.' + self._int[dotplace:] 104162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if leftdigits == dotplace: 104262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista exp = '' 104362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista else: 104462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if context is None: 104562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista context = getcontext() 104662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace) 104762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 104862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + intpart + fracpart + exp 10497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def to_eng_string(self, context=None): 1051af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger """Convert to a string, using engineering notation if an exponent is needed. 10527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1053af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger Engineering notation has an exponent which is a multiple of 3. This 1054af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger can leave up to 3 digits to the left of the decimal place and may 1055af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger require the addition of either one or two trailing zeros. 10567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1057353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self.__str__(eng=True, context=context) 10587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __neg__(self, context=None): 10607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns a copy with the sign switched. 10617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Rounds, if it has reason. 10637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1064636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 1065636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 1066636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1067636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 10687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10692c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson if context is None: 10702c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson context = getcontext() 10712c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson 10722c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson if not self and context.rounding != ROUND_FLOOR: 10732c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson # -Decimal('0') is Decimal('0'), not Decimal('-0'), except 10742c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson # in ROUND_FLOOR rounding mode. 10750f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista ans = self.copy_abs() 10767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 1077353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self.copy_negate() 1078636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1079e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 10807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __pos__(self, context=None): 10827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns a copy, unless it is a sNaN. 10837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10848d496add74530767cad3aa8b5b371b9a7f0b8498Martin Panter Rounds the number (if more than precision digits) 10857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1086636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 1087636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 1088636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1089636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 10907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10912c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson if context is None: 10922c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson context = getcontext() 10932c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson 10942c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson if not self and context.rounding != ROUND_FLOOR: 10952c8c62e64d3c8438f13774d275755ad77a516ad6Mark Dickinson # + (-0) = 0, except in ROUND_FLOOR rounding mode. 10960f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista ans = self.copy_abs() 1097353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1098353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = Decimal(self) 10997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1100e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 11017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1102e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista def __abs__(self, round=True, context=None): 11037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the absolute value of self. 11047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1105e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista If the keyword argument 'round' is false, do not round. The 1106e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista expression self.__abs__(round=False) is equivalent to 1107e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.copy_abs(). 11087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1109e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista if not round: 1110e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return self.copy_abs() 1111e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista 1112636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 1113636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 1114636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1115636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 11167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign: 11187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = self.__neg__(context=context) 11197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 11207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = self.__pos__(context=context) 11217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 11237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __add__(self, other, context=None): 11257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns self + other. 11267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -INF + INF (or the reverse) cause InvalidOperation errors. 11287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1129636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1130267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1131267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 1132636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 11337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context is None: 11347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = getcontext() 11357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1136636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 1137636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 1138636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1139636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 11407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1141636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity(): 114259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If both INF, same sign => same as both, opposite => error. 1143636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._sign != other._sign and other._isinfinity(): 1144636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, '-INF + INF') 1145636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Decimal(self) 1146636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other._isinfinity(): 114759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista return Decimal(other) # Can't both be infinity here 11487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp = min(self._exp, other._exp) 11507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger negativezero = 0 11517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding == ROUND_FLOOR and self._sign != other._sign: 115259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If the answer is 0, the sign should be negative, in this case. 11537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger negativezero = 1 11547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self and not other: 11567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sign = min(self._sign, other._sign) 11577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if negativezero: 11587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sign = 1 115972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(sign, '0', exp) 1160e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 1161353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 11627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 116399b5548298ffc2ae5f03bd9ef873ce45a9844f0eFacundo Batista exp = max(exp, other._exp - context.prec-1) 1164353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = other._rescale(exp, context.rounding) 1165e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 11667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 11677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not other: 116899b5548298ffc2ae5f03bd9ef873ce45a9844f0eFacundo Batista exp = max(exp, self._exp - context.prec-1) 1169353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(exp, context.rounding) 1170e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 11717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 11727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op1 = _WorkRep(self) 11747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op2 = _WorkRep(other) 1175e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista op1, op2 = _normalize(op1, op2, context.prec) 11767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result = _WorkRep() 11787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if op1.sign != op2.sign: 11797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Equal and opposite 118017931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op1.int == op2.int: 118172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(negativezero, '0', exp) 1182e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 1183353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 118417931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op1.int < op2.int: 11857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op1, op2 = op2, op1 118659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # OK, now abs(op1) > abs(op2) 118717931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op1.sign == 1: 118817931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger result.sign = 1 11897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op1.sign, op2.sign = op2.sign, op1.sign 11907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 119117931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger result.sign = 0 119259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # So we know the sign, and op1 > 0. 119317931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger elif op1.sign == 1: 11947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result.sign = 1 119517931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger op1.sign, op2.sign = (0, 0) 119617931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger else: 119717931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger result.sign = 0 119859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Now, op1 > abs(op2) > 0 11997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 120017931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op2.sign == 0: 1201636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger result.int = op1.int + op2.int 12027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 1203636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger result.int = op1.int - op2.int 12047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result.exp = op1.exp 12067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = Decimal(result) 1207e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 12087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 12097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __radd__ = __add__ 12117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __sub__(self, other, context=None): 1213353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return self - other""" 1214636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1215267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1216267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 12177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1218636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 1219636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context=context) 1220636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1221636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 12227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1223353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self - other is computed as self + other.copy_negate() 1224353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self.__add__(other.copy_negate(), context=context) 12257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rsub__(self, other, context=None): 1227353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return other - self""" 1228636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1229267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1230267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 12317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1232353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other.__sub__(self, context=context) 12337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __mul__(self, other, context=None): 12357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return self * other. 12367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (+-) INF * 0 (or its reverse) raise InvalidOperation. 12387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1239636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1240267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1241267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 1242636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 12437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context is None: 12447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = getcontext() 12457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1246d87ac8f24da5dce860b559b69e6af59edd2c3eecRaymond Hettinger resultsign = self._sign ^ other._sign 12477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1248636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 1249636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 1250636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1251636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 1252636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1253636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity(): 1254636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if not other: 1255636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, '(+-)INF * 0') 1256b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[resultsign] 1257636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1258636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other._isinfinity(): 1259636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if not self: 1260636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, '0 * (+-)INF') 1261b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[resultsign] 12627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger resultexp = self._exp + other._exp 12647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Special case for multiplying by zero 12667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self or not other: 126772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, '0', resultexp) 1268e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista # Fixing in case the exponent is out of bounds 1269e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 12707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 12717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Special case for multiplying by power of 10 127372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if self._int == '1': 127472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, other._int, resultexp) 1275e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 12767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 127772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if other._int == '1': 127872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, self._int, resultexp) 1279e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 12807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 12817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1282636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger op1 = _WorkRep(self) 1283636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger op2 = _WorkRep(other) 1284636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 128572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, str(op1.int * op2.int), resultexp) 1286e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 12877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 12897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __rmul__ = __mul__ 12907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12918aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson def __truediv__(self, other, context=None): 12927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return self / other.""" 1293636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1294267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1295cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return NotImplemented 1296636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 12977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context is None: 12987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = getcontext() 12997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1300636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger sign = self._sign ^ other._sign 1301636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1302636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 1303636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 1304636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 13057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 13067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1307636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity() and other._isinfinity(): 13087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF') 13097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 13107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._isinfinity(): 1311b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[sign] 1312636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1313636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other._isinfinity(): 1314636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context._raise_error(Clamped, 'Division by infinity') 131572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, '0', context.Etiny()) 1316636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1317636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # Special cases for zeroes 1318636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if not other: 1319cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self: 1320cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionUndefined, '0 / 0') 1321636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(DivisionByZero, 'x / 0', sign) 13227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1323cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self: 1324cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista exp = self._exp - other._exp 1325cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff = 0 1326cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1327cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # OK, so neither = 0, INF or NaN 1328cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista shift = len(other._int) - len(self._int) + context.prec + 1 1329cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista exp = self._exp - other._exp - shift 1330cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op1 = _WorkRep(self) 1331cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op2 = _WorkRep(other) 1332cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if shift >= 0: 1333cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff, remainder = divmod(op1.int * 10**shift, op2.int) 1334cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1335cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff, remainder = divmod(op1.int, op2.int * 10**-shift) 1336cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if remainder: 1337cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # result is not exact; adjust to ensure correct rounding 1338cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if coeff % 5 == 0: 1339cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff += 1 1340cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1341cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # result is exact; get as close to ideal exponent as possible 1342cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ideal_exp = self._exp - other._exp 1343cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista while exp < ideal_exp and coeff % 10 == 0: 1344cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff //= 10 1345cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista exp += 1 13467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 134772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(sign, str(coeff), exp) 1348cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans._fix(context) 13497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1350cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista def _divide(self, other, context): 1351cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista """Return (self // other, self % other), to context.prec precision. 13527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1353cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista Assumes that neither self nor other is a NaN, that self is not 1354cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista infinite and that other is nonzero. 1355cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista """ 1356cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista sign = self._sign ^ other._sign 1357cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other._isinfinity(): 1358cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ideal_exp = self._exp 1359cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1360cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ideal_exp = min(self._exp, other._exp) 13617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1362cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista expdiff = self.adjusted() - other.adjusted() 1363cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self or other._isinfinity() or expdiff <= -2: 136472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return (_dec_from_triple(sign, '0', 0), 1365cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista self._rescale(ideal_exp, context.rounding)) 1366cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if expdiff <= context.prec: 1367cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op1 = _WorkRep(self) 1368cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op2 = _WorkRep(other) 1369cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if op1.exp >= op2.exp: 1370cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op1.int *= 10**(op1.exp - op2.exp) 1371cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1372cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op2.int *= 10**(op2.exp - op1.exp) 1373cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista q, r = divmod(op1.int, op2.int) 1374cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if q < 10**context.prec: 137572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return (_dec_from_triple(sign, str(q), 0), 137672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista _dec_from_triple(self._sign, str(r), ideal_exp)) 13777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1378cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # Here the quotient is too large to be representable 1379cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = context._raise_error(DivisionImpossible, 1380cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 'quotient too large in //, % or divmod') 1381cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans, ans 13827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 13838aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson def __rtruediv__(self, other, context=None): 13848aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson """Swaps self/other and returns __truediv__.""" 1385636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1386267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1387267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 13888aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson return other.__truediv__(self, context=context) 13898aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson 13908aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson __div__ = __truediv__ 13918aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson __rdiv__ = __rtruediv__ 13927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 13937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __divmod__(self, other, context=None): 13947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1395cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista Return (self // other, self % other) 13967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1397cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista other = _convert_other(other) 1398cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other is NotImplemented: 1399cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return other 1400cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1401cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if context is None: 1402cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context = getcontext() 1403cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1404cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = self._check_nans(other, context) 1405cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if ans: 1406cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return (ans, ans) 1407cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1408cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista sign = self._sign ^ other._sign 1409cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self._isinfinity(): 1410cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other._isinfinity(): 1411cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = context._raise_error(InvalidOperation, 'divmod(INF, INF)') 1412cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans, ans 1413cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1414b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return (_SignedInfinity[sign], 1415cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context._raise_error(InvalidOperation, 'INF % x')) 1416cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1417cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not other: 1418cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self: 1419cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = context._raise_error(DivisionUndefined, 'divmod(0, 0)') 1420cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans, ans 1421cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1422cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return (context._raise_error(DivisionByZero, 'x // 0', sign), 1423cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context._raise_error(InvalidOperation, 'x % 0')) 1424cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1425cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista quotient, remainder = self._divide(other, context) 1426e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista remainder = remainder._fix(context) 1427cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return quotient, remainder 14287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rdivmod__(self, other, context=None): 14307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Swaps self/other and returns __divmod__.""" 1431636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1432267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1433267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 14347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return other.__divmod__(self, context=context) 14357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __mod__(self, other, context=None): 14377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 14387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self % other 14397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1440636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1441267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1442267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 14437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1444cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if context is None: 1445cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context = getcontext() 14467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1447cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = self._check_nans(other, context) 1448cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if ans: 1449cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans 14507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1451cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self._isinfinity(): 1452cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(InvalidOperation, 'INF % x') 1453cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista elif not other: 1454cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self: 1455cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(InvalidOperation, 'x % 0') 1456cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1457cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionUndefined, '0 % 0') 1458cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1459cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista remainder = self._divide(other, context)[1] 1460e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista remainder = remainder._fix(context) 1461cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return remainder 14627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rmod__(self, other, context=None): 14647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Swaps self/other and returns __mod__.""" 1465636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1466267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1467267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 14687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return other.__mod__(self, context=context) 14697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def remainder_near(self, other, context=None): 14717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 14727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Remainder nearest to 0- abs(remainder-near) <= other/2 14737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1474636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 1475636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 14767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1477353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 14787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1479353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 1480353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 1481353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 14827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1483353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self == +/-infinity -> InvalidOperation 1484353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 1485353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1486353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'remainder_near(infinity, x)') 14877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1488353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # other == 0 -> either InvalidOperation or DivisionUndefined 1489353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 1490353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self: 1491353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1492353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'remainder_near(x, 0)') 1493353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1494353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(DivisionUndefined, 1495353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'remainder_near(0, 0)') 14967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1497353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # other = +/-infinity -> remainder = self 1498353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinfinity(): 1499353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = Decimal(self) 1500353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 15017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1502353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self = 0 -> remainder = self, with ideal exponent 1503353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ideal_exponent = min(self._exp, other._exp) 1504353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 150572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(self._sign, '0', ideal_exponent) 1506353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 15077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1508353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # catch most cases of large or small quotient 1509353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista expdiff = self.adjusted() - other.adjusted() 1510353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if expdiff >= context.prec + 1: 1511353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expdiff >= prec+1 => abs(self/other) > 10**prec 1512cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionImpossible) 1513353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if expdiff <= -2: 1514353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expdiff <= -2 => abs(self/other) < 0.1 1515353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(ideal_exponent, context.rounding) 1516353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 15177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1518353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adjust both arguments to have the same exponent, then divide 1519353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op1 = _WorkRep(self) 1520353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op2 = _WorkRep(other) 1521353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op1.exp >= op2.exp: 1522353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op1.int *= 10**(op1.exp - op2.exp) 15237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 1524353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op2.int *= 10**(op2.exp - op1.exp) 1525353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q, r = divmod(op1.int, op2.int) 1526353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # remainder is r*10**ideal_exponent; other is +/-op2.int * 1527353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 10**ideal_exponent. Apply correction to ensure that 1528353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # abs(remainder) <= abs(other)/2 1529353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if 2*r + (q&1) > op2.int: 1530353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista r -= op2.int 1531353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q += 1 1532353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1533353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if q >= 10**context.prec: 1534cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionImpossible) 1535353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1536353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result has same sign as self unless r is negative 1537353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = self._sign 1538353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if r < 0: 1539353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = 1-sign 1540353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista r = -r 15417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 154272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(sign, str(r), ideal_exponent) 1543353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 15447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 15457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __floordiv__(self, other, context=None): 15467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """self // other""" 1547cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista other = _convert_other(other) 1548cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other is NotImplemented: 1549cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return other 1550cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1551cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if context is None: 1552cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context = getcontext() 1553cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1554cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = self._check_nans(other, context) 1555cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if ans: 1556cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans 1557cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1558cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self._isinfinity(): 1559cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other._isinfinity(): 1560cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(InvalidOperation, 'INF // INF') 1561cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1562b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[self._sign ^ other._sign] 1563cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1564cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not other: 1565cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self: 1566cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionByZero, 'x // 0', 1567cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista self._sign ^ other._sign) 1568cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1569cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionUndefined, '0 // 0') 1570cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1571cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return self._divide(other, context)[0] 15727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 15737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rfloordiv__(self, other, context=None): 15747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Swaps self/other and returns __floordiv__.""" 1575636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1576267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1577267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 15787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return other.__floordiv__(self, context=context) 15797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 15807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __float__(self): 15817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Float representation.""" 1582088cec3ab70f83564d14a43f802e01d63a5fc905Mark Dickinson if self._isnan(): 1583088cec3ab70f83564d14a43f802e01d63a5fc905Mark Dickinson if self.is_snan(): 1584088cec3ab70f83564d14a43f802e01d63a5fc905Mark Dickinson raise ValueError("Cannot convert signaling NaN to float") 1585088cec3ab70f83564d14a43f802e01d63a5fc905Mark Dickinson s = "-nan" if self._sign else "nan" 1586088cec3ab70f83564d14a43f802e01d63a5fc905Mark Dickinson else: 1587088cec3ab70f83564d14a43f802e01d63a5fc905Mark Dickinson s = str(self) 1588088cec3ab70f83564d14a43f802e01d63a5fc905Mark Dickinson return float(s) 15897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 15907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __int__(self): 159146b0802ccd9a9c47b27a285526fbb2a8bee5b8cbBrett Cannon """Converts self to an int, truncating if necessary.""" 1592636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 1593636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isnan(): 1594968f1690d39e8c825f9af1b634ff5b5a9517c290Mark Dickinson raise ValueError("Cannot convert NaN to integer") 1595636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger elif self._isinfinity(): 1596968f1690d39e8c825f9af1b634ff5b5a9517c290Mark Dickinson raise OverflowError("Cannot convert infinity to integer") 1597353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista s = (-1)**self._sign 15987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp >= 0: 159972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return s*int(self._int)*10**self._exp 1600605ed0248375bbf87bbd1d80afc7c6918fd3ce2bRaymond Hettinger else: 160172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return s*int(self._int[:self._exp] or '0') 16027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 16035a05364049a7b0c3eeee94fb7b77e6b41036b3aeRaymond Hettinger __trunc__ = __int__ 16045a05364049a7b0c3eeee94fb7b77e6b41036b3aeRaymond Hettinger 1605116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger def real(self): 1606116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger return self 160765808ff0c08e60572cf81ebe5dc24794a706f390Mark Dickinson real = property(real) 1608116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger 1609116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger def imag(self): 1610116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger return Decimal(0) 161165808ff0c08e60572cf81ebe5dc24794a706f390Mark Dickinson imag = property(imag) 1612116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger 1613116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger def conjugate(self): 1614116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger return self 1615116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger 1616116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger def __complex__(self): 1617116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger return complex(float(self)) 1618116f72fa5ceb864efcc47da2f8c437fd29c7e8b3Raymond Hettinger 16197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __long__(self): 16207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Converts to a long. 16217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 16227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Equivalent to long(int(self)) 16237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 16247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return long(self.__int__()) 16257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1626353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _fix_nan(self, context): 1627353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Decapitate the payload of a NaN to fit the context""" 1628353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista payload = self._int 1629353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1630353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # maximum length of payload is precision if _clamp=0, 1631353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # precision-1 if _clamp=1. 1632353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista max_payload_len = context.prec - context._clamp 1633353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if len(payload) > max_payload_len: 163472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista payload = payload[len(payload)-max_payload_len:].lstrip('0') 163572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, payload, self._exp, True) 16366c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 1637353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1638dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger def _fix(self, context): 16397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Round if it is necessary to keep self within prec precision. 16407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 16417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Rounds and fixes the exponent. Does not raise on a sNaN. 16427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 16437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Arguments: 16447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self - Decimal instance 16457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context - context used. 16467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1647636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1648353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 1649353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isnan(): 1650353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # decapitate payload if necessary 1651353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._fix_nan(context) 1652353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1653353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self is +/-Infinity; return unaltered 16546c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 16557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1656353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if self is zero then exponent should be between Etiny and 1657353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Emax if _clamp==0, and between Etiny and Etop if _clamp==1. 1658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Etiny = context.Etiny() 1659353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Etop = context.Etop() 16607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 1661353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_max = [context.Emax, Etop][context._clamp] 1662353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista new_exp = min(max(self._exp, Etiny), exp_max) 1663353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if new_exp != self._exp: 1664353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Clamped) 166572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, '0', new_exp) 16667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 16676c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 16687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1669353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp_min is the smallest allowable exponent of the result, 1670353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # equal to max(self.adjusted()-context.prec+1, Etiny) 1671353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_min = len(self._int) + self._exp - context.prec 1672353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if exp_min > Etop: 1673353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # overflow: exp_min > Etop iff self.adjusted() > Emax 16744f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson ans = context._raise_error(Overflow, 'above Emax', self._sign) 1675353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 1676353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 16774f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson return ans 16784f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 1679353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_is_subnormal = exp_min < Etiny 1680353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_subnormal: 1681353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_min = Etiny 16827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1683353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # round if self has too many digits 1684353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp < exp_min: 16852ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista digits = len(self._int) + self._exp - exp_min 16862ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if digits < 0: 16872ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista self = _dec_from_triple(self._sign, '1', exp_min-1) 16882ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista digits = 0 16894f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson rounding_method = self._pick_rounding_function[context.rounding] 1690d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger changed = rounding_method(self, digits) 16912ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = self._int[:digits] or '0' 16924f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if changed > 0: 16932ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = str(int(coeff)+1) 16944f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if len(coeff) > context.prec: 16954f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson coeff = coeff[:-1] 16964f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson exp_min += 1 16972ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista 16984f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # check whether the rounding pushed the exponent out of range 16994f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if exp_min > Etop: 17004f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson ans = context._raise_error(Overflow, 'above Emax', self._sign) 17014f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson else: 17024f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson ans = _dec_from_triple(self._sign, coeff, exp_min) 17034f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 17044f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # raise the appropriate signals, taking care to respect 17054f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # the precedence described in the specification 17064f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if changed and self_is_subnormal: 17074f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Underflow) 17084f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if self_is_subnormal: 17094f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Subnormal) 17102ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if changed: 1711353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 17124f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Rounded) 17134f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if not ans: 17144f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # raise Clamped on underflow to 0 17154f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Clamped) 17167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 17177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 17184f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if self_is_subnormal: 17194f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Subnormal) 17204f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 1721353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fold down if _clamp == 1 and self has too few digits 1722353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context._clamp == 1 and self._exp > Etop: 1723353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Clamped) 172472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self_padded = self._int + '0'*(self._exp - Etop) 172572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, self_padded, Etop) 17267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1727353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # here self was representable to begin with; return unchanged 17286c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 17297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1730353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for each of the rounding functions below: 1731353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self is a finite, nonzero Decimal 1732353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # prec is an integer satisfying 0 <= prec < len(self._int) 17332ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # 17342ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # each function returns either -1, 0, or 1, as follows: 17352ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # 1 indicates that self should be rounded up (away from zero) 17362ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # 0 indicates that self should be truncated, and that all the 17372ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # digits to be truncated are zeros (so the value is unchanged) 17382ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # -1 indicates that there are nonzero digits to be truncated 17397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1740353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_down(self, prec): 1741353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Also known as round-towards-0, truncate.""" 17422ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if _all_zeros(self._int, prec): 17432ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return 0 17442ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista else: 17452ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 17467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1747353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_up(self, prec): 1748353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds away from 0.""" 17492ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 17507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1751353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_half_up(self, prec): 1752353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds 5 up (away from 0)""" 175372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if self._int[prec] in '56789': 17542ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return 1 17552ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista elif _all_zeros(self._int, prec): 17562ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return 0 1757353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 17582ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 17597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1760353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_half_down(self, prec): 17617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Round 5 down""" 17622ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if _exact_half(self._int, prec): 17632ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 17642ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista else: 17652ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return self._round_half_up(prec) 17667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1767353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_half_even(self, prec): 1768353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Round 5 to even, rest to nearest.""" 17692ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if _exact_half(self._int, prec) and \ 17702ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista (prec == 0 or self._int[prec-1] in '02468'): 17712ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 1772353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 17732ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return self._round_half_up(prec) 17747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1775353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_ceiling(self, prec): 17767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Rounds up (not away from 0 if negative.)""" 17777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign: 1778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._round_down(prec) 17797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 17802ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 17817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1782353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_floor(self, prec): 17837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Rounds down (not towards 0 if negative)""" 17847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self._sign: 1785353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._round_down(prec) 17867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 17872ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 17887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1789353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_05up(self, prec): 1790353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Round down unless digit prec-1 is 0 or 5.""" 17912ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if prec and self._int[prec-1] not in '05': 1792353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._round_down(prec) 17932ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista else: 17942ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 1795353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1796e4579c33802f555594678ead8d4a163f033e8a54Raymond Hettinger _pick_rounding_function = dict( 1797d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_DOWN = _round_down, 1798d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_UP = _round_up, 1799d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_HALF_UP = _round_half_up, 1800d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_HALF_DOWN = _round_half_down, 1801d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_HALF_EVEN = _round_half_even, 1802d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_CEILING = _round_ceiling, 1803d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_FLOOR = _round_floor, 1804d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger ROUND_05UP = _round_05up, 1805e4579c33802f555594678ead8d4a163f033e8a54Raymond Hettinger ) 1806e4579c33802f555594678ead8d4a163f033e8a54Raymond Hettinger 1807353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def fma(self, other, third, context=None): 1808353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Fused multiply-add. 18097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1810353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Returns self*other+third with no rounding of the intermediate 1811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista product self*other. 1812353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1813353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self and other are multiplied together, with no rounding of 1814353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the result. The third operand is then added to the result, 1815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista and a single final rounding is performed. 18167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 18197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 182058f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista # compute product; raise InvalidOperation if either operand is 182158f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista # a signaling NaN or if the product is zero times infinity. 182258f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if self._is_special or other._is_special: 182358f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if context is None: 182458f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista context = getcontext() 182558f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if self._exp == 'N': 18260f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista return context._raise_error(InvalidOperation, 'sNaN', self) 182758f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if other._exp == 'N': 18280f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista return context._raise_error(InvalidOperation, 'sNaN', other) 182958f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if self._exp == 'n': 183058f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = self 183158f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista elif other._exp == 'n': 183258f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = other 183358f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista elif self._exp == 'F': 183458f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if not other: 183558f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista return context._raise_error(InvalidOperation, 183658f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista 'INF * 0 in fma') 1837b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger product = _SignedInfinity[self._sign ^ other._sign] 183858f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista elif other._exp == 'F': 183958f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if not self: 184058f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista return context._raise_error(InvalidOperation, 184158f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista '0 * INF in fma') 1842b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger product = _SignedInfinity[self._sign ^ other._sign] 184358f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista else: 184458f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = _dec_from_triple(self._sign ^ other._sign, 184558f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista str(int(self._int) * int(other._int)), 184658f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista self._exp + other._exp) 18477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 184858f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista third = _convert_other(third, raiseit=True) 184958f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista return product.__add__(third, context) 18507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1851353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _power_modulo(self, other, modulo, context=None): 1852353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Three argument version of __pow__""" 18537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1854353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if can't convert other and modulo to Decimal, raise 1855353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # TypeError; there's no point returning NotImplemented (no 1856353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # equivalent of __rpow__ for three argument pow) 1857353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 1858353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo = _convert_other(modulo, raiseit=True) 18597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1860353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 1861353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 18627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1863353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # deal with NaNs: if there are any sNaNs then first one wins, 1864353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (i.e. behaviour for NaNs is identical to that of fma) 1865353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_is_nan = self._isnan() 1866353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other_is_nan = other._isnan() 1867353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo_is_nan = modulo._isnan() 1868353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan or other_is_nan or modulo_is_nan: 1869353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan == 2: 1870353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 18710f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista self) 1872353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_is_nan == 2: 1873353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 18740f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista other) 1875353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if modulo_is_nan == 2: 1876353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 18770f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista modulo) 1878353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan: 18796c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return self._fix_nan(context) 1880353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_is_nan: 18816c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return other._fix_nan(context) 18826c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return modulo._fix_nan(context) 1883353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1884353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # check inputs: we apply same restrictions as Python's pow() 1885353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (self._isinteger() and 1886353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other._isinteger() and 1887353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo._isinteger()): 1888353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1889353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'pow() 3rd argument not allowed ' 1890353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'unless all arguments are integers') 1891353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other < 0: 1892353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1893353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'pow() 2nd argument cannot be ' 1894353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'negative when 3rd argument specified') 1895353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not modulo: 1896353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1897353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'pow() 3rd argument cannot be 0') 1898353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1899353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # additional restriction for decimal: the modulus must be less 1900353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # than 10**prec in absolute value 1901353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if modulo.adjusted() >= context.prec: 1902353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1903353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'insufficient precision: pow() 3rd ' 1904353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'argument must not have more than ' 1905353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'precision digits') 1906353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1907353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # define 0**0 == NaN, for consistency with two-argument pow 1908353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (even though it hurts!) 1909353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other and not self: 1910353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1911353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'at least one of pow() 1st argument ' 1912353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'and 2nd argument must be nonzero ;' 1913353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '0**0 is not defined') 1914353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1915353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute sign of result 1916353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._iseven(): 1917353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = 0 1918353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1919353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = self._sign 1920353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1921353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # convert modulo to a Python integer, and self and other to 1922353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Decimal integers (i.e. force their exponents to be >= 0) 1923353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo = abs(int(modulo)) 1924353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = _WorkRep(self.to_integral_value()) 1925353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exponent = _WorkRep(other.to_integral_value()) 1926353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1927353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute result using integer pow() 1928353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo 1929353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for i in xrange(exponent.exp): 1930353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = pow(base, 10, modulo) 1931353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = pow(base, exponent.int, modulo) 1932353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 193372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, str(base), 0) 1934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _power_exact(self, other, p): 1936353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Attempt to compute self**other exactly. 1937353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1938353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Given Decimals self and other and an integer p, attempt to 1939353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista compute an exact result for the power self**other, with p 1940353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits of precision. Return None if self**other is not 1941353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exactly representable in p digits. 1942353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1943353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Assumes that elimination of special cases has already been 1944353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista performed: self and other must both be nonspecial; self must 1945353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista be positive and not numerically equal to 1; other must be 1946353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista nonzero. For efficiency, other._exp should not be too large, 1947353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista so that 10**abs(other._exp) is a feasible calculation.""" 1948353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1949a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # In the comments below, we write x for the value of self and y for the 1950a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # value of other. Write x = xc*10**xe and abs(y) = yc*10**ye, with xc 1951a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # and yc positive integers not divisible by 10. 1952353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1953353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # The main purpose of this method is to identify the *failure* 1954353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # of x**y to be exactly representable with as little effort as 1955353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # possible. So we look for cheap and easy tests that 1956353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # eliminate the possibility of x**y being exact. Only if all 1957353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # these tests are passed do we go on to actually compute x**y. 1958353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1959a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # Here's the main idea. Express y as a rational number m/n, with m and 1960a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # n relatively prime and n>0. Then for x**y to be exactly 1961a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # representable (at *any* precision), xc must be the nth power of a 1962a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # positive integer and xe must be divisible by n. If y is negative 1963a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # then additionally xc must be a power of either 2 or 5, hence a power 1964a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # of 2**n or 5**n. 1965353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1966353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # There's a limit to how small |y| can be: if y=m/n as above 1967353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # then: 1968353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1969353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (1) if xc != 1 then for the result to be representable we 1970353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # need xc**(1/n) >= 2, and hence also xc**|y| >= 2. So 1971353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <= 1972353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 2**(1/|y|), hence xc**|y| < 2 and the result is not 1973353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # representable. 1974353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1975353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1. Hence if 1976353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # |y| < 1/|xe| then the result is not representable. 1977353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1978353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Note that since x is not equal to 1, at least one of (1) and 1979353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (2) must apply. Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) < 1980353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye. 1981353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1982353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # There's also a limit to how large y can be, at least if it's 1983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # positive: the normalized result will have coefficient xc**y, 1984353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # so if it's representable then xc**y < 10**p, and y < 1985353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p/log10(xc). Hence if y*log10(xc) >= p then the result is 1986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # not exactly representable. 1987353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1988353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye, 1989353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # so |y| < 1/xe and the result is not representable. 1990353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y| 1991353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # < 1/nbits(xc). 1992353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1993353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista x = _WorkRep(self) 1994353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc, xe = x.int, x.exp 1995353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while xc % 10 == 0: 1996353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc //= 10 1997353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe += 1 1998353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1999353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _WorkRep(other) 2000353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc, ye = y.int, y.exp 2001353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while yc % 10 == 0: 2002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc //= 10 2003353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ye += 1 2004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # case where xc == 1: result is 10**(xe*y), with xe*y 2006353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # required to be an integer 2007353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc == 1: 2008e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson xe *= yc 2009e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson # result is now 10**(xe * 10**ye); xe * 10**ye must be integral 2010e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson while xe % 10 == 0: 2011e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson xe //= 10 2012e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson ye += 1 2013e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson if ye < 0: 2014e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson return None 2015e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson exponent = xe * 10**ye 2016353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if y.sign == 1: 2017353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exponent = -exponent 2018353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if other is a nonnegative integer, use ideal exponent 2019353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger() and other._sign == 0: 2020353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ideal_exponent = self._exp*int(other) 2021353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = min(exponent-ideal_exponent, p-1) 2022353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2023353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = 0 202472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, '1' + '0'*zeros, exponent-zeros) 2025353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2026353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # case where y is negative: xc must be either a power 2027353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # of 2 or a power of 5. 2028353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if y.sign == 1: 2029353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista last_digit = xc % 10 2030353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if last_digit in (2,4,6,8): 2031353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # quick test for power of 2 2032353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc & -xc != xc: 2033353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2034353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # now xc is a power of 2; e is its exponent 2035353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = _nbits(xc)-1 2036a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 2037a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # We now have: 2038a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2039a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # x = 2**e * 10**xe, e > 0, and y < 0. 2040a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2041a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # The exact result is: 2042a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2043a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # x**y = 5**(-e*y) * 10**(e*y + xe*y) 2044a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2045a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # provided that both e*y and xe*y are integers. Note that if 2046a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 5**(-e*y) >= 10**p, then the result can't be expressed 2047a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # exactly with p digits of precision. 2048a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2049a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # Using the above, we can guard against large values of ye. 2050a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 93/65 is an upper bound for log(10)/log(5), so if 2051a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2052a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # ye >= len(str(93*p//65)) 2053a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2054a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # then 2055a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2056a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # -e*y >= -y >= 10**ye > 93*p/65 > p*log(10)/log(5), 2057a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # 2058a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # so 5**(-e*y) >= 10**p, and the coefficient of the result 2059a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # can't be expressed in p digits. 2060a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 2061a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # emax >= largest e such that 5**e < 10**p. 2062a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson emax = p*93//65 2063a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson if ye >= len(str(emax)): 2064a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson return None 2065a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 2066a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # Find -e*y and -xe*y; both must be integers 2067a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson e = _decimal_lshift_exact(e * yc, ye) 2068a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson xe = _decimal_lshift_exact(xe * yc, ye) 2069a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson if e is None or xe is None: 2070a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson return None 2071a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 2072a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson if e > emax: 2073353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2074353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = 5**e 2075353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2076353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif last_digit == 5: 2077353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # e >= log_5(xc) if xc is a power of 5; we have 2078353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # equality all the way up to xc=5**2658 2079353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = _nbits(xc)*28//65 2080353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc, remainder = divmod(5**e, xc) 2081353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if remainder: 2082353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2083353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while xc % 5 == 0: 2084353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc //= 5 2085353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e -= 1 2086a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 2087a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # Guard against large values of ye, using the same logic as in 2088a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # the 'xc is a power of 2' branch. 10/3 is an upper bound for 2089a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # log(10)/log(2). 2090a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson emax = p*10//3 2091a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson if ye >= len(str(emax)): 2092a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson return None 2093a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 2094a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson e = _decimal_lshift_exact(e * yc, ye) 2095a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson xe = _decimal_lshift_exact(xe * yc, ye) 2096a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson if e is None or xe is None: 2097a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson return None 2098a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 2099a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson if e > emax: 2100353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2101353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = 2**e 2102353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2103353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 21047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2105353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc >= 10**p: 2106353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2107353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe = -e-xe 210872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, str(xc), xe) 21097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2110353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # now y is positive; find m and n such that y = m/n 2111353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ye >= 0: 2112353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m, n = yc*10**ye, 1 2113353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2114353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xe != 0 and len(str(abs(yc*xe))) <= -ye: 2115353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2116353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc_bits = _nbits(xc) 2117353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc != 1 and len(str(abs(yc)*xc_bits)) <= -ye: 2118353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2119353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m, n = yc, 10**(-ye) 2120353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while m % 2 == n % 2 == 0: 2121353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m //= 2 2122353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n //= 2 2123353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while m % 5 == n % 5 == 0: 2124353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m //= 5 2125353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n //= 5 2126353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2127353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute nth root of xc*10**xe 2128353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n > 1: 2129353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if 1 < xc < 2**n then xc isn't an nth power 2130353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc != 1 and xc_bits <= n: 2131353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2132353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2133353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe, rem = divmod(xe, n) 2134353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rem != 0: 2135353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2136353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2137353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute nth root of xc using Newton's method 2138353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista a = 1L << -(-_nbits(xc)//n) # initial estimate 2139353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2140353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q, r = divmod(xc, a**(n-1)) 2141353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if a <= q: 2142353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 2143353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2144353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista a = (a*(n-1) + q)//n 2145353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (a == q and r == 0): 2146353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2147353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = a 2148353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2149353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # now xc*10**xe is the nth root of the original xc*10**xe 2150353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute mth power of xc*10**xe 2151353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2152353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if m > p*100//_log10_lb(xc) then m > p/log10(xc), hence xc**m > 2153353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 10**p and the result is not representable. 2154353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc > 1 and m > p*100//_log10_lb(xc): 2155353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2156353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = xc**m 2157353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe *= m 2158353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc > 10**p: 2159353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 2160353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2161353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # by this point the result *is* exactly representable 2162353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adjust the exponent to get as close as possible to the ideal 2163353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exponent, if necessary 2164353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista str_xc = str(xc) 2165353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger() and other._sign == 0: 2166353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ideal_exponent = self._exp*int(other) 2167353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = min(xe-ideal_exponent, p-len(str_xc)) 2168353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2169353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = 0 217072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, str_xc+'0'*zeros, xe-zeros) 2171cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 2172353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def __pow__(self, other, modulo=None, context=None): 2173353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return self ** other [ % modulo]. 21747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2175353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With two arguments, compute self**other. 21767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2177353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With three arguments, compute (self**other) % modulo. For the 2178353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista three argument form, the following restrictions on the 2179353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista arguments hold: 21807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2181353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - all three arguments must be integral 2182353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - other must be nonnegative 2183353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - either self or other (or both) must be nonzero 2184353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - modulo must be nonzero and must have at most p digits, 2185353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista where p is the context precision. 21867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2187353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista If any of these restrictions is violated the InvalidOperation 2188353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista flag is raised. 2189353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2190353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result of pow(self, other, modulo) is identical to the 2191353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista result that would be obtained by computing (self**other) % 2192353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo with unbounded precision, but is computed more 2193353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista efficiently. It is always exact. 2194353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2195353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2196353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if modulo is not None: 2197353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._power_modulo(other, modulo, context) 21987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2199636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 2200267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 2201267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 22027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2203353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2204353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 22057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2206353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # either argument is a NaN => result is NaN 2207353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 2208353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2209353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 22107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2211353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity) 2212353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 2213353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 2214353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, '0 ** 0') 2215353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2216b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 22177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2218353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result has sign 1 iff self._sign is 1 and other is an odd integer 2219353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista result_sign = 0 2220353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 1: 2221353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger(): 2222353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other._iseven(): 2223353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista result_sign = 1 2224353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2225353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # -ve**noninteger = NaN 2226353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (-0)**noninteger = 0**noninteger 2227353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self: 2228353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2229353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'x ** y with x negative and y not an integer') 2230353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # negate self, without doing any unwanted rounding 223172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self = self.copy_negate() 2232353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2233353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 0**(+ve or Inf)= 0; 0**(-ve or -Inf) = Infinity 2234353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 2235353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._sign == 0: 223672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '0', 0) 2237353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2238b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[result_sign] 2239353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2240353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Inf**(+ve or Inf) = Inf; Inf**(-ve or -Inf) = 0 2241353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 2242353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._sign == 0: 2243b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[result_sign] 2244353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 224572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '0', 0) 2246353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2247353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1**other = 1, but the choice of exponent and the flags 2248353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # depend on the exponent of self, and on whether other is a 2249353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # positive integer, a negative integer, or neither 2250b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger if self == _One: 2251353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger(): 2252353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp = max(self._exp*max(int(other), 0), 2253353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1-context.prec) but evaluating int(other) directly 2254353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # is dangerous until we know other is small (other 2255353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # could be 1e999999999) 2256353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._sign == 1: 2257353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplier = 0 2258353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif other > context.prec: 2259353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplier = context.prec 2260353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2261353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplier = int(other) 2262353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2263353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp = self._exp * multiplier 2264353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if exp < 1-context.prec: 2265353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp = 1-context.prec 2266353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 2267353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2268353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 2269353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 2270353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp = 1-context.prec 2271353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 227272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '1'+'0'*-exp, exp) 2273353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2274353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute adjusted exponent of self 2275353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_adj = self.adjusted() 2276353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2277353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self ** infinity is infinity if self > 1, 0 if self < 1 2278353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self ** -infinity is infinity if self < 1, 0 if self > 1 2279353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinfinity(): 2280353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if (other._sign == 0) == (self_adj < 0): 228172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '0', 0) 2282353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2283b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _SignedInfinity[result_sign] 2284353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2285353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # from here on, the result always goes through the call 2286353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # to _fix at the end of this function. 2287353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = None 22884f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson exact = False 2289353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2290353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # crude test to catch cases of extreme overflow/underflow. If 2291353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(self)*other >= 10**bound and bound >= len(str(Emax)) 2292353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # then 10**bound >= 10**len(str(Emax)) >= Emax+1 and hence 2293353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self**other >= 10**(Emax+1), so overflow occurs. The test 2294353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for underflow is similar. 2295353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista bound = self._log10_exp_bound() + other.adjusted() 2296353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if (self_adj >= 0) == (other._sign == 0): 2297353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self > 1 and other +ve, or self < 1 and other -ve 2298353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # possibility of overflow 2299353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if bound >= len(str(context.Emax)): 230072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(result_sign, '1', context.Emax+1) 2301353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2302353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self > 1 and other -ve, or self < 1 and other +ve 2303353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # possibility of underflow to 0 2304353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Etiny = context.Etiny() 2305353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if bound >= len(str(-Etiny)): 230672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(result_sign, '1', Etiny-1) 2307353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2308353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # try for an exact result with precision +1 2309353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans is None: 2310353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._power_exact(other, context.prec + 1) 2311e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson if ans is not None: 2312e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson if result_sign == 1: 2313e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson ans = _dec_from_triple(1, ans._int, ans._exp) 2314e85aa739ab0d396665908c0a489cfdeb49c88674Mark Dickinson exact = True 2315353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2316353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # usual case: inexact result, x**y computed directly as exp(y*log(x)) 2317353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans is None: 2318353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 2319353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista x = _WorkRep(self) 2320353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc, xe = x.int, x.exp 2321353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _WorkRep(other) 2322353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc, ye = y.int, y.exp 2323353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if y.sign == 1: 2324353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc = -yc 2325353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2326353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute correctly rounded result: start with precision +3, 2327353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # then increase precision until result is unambiguously roundable 2328353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra = 3 2329353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2330353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = _dpower(xc, xe, yc, ye, p+extra) 2331353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(coeff))-p-1)): 2332353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 2333353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra += 3 2334353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 233572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(result_sign, str(coeff), exp) 2336353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 23374f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # unlike exp, ln and log10, the power function respects the 23384f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # rounding mode; no need to switch to ROUND_HALF_EVEN here 23394f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 23404f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # There's a difficulty here when 'other' is not an integer and 23414f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # the result is exact. In this case, the specification 23424f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # requires that the Inexact flag be raised (in spite of 23434f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # exactness), but since the result is exact _fix won't do this 23444f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # for us. (Correspondingly, the Underflow signal should also 23454f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # be raised for subnormal results.) We can't directly raise 23464f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # these signals either before or after calling _fix, since 23474f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # that would violate the precedence for signals. So we wrap 23484f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # the ._fix call in a temporary context, and reraise 23494f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # afterwards. 23504f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if exact and not other._isinteger(): 23514f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # pad with zeros up to length context.prec+1 if necessary; this 23524f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # ensures that the Rounded signal will be raised. 2353353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if len(ans._int) <= context.prec: 23544f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson expdiff = context.prec + 1 - len(ans._int) 235572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(ans._sign, ans._int+'0'*expdiff, 235672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans._exp-expdiff) 2357353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 23584f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # create a copy of the current context, with cleared flags/traps 23594f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson newcontext = context.copy() 23604f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson newcontext.clear_flags() 23614f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson for exception in _signals: 23624f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson newcontext.traps[exception] = 0 23634f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 23644f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # round in the new context 23654f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson ans = ans._fix(newcontext) 23664f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 23674f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # raise Inexact, and if necessary, Underflow 23684f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson newcontext._raise_error(Inexact) 23694f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if newcontext.flags[Subnormal]: 23704f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson newcontext._raise_error(Underflow) 23714f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 23724f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # propagate signals to the original context; _fix could 23734f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # have raised any of Overflow, Underflow, Subnormal, 23744f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # Inexact, Rounded, Clamped. Overflow needs the correct 23754f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # arguments. Note that the order of the exceptions is 23764f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # important here. 23774f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if newcontext.flags[Overflow]: 23784f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Overflow, 'above Emax', ans._sign) 23794f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson for exception in Underflow, Subnormal, Inexact, Rounded, Clamped: 23804f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if newcontext.flags[exception]: 23814f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(exception) 23824f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 23834f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson else: 23844f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson ans = ans._fix(context) 23854f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson 2386353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2387353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2388353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def __rpow__(self, other, context=None): 2389353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Swaps self/other and returns __pow__.""" 2390353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other) 2391353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other is NotImplemented: 2392353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other 2393353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other.__pow__(self, context=context) 2394353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2395353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def normalize(self, context=None): 2396353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Normalize- strip trailing 0s, change anything equal to 0 to 0e0""" 2397353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2398353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2399353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2400353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2401353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 2402353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2403353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2404353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2405353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2406353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista dup = self._fix(context) 2407353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if dup._isinfinity(): 2408353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return dup 2409353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2410353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not dup: 241172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(dup._sign, '0', 0) 2412353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_max = [context.Emax, context.Etop()][context._clamp] 2413353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista end = len(dup._int) 24147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp = dup._exp 241572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista while dup._int[end-1] == '0' and exp < exp_max: 24167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp += 1 24177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger end -= 1 241872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(dup._sign, dup._int[:end], exp) 24197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2420bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista def quantize(self, exp, rounding=None, context=None, watchexp=True): 24217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Quantize self so its exponent is the same as that of exp. 24227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 24237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Similar to self._rescale(exp._exp) but with error checking. 24247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2425bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista exp = _convert_other(exp, raiseit=True) 2426bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista 2427353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2428353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2429353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rounding is None: 2430353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context.rounding 2431353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2432636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or exp._is_special: 2433636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(exp, context) 2434636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 2435636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 24367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2437636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if exp._isinfinity() or self._isinfinity(): 2438636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if exp._isinfinity() and self._isinfinity(): 24396c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) # if both are inf, it is OK 2440636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, 2441636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 'quantize with one INF') 2442353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2443bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista # if we're not watching exponents, do a simple rescale 2444bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista if not watchexp: 2445bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista ans = self._rescale(exp._exp, rounding) 2446bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista # raise Inexact and Rounded where appropriate 2447bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista if ans._exp > self._exp: 2448bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista context._raise_error(Rounded) 2449bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista if ans != self: 2450bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista context._raise_error(Inexact) 2451bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista return ans 2452bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista 2453353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp._exp should be between Etiny and Emax 2454353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (context.Etiny() <= exp._exp <= context.Emax): 2455353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2456353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'target exponent out of bounds in quantize') 2457353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2458353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 245972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(self._sign, '0', exp._exp) 2460353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 2461353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2462353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_adjusted = self.adjusted() 2463353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_adjusted > context.Emax: 2464353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2465353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'exponent of quantize result too large for current context') 2466353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_adjusted - exp._exp + 1 > context.prec: 2467353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2468353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'quantize result has too many digits for current context') 2469353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2470353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(exp._exp, rounding) 2471353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans.adjusted() > context.Emax: 2472353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2473353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'exponent of quantize result too large for current context') 2474353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if len(ans._int) > context.prec: 2475353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2476353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'quantize result has too many digits for current context') 2477353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2478353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # raise appropriate flags 24794f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson if ans and ans.adjusted() < context.Emin: 24804f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Subnormal) 2481353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans._exp > self._exp: 2482353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans != self: 2483353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 24844f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Rounded) 2485353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 24864f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # call to fix takes care of any necessary folddown, and 24874f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson # signals Clamped if necessary 2488353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 2489353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 24907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 24917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def same_quantum(self, other): 24921a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self and other have the same exponent; otherwise 24931a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return False. 24947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 24951a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista If either operand is a special value, the following rules are used: 24961a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista * return True if both operands are infinities 24971a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista * return True if both operands are NaNs 24981a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista * otherwise, return False. 24997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 25001a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista other = _convert_other(other, raiseit=True) 2501636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 25021a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return (self.is_nan() and other.is_nan() or 25031a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista self.is_infinite() and other.is_infinite()) 25047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self._exp == other._exp 25057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2506353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _rescale(self, exp, rounding): 2507353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rescale self so that the exponent is exp, either by padding with zeros 2508353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista or by truncating digits, using the given rounding mode. 2509353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2510353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Specials are returned without change. This operation is 2511353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista quiet: it raises no flags, and uses no information from the 2512353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context. 25137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 25147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp = exp to scale to (an integer) 2515353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = rounding mode 25167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2517636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 25186c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 25197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 252072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, '0', exp) 25217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2522353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp >= exp: 2523353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # pad answer with zeros if necessary 252472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, 252572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int + '0'*(self._exp - exp), exp) 25267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2527353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # too many digits; round and lose data. If self.adjusted() < 2528353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp-1, replace self by 10**(exp-1) before rounding 2529353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits = len(self._int) + self._exp - exp 25307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if digits < 0: 253172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self = _dec_from_triple(self._sign, '1', exp-1) 2532353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits = 0 2533d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger this_function = self._pick_rounding_function[rounding] 2534d92232976e1cdc77a28d28b94b897c37e847492bRaymond Hettinger changed = this_function(self, digits) 25352ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = self._int[:digits] or '0' 25362ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if changed == 1: 25372ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = str(int(coeff)+1) 25382ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return _dec_from_triple(self._sign, coeff, exp) 25397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 25401ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson def _round(self, places, rounding): 25411ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson """Round a nonzero, nonspecial Decimal to a fixed number of 25421ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson significant figures, using the given rounding mode. 25431ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 25441ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson Infinities, NaNs and zeros are returned unaltered. 25451ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 25461ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson This operation is quiet: it raises no flags, and uses no 25471ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson information from the context. 25481ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 25491ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson """ 25501ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if places <= 0: 25511ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson raise ValueError("argument should be at least 1 in _round") 25521ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if self._is_special or not self: 25531ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson return Decimal(self) 25541ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson ans = self._rescale(self.adjusted()+1-places, rounding) 25551ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # it can happen that the rescale alters the adjusted exponent; 25561ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # for example when rounding 99.97 to 3 significant figures. 25571ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # When this happens we end up with an extra 0 at the end of 25581ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # the number; a second rescale fixes this. 25591ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if ans.adjusted() != self.adjusted(): 25601ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson ans = ans._rescale(ans.adjusted()+1-places, rounding) 25611ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson return ans 25621ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 2563353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_exact(self, rounding=None, context=None): 2564353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds to a nearby integer. 25657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2566353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista If no rounding mode is specified, take the rounding mode from 2567353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the context. This method raises the Rounded and Inexact flags 2568353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista when appropriate. 25697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2570353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista See also: to_integral_value, which does exactly the same as 2571353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista this method except that it doesn't raise Inexact or Rounded. 2572353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2573636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 2574636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 2575636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 2576636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 25776c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 25787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp >= 0: 25796c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 2580353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 258172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, '0', 0) 2582636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 2583636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 2584353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rounding is None: 2585353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context.rounding 2586353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(0, rounding) 2587353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans != self: 2588353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 25894f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Rounded) 25907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 25917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2592353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_value(self, rounding=None, context=None): 2593353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds to the nearest integer, without raising inexact, rounded.""" 2594353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2595353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2596353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rounding is None: 2597353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context.rounding 2598353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 2599353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2600353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2601353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 26026c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 2603353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp >= 0: 26046c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 2605353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2606353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._rescale(0, rounding) 26077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2608353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the method name changed, but we provide also the old one, for compatibility 2609353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to_integral = to_integral_value 2610353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2611353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def sqrt(self, context=None): 2612353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return the square root of self.""" 26133b24ccbe7e2b43c4373e4b1e988df55978b819e7Mark Dickinson if context is None: 26143b24ccbe7e2b43c4373e4b1e988df55978b819e7Mark Dickinson context = getcontext() 26153b24ccbe7e2b43c4373e4b1e988df55978b819e7Mark Dickinson 2616636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 2617636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 2618636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 2619636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 26207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2621636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity() and self._sign == 0: 2622636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Decimal(self) 26237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 26247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 2625353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exponent = self._exp // 2. sqrt(-0) = -0 262672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(self._sign, '0', self._exp // 2) 2627353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 26287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 26297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign == 1: 26307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0') 26317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2632353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # At this point self represents a positive number. Let p be 2633353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the desired precision and express self in the form c*100**e 2634353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # with c a positive real number and e an integer, c and e 2635353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # being chosen so that 100**(p-1) <= c < 100**p. Then the 2636353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (exact) square root of self is sqrt(c)*10**e, and 10**(p-1) 2637353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # <= sqrt(c) < 10**p, so the closest representable Decimal at 2638353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # precision p is n*10**e where n = round_half_even(sqrt(c)), 2639353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the closest integer to sqrt(c) with the even integer chosen 2640353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # in the case of a tie. 2641353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 2642353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # To ensure correct rounding in all cases, we use the 2643353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # following trick: we compute the square root to an extra 2644353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # place (precision p+1 instead of precision p), rounding down. 2645353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Then, if the result is inexact and its last digit is 0 or 5, 2646353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we increase the last digit to 1 or 6 respectively; if it's 2647353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exact we leave the last digit alone. Now the final round to 2648353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p places (or fewer in the case of underflow) will round 2649353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # correctly and raise the appropriate flags. 2650353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2651353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # use an extra digit of precision 2652353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista prec = context.prec+1 2653353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2654353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # write argument in the form c*100**e where e = self._exp//2 2655353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # is the 'ideal' exponent, to be used if the square root is 2656353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exactly representable. l is the number of 'digits' of c in 2657353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # base 100, so that 100**(l-1) <= c < 100**l. 2658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2659353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = op.exp >> 1 2660353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op.exp & 1: 2661353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = op.int * 10 2662353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = (len(self._int) >> 1) + 1 26637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 2664353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = op.int 2665353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = len(self._int)+1 >> 1 2666353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2667353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rescale so that c has exactly prec base 100 'digits' 2668353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista shift = prec-l 2669353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 2670353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c *= 100**shift 2671353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exact = True 26727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 2673353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, remainder = divmod(c, 100**-shift) 2674353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exact = not remainder 2675353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e -= shift 2676353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2677353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # find n = floor(sqrt(c)) using Newton's method 2678353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n = 10**prec 2679353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2680353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q = c//n 2681353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n <= q: 26827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger break 2683353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2684353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n = n + q >> 1 2685353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exact = exact and n*n == c 2686353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2687353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if exact: 2688353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is exact; rescale to use ideal exponent e 2689353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 2690353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # assert n % 10**shift == 0 2691353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n //= 10**shift 2692353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2693353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n *= 10**-shift 2694353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e += shift 26957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 2696353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is not exact; fix last digit as described above 2697353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n % 5 == 0: 2698353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n += 1 26997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 270072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, str(n), e) 27017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2702353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # round, and fit to current context 2703353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 2704353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 2705dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger ans = ans._fix(context) 2706353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 27077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2708353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 27097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 27107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def max(self, other, context=None): 27117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the larger value. 27127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2713353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Like max(self, other) except if one is not a number, returns 27147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger NaN (and signals if one is sNaN). Also rounds. 27157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2716353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 2717636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 27186c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 27196c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 27206c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 2721636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 272259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If one operand is a quiet NaN and the other is number, then the 2723636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # number is always returned 2724636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger sn = self._isnan() 2725636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger on = other._isnan() 2726636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if sn or on: 2727e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if on == 1 and sn == 0: 2728e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return self._fix(context) 2729e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if sn == 1 and on == 0: 2730e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return other._fix(context) 2731636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self._check_nans(other, context) 27327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 27332fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson c = self._cmp(other) 2734d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger if c == 0: 273559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If both operands are finite and equal in numerical value 2736d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # then an ordering is applied: 2737d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # 273859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If the signs differ then max returns the operand with the 2739d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # positive sign and min returns the operand with the negative sign 2740d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # 274159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If the signs are the same then the exponent is used to select 2742353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the result. This is exactly the ordering used in compare_total. 2743353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 2744353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2745353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 27467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = other 2747353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2748353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 2749636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 2750e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 27517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 27527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def min(self, other, context=None): 27537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the smaller value. 27547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 275559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista Like min(self, other) except if one is not a number, returns 27567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger NaN (and signals if one is sNaN). Also rounds. 27577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2758353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 2759636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 27606c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 27616c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 27626c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 2763636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 276459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If one operand is a quiet NaN and the other is number, then the 2765636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # number is always returned 2766636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger sn = self._isnan() 2767636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger on = other._isnan() 2768636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if sn or on: 2769e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if on == 1 and sn == 0: 2770e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return self._fix(context) 2771e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if sn == 1 and on == 0: 2772e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return other._fix(context) 2773636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self._check_nans(other, context) 27747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 27752fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson c = self._cmp(other) 2776d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger if c == 0: 2777353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 2778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2779353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 2780353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 2781353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 27827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = other 2783636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 2784e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 27857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 27867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _isinteger(self): 27877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether self is an integer""" 2788353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 2789353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return False 27907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp >= 0: 27917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return True 27927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rest = self._int[self._exp:] 279372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return rest == '0'*len(rest) 27947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 27957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _iseven(self): 2796353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns True if self is even. Assumes self is an integer.""" 2797353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self or self._exp > 0: 2798353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return True 279972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self._int[-1+self._exp] in '02468' 28007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 28017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def adjusted(self): 28027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return the adjusted exponent of self""" 28037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger try: 28047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self._exp + len(self._int) - 1 280559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If NaN or Infinity, self._exp is string 28067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger except TypeError: 28077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 28087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2809353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def canonical(self, context=None): 2810353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the same Decimal object. 2811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2812353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista As we do not have different encodings for the same number, the 2813353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista received object already is in its canonical form. 2814353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self 2816353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_signal(self, other, context=None): 2818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares self to the other operand numerically. 2819353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista It's pretty much like compare(), but all NaNs signal, with signaling 2821353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista NaNs taking precedence over quiet NaNs. 2822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 28232fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson other = _convert_other(other, raiseit = True) 28242fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson ans = self._compare_check_nans(other, context) 28252fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson if ans: 28262fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson return ans 2827353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self.compare(other, context=context) 2828353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2829353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total(self, other): 2830353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares self to other using the abstract representations. 2831353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2832353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista This is not like the standard compare, which use their numerical 2833353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value. Note that a total ordering is defined for all possible abstract 2834353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista representations. 2835353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 28360c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 28370c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 2838353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if one is negative and the other is positive, it's easy 2839353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign and not other._sign: 2840b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2841353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._sign and other._sign: 2842b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = self._sign 2844353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2845353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # let's handle both NaN types 2846353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_nan = self._isnan() 2847353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other_nan = other._isnan() 2848353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan or other_nan: 2849353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == other_nan: 28507a7739d75ed505033445248a126830bedb17101bMark Dickinson # compare payloads as though they're integers 28517a7739d75ed505033445248a126830bedb17101bMark Dickinson self_key = len(self._int), self._int 28527a7739d75ed505033445248a126830bedb17101bMark Dickinson other_key = len(other._int), other._int 28537a7739d75ed505033445248a126830bedb17101bMark Dickinson if self_key < other_key: 2854353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2855b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2856353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2857b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 28587a7739d75ed505033445248a126830bedb17101bMark Dickinson if self_key > other_key: 2859353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2860b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2861353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2862b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2863b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Zero 2864353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2865353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2866353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 1: 2867b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2868353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 1: 2869b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2870353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 2: 2871b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2872353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 2: 2873b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2874353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2875353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 1: 2876b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2877353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 1: 2878b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2879353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 2: 2880b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2881353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 2: 2882b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2883353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2884353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self < other: 2885b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2886353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self > other: 2887b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2888353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2889353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp < other._exp: 2890353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2891b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2892353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2893b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2894353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp > other._exp: 2895353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2896b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeOne 2897353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2898b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2899b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Zero 2900353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2901353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2902353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total_mag(self, other): 2903353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares self to other using abstract repr., ignoring sign. 2904353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2905353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Like compare_total, but with operand's sign ignored and assumed to be 0. 2906353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 29070c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 29080c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 2909353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista s = self.copy_abs() 2910353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista o = other.copy_abs() 2911353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return s.compare_total(o) 2912353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2913353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_abs(self): 2914353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy with the sign set to 0. """ 291572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, self._int, self._exp, self._is_special) 2916353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2917353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_negate(self): 2918353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy with the sign inverted.""" 2919353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 292072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, self._int, self._exp, self._is_special) 2921353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 292272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(1, self._int, self._exp, self._is_special) 2923353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2924353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_sign(self, other): 2925353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns self with the sign of other.""" 29266d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson other = _convert_other(other, raiseit=True) 292772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(other._sign, self._int, 292872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._exp, self._is_special) 2929353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2930353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def exp(self, context=None): 2931353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns e ** self.""" 2932353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2933353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2936353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(NaN) = NaN 2937353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2938353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2939353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2940353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2941353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(-Infinity) = 0 2942353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == -1: 2943b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Zero 2944353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2945353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(0) = 1 2946353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 2947b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _One 2948353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2949353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(Infinity) = Infinity 2950353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 2951353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(self) 2952353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2953353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the result is now guaranteed to be inexact (the true 2954353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # mathematical result is transcendental). There's no need to 2955353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # raise Rounded and Inexact here---they'll always be raised as 2956353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # a result of the call to _fix. 2957353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 2958353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista adj = self.adjusted() 2959353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2960353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we only need to do any computation for quite a small range 2961353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # of adjusted exponents---for example, -29 <= adj <= 10 for 2962353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the default context. For smaller exponent the result is 2963353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # indistinguishable from 1 at the given precision, while for 2964353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # larger exponent the result either overflows or underflows. 2965353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 0 and adj > len(str((context.Emax+1)*3)): 2966353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # overflow 296772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '1', context.Emax+1) 2968353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self._sign == 1 and adj > len(str((-context.Etiny()+1)*3)): 2969353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # underflow to 0 297072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '1', context.Etiny()-1) 2971353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self._sign == 0 and adj < -p: 2972353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p+1 digits; final round will raise correct flags 297372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '1' + '0'*(p-1) + '1', -p) 2974353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self._sign == 1 and adj < -p-1: 2975353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p+1 digits; final round will raise correct flags 297672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '9'*(p+1), -p-1) 2977353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # general case 2978353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2979353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2980353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 2981353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op.sign == 1: 2982353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = -c 2983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2984353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute correctly rounded result: increase precision by 2985353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 3 digits at a time until we get an unambiguously 2986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # roundable result 2987353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra = 3 2988353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2989353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = _dexp(c, e, p+extra) 2990353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(coeff))-p-1)): 2991353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 2992353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra += 3 2993353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 299472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, str(coeff), exp) 2995353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2996353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # at this stage, ans should round correctly with *any* 2997353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rounding mode, not just with ROUND_HALF_EVEN 2998353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 2999353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 3000353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 3001353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 3002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3003353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_canonical(self): 30061a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is canonical; otherwise return False. 30071a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista 30081a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista Currently, the encoding of a Decimal instance is always 30091a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista canonical, so this method returns True for any Decimal. 30101a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """ 30111a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return True 3012353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3013353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_finite(self): 30141a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is finite; otherwise return False. 3015353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 30161a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista A Decimal instance is considered finite if it is neither 30171a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista infinite nor a NaN. 3018353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 30191a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return not self._is_special 3020353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3021353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_infinite(self): 30221a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is infinite; otherwise return False.""" 30231a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp == 'F' 3024353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3025353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_nan(self): 30261a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a qNaN or sNaN; otherwise return False.""" 30271a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp in ('n', 'N') 3028353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3029353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_normal(self, context=None): 30301a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a normal number; otherwise return False.""" 30311a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista if self._is_special or not self: 30321a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return False 3033353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3034353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3035a7a52ab7ee58be0afca3a7763fffc928dcf5459dMark Dickinson return context.Emin <= self.adjusted() 3036353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3037353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_qnan(self): 30381a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a quiet NaN; otherwise return False.""" 30391a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp == 'n' 3040353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3041353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_signed(self): 30421a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is negative; otherwise return False.""" 30431a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._sign == 1 3044353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3045353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_snan(self): 30461a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a signaling NaN; otherwise return False.""" 30471a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp == 'N' 3048353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3049353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_subnormal(self, context=None): 30501a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is subnormal; otherwise return False.""" 30511a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista if self._is_special or not self: 30521a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return False 3053353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3054353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 30551a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self.adjusted() < context.Emin 3056353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3057353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_zero(self): 30581a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a zero; otherwise return False.""" 305972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return not self._is_special and self._int == '0' 3060353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3061353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _ln_exp_bound(self): 3062353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute a lower bound for the adjusted exponent of self.ln(). 3063353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In other words, compute r such that self.ln() >= 10**r. Assumes 3064353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista that self is finite and positive and that self != 1. 3065353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3066353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3067353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for 0.1 <= x <= 10 we use the inequalities 1-1/x <= ln(x) <= x-1 3068353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista adj = self._exp + len(self._int) - 1 3069353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj >= 1: 3070353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # argument >= 10; we use 23/10 = 2.3 as a lower bound for ln(10) 3071353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str(adj*23//10)) - 1 3072353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj <= -2: 3073353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # argument <= 0.1 3074353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str((-1-adj)*23//10)) - 1 3075353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 3076353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 3077353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj == 0: 3078353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1 < self < 10 3079353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista num = str(c-10**-e) 3080353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista den = str(c) 3081353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(num) - len(den) - (num < den) 3082353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adj == -1, 0.1 <= self < 1 3083353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return e + len(str(10**-e - c)) - 1 3084353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3085353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3086353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def ln(self, context=None): 3087353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the natural (base e) logarithm of self.""" 3088353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3089353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3090353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3091353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3092353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(NaN) = NaN 3093353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 3094353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3095353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3096353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3097353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(0.0) == -Infinity 3098353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 3099b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeInfinity 3100353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3101353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(Infinity) = Infinity 3102353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 3103b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Infinity 3104353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3105353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(1.0) == 0.0 3106b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger if self == _One: 3107b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Zero 3108353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3109353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(negative) raises InvalidOperation 3110353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 1: 3111353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 3112353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'ln of a negative value') 3113353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3114353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is irrational, so necessarily inexact 3115353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 3116353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 3117353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 3118353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3119353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # correctly rounded result: repeatedly increase precision by 3 3120353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # until we get an unambiguously roundable result 3121353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places = p - self._ln_exp_bound() + 2 # at least p+3 places 3122353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 3123353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff = _dlog(c, e, places) 3124353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # assert len(str(abs(coeff)))-p >= 1 3125353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(abs(coeff)))-p-1)): 3126353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 3127353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places += 3 312872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places) 3129353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3130353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 3131353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 3132353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 3133353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 3134353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3135353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3136353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _log10_exp_bound(self): 3137353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute a lower bound for the adjusted exponent of self.log10(). 3138353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In other words, find r such that self.log10() >= 10**r. 3139353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Assumes that self is finite and positive and that self != 1. 3140353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3141353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3142353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # For x >= 10 or x < 0.1 we only need a bound on the integer 3143353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # part of log10(self), and this comes directly from the 3144353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exponent of x. For 0.1 <= x <= 10 we use the inequalities 3145353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1-1/x <= log(x) <= x-1. If x > 1 we have |log10(x)| > 3146353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (1-1/x)/2.31 > 0. If x < 1 then |log10(x)| > (1-x)/2.31 > 0 3147353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3148353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista adj = self._exp + len(self._int) - 1 3149353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj >= 1: 3150353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self >= 10 3151353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str(adj))-1 3152353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj <= -2: 3153353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self < 0.1 3154353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str(-1-adj))-1 3155353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 3156353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 3157353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj == 0: 3158353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1 < self < 10 3159353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista num = str(c-10**-e) 3160353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista den = str(231*c) 3161353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(num) - len(den) - (num < den) + 2 3162353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adj == -1, 0.1 <= self < 1 3163353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista num = str(10**-e-c) 3164353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(num) + e - (num < "231") - 1 3165353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3166353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def log10(self, context=None): 3167353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the base 10 logarithm of self.""" 3168353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3169353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3170353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3171353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3172353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(NaN) = NaN 3173353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 3174353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3175353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3176353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3177353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(0.0) == -Infinity 3178353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 3179b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeInfinity 3180353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3181353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(Infinity) = Infinity 3182353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 3183b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Infinity 3184353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3185353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(negative or -Infinity) raises InvalidOperation 3186353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 1: 3187353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 3188353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'log10 of a negative value') 3189353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3190353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(10**n) = n 319172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if self._int[0] == '1' and self._int[1:] == '0'*(len(self._int) - 1): 3192353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # answer may need rounding 3193353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = Decimal(self._exp + len(self._int) - 1) 3194353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3195353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is irrational, so necessarily inexact 3196353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 3197353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 3198353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 3199353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3200353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # correctly rounded result: repeatedly increase precision 3201353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # until result is unambiguously roundable 3202353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places = p-self._log10_exp_bound()+2 3203353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 3204353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff = _dlog10(c, e, places) 3205353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # assert len(str(abs(coeff)))-p >= 1 3206353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(abs(coeff)))-p-1)): 3207353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 3208353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places += 3 320972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places) 3210353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3211353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 3212353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 3213353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 3214353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 3215353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3216353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3217353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logb(self, context=None): 3218353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ Returns the exponent of the magnitude of self's MSD. 3219353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3220353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the integer which is the exponent of the magnitude 3221353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of the most significant digit of self (as though it were truncated 3222353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to a single digit while maintaining the value of that digit and 3223353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista without limiting the resulting exponent). 3224353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3225353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # logb(NaN) = NaN 3226353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 3227353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3228353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3229353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3230353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3231353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3232353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3233353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # logb(+/-Inf) = +Inf 3234353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 3235b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Infinity 3236353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3237353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # logb(0) = -Inf, DivisionByZero 3238353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 3239cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionByZero, 'logb(0)', 1) 3240353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3241353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # otherwise, simply return the adjusted exponent of self, as a 3242353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Decimal. Note that no attempt is made to fit the result 3243353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # into the current context. 324415ae41c2db8b0779a7f584c238a8e9efe6e370b3Mark Dickinson ans = Decimal(self.adjusted()) 324515ae41c2db8b0779a7f584c238a8e9efe6e370b3Mark Dickinson return ans._fix(context) 3246353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3247353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _islogical(self): 3248353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return True if self is a logical operand. 3249353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3250c8acc882a92c1fe5df95ae73b7b8004737ed9222Andrew M. Kuchling For being logical, it must be a finite number with a sign of 0, 3251353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista an exponent of 0, and a coefficient whose digits must all be 3252353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista either 0 or 1. 3253353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3254353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign != 0 or self._exp != 0: 3255353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return False 3256353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for dig in self._int: 325772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if dig not in '01': 3258353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return False 3259353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return True 3260353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3261353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _fill_logical(self, context, opa, opb): 3262353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista dif = context.prec - len(opa) 3263353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if dif > 0: 326472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista opa = '0'*dif + opa 3265353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif dif < 0: 3266353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista opa = opa[-context.prec:] 3267353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista dif = context.prec - len(opb) 3268353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if dif > 0: 326972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista opb = '0'*dif + opb 3270353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif dif < 0: 3271353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista opb = opb[-context.prec:] 3272353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return opa, opb 3273353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3274353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_and(self, other, context=None): 3275353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies an 'and' operation between self and other's digits.""" 3276353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3277353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 32780c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 32790c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 32800c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 3281353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._islogical() or not other._islogical(): 3282353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3283353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3284353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fill to context.prec 3285353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (opa, opb) = self._fill_logical(context, self._int, other._int) 3286353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3287353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # make the operation, and clean starting zeroes 328872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista result = "".join([str(int(a)&int(b)) for a,b in zip(opa,opb)]) 328972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, result.lstrip('0') or '0', 0) 3290353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3291353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_invert(self, context=None): 3292353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Invert all its digits.""" 3293353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3294353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 329572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0), 329672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context) 3297353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3298353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_or(self, other, context=None): 3299353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies an 'or' operation between self and other's digits.""" 3300353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3301353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 33020c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 33030c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 33040c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 3305353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._islogical() or not other._islogical(): 3306353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3307353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3308353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fill to context.prec 3309353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (opa, opb) = self._fill_logical(context, self._int, other._int) 3310353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3311353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # make the operation, and clean starting zeroes 331265808ff0c08e60572cf81ebe5dc24794a706f390Mark Dickinson result = "".join([str(int(a)|int(b)) for a,b in zip(opa,opb)]) 331372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, result.lstrip('0') or '0', 0) 3314353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3315353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_xor(self, other, context=None): 3316353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies an 'xor' operation between self and other's digits.""" 3317353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3318353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 33190c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 33200c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 33210c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 3322353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._islogical() or not other._islogical(): 3323353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3324353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3325353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fill to context.prec 3326353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (opa, opb) = self._fill_logical(context, self._int, other._int) 3327353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3328353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # make the operation, and clean starting zeroes 332965808ff0c08e60572cf81ebe5dc24794a706f390Mark Dickinson result = "".join([str(int(a)^int(b)) for a,b in zip(opa,opb)]) 333072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, result.lstrip('0') or '0', 0) 3331353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3332353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def max_mag(self, other, context=None): 3333353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values numerically with their sign ignored.""" 3334353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 3335353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 33366c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 33376c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 33386c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 3339353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special or other._is_special: 3340353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # If one operand is a quiet NaN and the other is number, then the 3341353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # number is always returned 3342353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sn = self._isnan() 3343353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista on = other._isnan() 3344353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sn or on: 3345e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if on == 1 and sn == 0: 3346e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return self._fix(context) 3347e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if sn == 1 and on == 0: 3348e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return other._fix(context) 3349353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._check_nans(other, context) 3350353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 33512fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson c = self.copy_abs()._cmp(other.copy_abs()) 3352353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == 0: 3353353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 3354353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3355353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 3356353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = other 3357353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3358353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 3359353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3360e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 3361353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3362353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def min_mag(self, other, context=None): 3363353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values numerically with their sign ignored.""" 3364353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 3365353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 33666c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 33676c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 33686c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 3369353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special or other._is_special: 3370353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # If one operand is a quiet NaN and the other is number, then the 3371353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # number is always returned 3372353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sn = self._isnan() 3373353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista on = other._isnan() 3374353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sn or on: 3375e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if on == 1 and sn == 0: 3376e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return self._fix(context) 3377e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista if sn == 1 and on == 0: 3378e29d435e0cefb3e1772f6de0844e628aac3cf98eFacundo Batista return other._fix(context) 3379353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._check_nans(other, context) 3380353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 33812fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson c = self.copy_abs()._cmp(other.copy_abs()) 3382353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == 0: 3383353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 3384353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3385353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 3386353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 3387353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3388353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = other 3389353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3390e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 3391353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3392353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_minus(self, context=None): 3393353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the largest representable number smaller than itself.""" 3394353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3395353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3396353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3397353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 3398353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3399353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3400353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3401353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == -1: 3402b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _NegativeInfinity 3403353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 340472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, '9'*context.prec, context.Etop()) 3405353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3406353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context.copy() 3407353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._set_rounding(ROUND_FLOOR) 3408353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._ignore_all_flags() 3409353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista new_self = self._fix(context) 3410353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if new_self != self: 3411353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return new_self 341272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.__sub__(_dec_from_triple(0, '1', context.Etiny()-1), 341372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context) 3414353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3415353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_plus(self, context=None): 3416353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the smallest representable number larger than itself.""" 3417353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3418353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3419353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3420353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 3421353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3422353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3423353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3424353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 3425b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger return _Infinity 3426353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == -1: 342772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(1, '9'*context.prec, context.Etop()) 3428353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3429353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context.copy() 3430353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._set_rounding(ROUND_CEILING) 3431353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._ignore_all_flags() 3432353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista new_self = self._fix(context) 3433353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if new_self != self: 3434353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return new_self 343572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.__add__(_dec_from_triple(0, '1', context.Etiny()-1), 343672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context) 3437353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3438353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_toward(self, other, context=None): 3439353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the number closest to self, in the direction towards other. 3440353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3441353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the closest representable number to self 3442353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (excluding self) that is in the direction towards other, 3443353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista unless both have the same value. If the two operands are 3444353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista numerically equal, then the result is a copy of self with the 3445353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign set to be the same as the sign of other. 3446353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3447353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 3448353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3449353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3450353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3451353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3452353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3453353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3454353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3455353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 34562fc9263df56d28a6c1def6c8a0517bbddfa899afMark Dickinson comparison = self._cmp(other) 3457353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if comparison == 0: 345872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.copy_sign(other) 3459353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3460353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if comparison == -1: 3461353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self.next_plus(context) 3462353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: # comparison == 1 3463353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self.next_minus(context) 3464353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3465353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # decide which flags to raise using value of ans 3466353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans._isinfinity(): 3467353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Overflow, 3468353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'Infinite result from next_toward', 3469353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans._sign) 3470353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 34714f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Rounded) 3472353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif ans.adjusted() < context.Emin: 3473353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Underflow) 3474353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Subnormal) 3475353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 34764f96f5ffc6dd0b171bb14666d134af84ae307752Mark Dickinson context._raise_error(Rounded) 3477353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if precision == 1 then we don't raise Clamped for a 3478353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result 0E-Etiny. 3479353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not ans: 3480353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Clamped) 3481353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3482353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3483353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3484353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def number_class(self, context=None): 3485353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns an indication of the class of self. 3486353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3487353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The class is one of the following strings: 34880f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista sNaN 34890f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista NaN 3490353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Infinity 3491353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Normal 3492353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Subnormal 3493353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Zero 3494353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Zero 3495353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Subnormal 3496353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Normal 3497353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Infinity 3498353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3499353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_snan(): 3500353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "sNaN" 3501353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_qnan(): 3502353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "NaN" 3503353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista inf = self._isinfinity() 3504353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if inf == 1: 3505353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Infinity" 3506353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if inf == -1: 3507353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Infinity" 3508353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_zero(): 3509353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 3510353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Zero" 3511353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3512353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Zero" 3513353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3514353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3515353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_subnormal(context=context): 3516353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 3517353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Subnormal" 3518353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3519353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Subnormal" 3520353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # just a normal, regular, boring number, :) 3521353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 3522353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Normal" 3523353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3524353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Normal" 3525353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3526353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def radix(self): 3527353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Just returns 10, as this is Decimal, :)""" 3528353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(10) 3529353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3530353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def rotate(self, other, context=None): 3531353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a rotated copy of self, value-of-other times.""" 3532353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3533353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3534353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 35350c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 35360c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 3537353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3538353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3539353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3540353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3541353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._exp != 0: 3542353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3543353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (-context.prec <= int(other) <= context.prec): 3544353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3545353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3546353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 35476c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 3548353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3549353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # get values, pad if necessary 3550353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista torot = int(other) 3551353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotdig = self._int 3552353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista topad = context.prec - len(rotdig) 35536f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson if topad > 0: 355472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotdig = '0'*topad + rotdig 35556f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson elif topad < 0: 35566f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson rotdig = rotdig[-topad:] 3557353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3558353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # let's rotate! 3559353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotated = rotdig[torot:] + rotdig[:torot] 356072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, 356172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotated.lstrip('0') or '0', self._exp) 3562353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 35630c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson def scaleb(self, other, context=None): 3564353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns self operand after adding the second value to its exp.""" 3565353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3566353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3567353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 35680c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 35690c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 3570353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3571353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3572353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3573353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3574353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._exp != 0: 3575353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3576353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista liminf = -2 * (context.Emax + context.prec) 3577353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista limsup = 2 * (context.Emax + context.prec) 3578353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (liminf <= int(other) <= limsup): 3579353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3580353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3581353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 35826c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 3583353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 358472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista d = _dec_from_triple(self._sign, self._int, self._exp + int(other)) 3585353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista d = d._fix(context) 3586353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return d 3587353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3588353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def shift(self, other, context=None): 3589353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a shifted copy of self, value-of-other times.""" 3590353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3591353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3592353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 35930c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson other = _convert_other(other, raiseit=True) 35940c67312c5c0d5d44e065d2ecc5023d0e47f815eeMark Dickinson 3595353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3596353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3597353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3598353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3599353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._exp != 0: 3600353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3601353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (-context.prec <= int(other) <= context.prec): 3602353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3603353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3604353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 36056c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 3606353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3607353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # get values, pad if necessary 3608353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista torot = int(other) 3609353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotdig = self._int 3610353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista topad = context.prec - len(rotdig) 36116f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson if topad > 0: 361272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotdig = '0'*topad + rotdig 36136f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson elif topad < 0: 36146f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson rotdig = rotdig[-topad:] 3615353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3616353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # let's shift! 3617353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if torot < 0: 36186f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson shifted = rotdig[:torot] 3619353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 36206f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson shifted = rotdig + '0'*torot 36216f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson shifted = shifted[-context.prec:] 3622353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 362372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, 36246f3900163a50d04ecae173e2703f626f5daf8318Mark Dickinson shifted.lstrip('0') or '0', self._exp) 3625353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 362659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Support for pickling, copy, and deepcopy 36277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __reduce__(self): 36287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return (self.__class__, (str(self),)) 36297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 36307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __copy__(self): 363128e369a8f8a5fe416159a2a5604321574ea148b3Benjamin Peterson if type(self) is Decimal: 3632cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis return self # I'm immutable; therefore I am my own clone 36337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self.__class__(str(self)) 36347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 36357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __deepcopy__(self, memo): 363628e369a8f8a5fe416159a2a5604321574ea148b3Benjamin Peterson if type(self) is Decimal: 3637cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis return self # My components are also immutable 36387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self.__class__(str(self)) 36397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3640277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # PEP 3101 support. the _localeconv keyword argument should be 3641277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # considered private: it's provided for ease of testing only. 3642277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson def __format__(self, specifier, context=None, _localeconv=None): 3643f4da77765f8581e31620e9f49c68028e57b0ae85Mark Dickinson """Format a Decimal instance according to the given specifier. 36441ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 36451ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson The specifier should be a standard format specifier, with the 36461ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson form described in PEP 3101. Formatting types 'e', 'E', 'f', 3647277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 'F', 'g', 'G', 'n' and '%' are supported. If the formatting 3648277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson type is omitted it defaults to 'g' or 'G', depending on the 3649277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson value of context.capitals. 36501ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson """ 36511ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 36521ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # Note: PEP 3101 says that if the type is not present then 36531ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # there should be at least one digit after the decimal point. 36541ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # We take the liberty of ignoring this requirement for 36551ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # Decimal---it's presumably there to make sure that 36561ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # format(float, '') behaves similarly to str(float). 36571ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if context is None: 36581ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson context = getcontext() 36591ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 3660277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson spec = _parse_format_specifier(specifier, _localeconv=_localeconv) 36611ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 3662277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # special values don't care about the type or precision 36631ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if self._is_special: 3664277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson sign = _format_sign(self._sign, spec) 3665277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson body = str(self.copy_abs()) 3666ce2ec49d92b4c31b6ad358402d8a124e7b03e239Stefan Krah if spec['type'] == '%': 3667ce2ec49d92b4c31b6ad358402d8a124e7b03e239Stefan Krah body += '%' 3668277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return _format_align(sign, body, spec) 36691ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 36701ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # a type of None defaults to 'g' or 'G', depending on context 36711ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if spec['type'] is None: 36721ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson spec['type'] = ['g', 'G'][context.capitals] 3673277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 3674277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # if type is '%', adjust exponent of self accordingly 3675277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if spec['type'] == '%': 36761ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson self = _dec_from_triple(self._sign, self._int, self._exp+2) 36771ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 36781ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # round if necessary, taking rounding mode from the context 36791ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson rounding = context.rounding 36801ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson precision = spec['precision'] 36811ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if precision is not None: 36821ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if spec['type'] in 'eE': 36831ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson self = self._round(precision+1, rounding) 36841ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson elif spec['type'] in 'fF%': 36851ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson self = self._rescale(-precision, rounding) 3686277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson elif spec['type'] in 'gG' and len(self._int) > precision: 3687277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson self = self._round(precision, rounding) 36881ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # special case: zeros with a positive exponent can't be 36891ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # represented in fixed point; rescale them to 0e0. 3690277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if not self and self._exp > 0 and spec['type'] in 'fF%': 36911ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson self = self._rescale(0, rounding) 36921ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 36931ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # figure out placement of the decimal point 36941ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson leftdigits = self._exp + len(self._int) 3695277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if spec['type'] in 'eE': 36961ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if not self and precision is not None: 36971ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson dotplace = 1 - precision 36981ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson else: 36991ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson dotplace = 1 3700277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson elif spec['type'] in 'fF%': 3701277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson dotplace = leftdigits 37021ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson elif spec['type'] in 'gG': 37031ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if self._exp <= 0 and leftdigits > -6: 37041ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson dotplace = leftdigits 37051ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson else: 37061ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson dotplace = 1 37071ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 3708277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # find digits before and after decimal point, and get exponent 3709277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if dotplace < 0: 3710277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson intpart = '0' 3711277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fracpart = '0'*(-dotplace) + self._int 3712277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson elif dotplace > len(self._int): 3713277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson intpart = self._int + '0'*(dotplace-len(self._int)) 3714277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fracpart = '' 37151ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson else: 3716277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson intpart = self._int[:dotplace] or '0' 3717277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fracpart = self._int[dotplace:] 3718277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson exp = leftdigits-dotplace 37191ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 3720277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # done with the decimal-specific stuff; hand over the rest 3721277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # of the formatting to the _format_number function 3722277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return _format_number(self._sign, intpart, fracpart, exp, spec) 37231ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 372472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batistadef _dec_from_triple(sign, coefficient, exponent, special=False): 372572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista """Create a decimal instance directly, without any validation, 372672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista normalization (e.g. removal of leading zeros) or argument 372772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista conversion. 372872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 372972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista This function is for *internal use only*. 373072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista """ 373172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 373272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self = object.__new__(Decimal) 373372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._sign = sign 373472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = coefficient 373572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._exp = exponent 373672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._is_special = special 373772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 373872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self 373972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 37402c8585b0afb6a39c215a71963e2fadea96f2c6aeRaymond Hettinger# Register Decimal as a kind of Number (an abstract base class). 37412c8585b0afb6a39c215a71963e2fadea96f2c6aeRaymond Hettinger# However, do not register it as Real (because Decimals are not 37422c8585b0afb6a39c215a71963e2fadea96f2c6aeRaymond Hettinger# interoperable with floats). 37432c8585b0afb6a39c215a71963e2fadea96f2c6aeRaymond Hettinger_numbers.Number.register(Decimal) 37442c8585b0afb6a39c215a71963e2fadea96f2c6aeRaymond Hettinger 37452c8585b0afb6a39c215a71963e2fadea96f2c6aeRaymond Hettinger 374659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Context class ####################################################### 3747cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 3748ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlanclass _ContextManager(object): 37498b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """Context manager class to support localcontext(). 37501a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum 3751ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan Sets a copy of the supplied context in __enter__() and restores 37528b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan the previous decimal context in __exit__() 37538b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """ 37541a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum def __init__(self, new_context): 3755ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan self.new_context = new_context.copy() 37561a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum def __enter__(self): 37571a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum self.saved_context = getcontext() 37581a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum setcontext(self.new_context) 37591a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum return self.new_context 37601a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum def __exit__(self, t, v, tb): 37611a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum setcontext(self.saved_context) 37621a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum 37637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Context(object): 37647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Contains the context for a Decimal instance. 37657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 37667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Contains: 37677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger prec - precision (for use in rounding, division, square roots..) 376859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista rounding - rounding type (how you round) 3769bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps - If traps[exception] = 1, then the exception is 37707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger raised when it is caused. Otherwise, a value is 37717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger substituted in. 37721840c1abcade9215faf91b701ab4525ba948d9dcMark Dickinson flags - When an exception is caused, flags[exception] is set. 37737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (Whether or not the trap_enabler is set) 37747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Should be reset by user of Decimal instance. 37750ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettinger Emin - Minimum exponent 37760ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettinger Emax - Maximum exponent 37777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger capitals - If 1, 1*10^1 is printed as 1E+1. 37787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If 0, printed as 1e1 3779e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger _clamp - If 1, change exponents if too high (Default 0) 37807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 37819ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger 37827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __init__(self, prec=None, rounding=None, 3783abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger traps=None, flags=None, 37840ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettinger Emin=None, Emax=None, 3785e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger capitals=None, _clamp=0, 3786abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger _ignored_flags=None): 37879b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson # Set defaults; for everything except flags and _ignored_flags, 37889b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson # inherit from DefaultContext. 37899b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson try: 37909b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson dc = DefaultContext 37919b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson except NameError: 37929b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson pass 37939b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson 37949b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.prec = prec if prec is not None else dc.prec 37959b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.rounding = rounding if rounding is not None else dc.rounding 37969b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.Emin = Emin if Emin is not None else dc.Emin 37979b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.Emax = Emax if Emax is not None else dc.Emax 37989b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.capitals = capitals if capitals is not None else dc.capitals 37999b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self._clamp = _clamp if _clamp is not None else dc._clamp 38009b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson 3801abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger if _ignored_flags is None: 38029b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self._ignored_flags = [] 38039b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson else: 38049b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self._ignored_flags = _ignored_flags 38059b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson 38069b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson if traps is None: 38079b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.traps = dc.traps.copy() 38089b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson elif not isinstance(traps, dict): 38099b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.traps = dict((s, int(s in traps)) for s in _signals) 38109b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson else: 38119b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.traps = traps 38129b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson 38139b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson if flags is None: 38149b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.flags = dict.fromkeys(_signals, 0) 38159b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson elif not isinstance(flags, dict): 38169b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.flags = dict((s, int(s in flags)) for s in _signals) 38179b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson else: 38189b9e12530d976d2698c583eb3784e11b10270eb8Mark Dickinson self.flags = flags 38197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3820b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger def __repr__(self): 3821bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger """Show the current context.""" 3822b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger s = [] 382359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista s.append('Context(prec=%(prec)d, rounding=%(rounding)s, ' 382459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d' 382559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista % vars(self)) 382659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista names = [f.__name__ for f, v in self.flags.items() if v] 382759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista s.append('flags=[' + ', '.join(names) + ']') 382859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista names = [t.__name__ for t, v in self.traps.items() if v] 382959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista s.append('traps=[' + ', '.join(names) + ']') 3830b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger return ', '.join(s) + ')' 3831b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger 3832d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger def clear_flags(self): 3833d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger """Reset all flags to zero""" 3834d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger for flag in self.flags: 3835b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger self.flags[flag] = 0 3836d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger 38379fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger def _shallow_copy(self): 38389fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger """Returns a shallow copy from self.""" 3839e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista nc = Context(self.prec, self.rounding, self.traps, 3840e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.flags, self.Emin, self.Emax, 3841e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.capitals, self._clamp, self._ignored_flags) 38427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return nc 38439fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger 38449fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger def copy(self): 38459fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger """Returns a deep copy from self.""" 384659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista nc = Context(self.prec, self.rounding, self.traps.copy(), 3847e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.flags.copy(), self.Emin, self.Emax, 3848e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.capitals, self._clamp, self._ignored_flags) 38499fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger return nc 38509ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger __copy__ = copy 38517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38525aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger def _raise_error(self, condition, explanation = None, *args): 38537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Handles an error 38547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the flag is in _ignored_flags, returns the default response. 38561840c1abcade9215faf91b701ab4525ba948d9dcMark Dickinson Otherwise, it sets the flag, then, if the corresponding 38578a6f3fe3b5588da1e2803a308628ed909ea8bcfbStefan Krah trap_enabler is set, it reraises the exception. Otherwise, it returns 38581840c1abcade9215faf91b701ab4525ba948d9dcMark Dickinson the default value after setting the flag. 38597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 38605aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger error = _condition_map.get(condition, condition) 38617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if error in self._ignored_flags: 386259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Don't touch the flag 38637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return error().handle(self, *args) 38647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38651840c1abcade9215faf91b701ab4525ba948d9dcMark Dickinson self.flags[error] = 1 3866bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger if not self.traps[error]: 386759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # The errors define how to handle themselves. 38685aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger return condition().handle(self, *args) 38697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Errors should only be risked on copies of the context 387159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # self._ignored_flags = [] 38728aca9d032e7813da9a23fd0771c008aff5a5e62fMark Dickinson raise error(explanation) 38737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _ignore_all_flags(self): 38757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Ignore all flags, if they are raised""" 3876fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger return self._ignore_flags(*_signals) 38777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _ignore_flags(self, *flags): 38797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Ignore the flags, if they are raised""" 38807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Do not mutate-- This way, copies of a context leave the original 38817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # alone. 38827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self._ignored_flags = (self._ignored_flags + list(flags)) 38837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return list(flags) 38847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _regard_flags(self, *flags): 38867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Stop ignoring the flags, if they are raised""" 38877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if flags and isinstance(flags[0], (tuple,list)): 38887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger flags = flags[0] 38897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger for flag in flags: 38907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self._ignored_flags.remove(flag) 38917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 389253663a695ef2bb96ac0252cd4cc4aa40d4f953beNick Coghlan # We inherit object.__hash__, so we must deny this explicitly 389353663a695ef2bb96ac0252cd4cc4aa40d4f953beNick Coghlan __hash__ = None 38945aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger 38957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def Etiny(self): 38967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns Etiny (= Emin - prec + 1)""" 38977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return int(self.Emin - self.prec + 1) 38987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 38997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def Etop(self): 3900e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger """Returns maximum exponent (= Emax - prec + 1)""" 39017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return int(self.Emax - self.prec + 1) 39027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _set_rounding(self, type): 39047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Sets the rounding type. 39057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Sets the rounding type, and returns the current (previous) 39077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounding type. Often used like: 39087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = context.copy() 39107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # so you don't change the calling context 39117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # if an error occurs in the middle. 39127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounding = context._set_rounding(ROUND_UP) 39137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger val = self.__sub__(other, context=context) 39147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context._set_rounding(rounding) 39157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This will make it round up for that operation. 39177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 39187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounding = self.rounding 39197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.rounding= type 39207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return rounding 39217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3922fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger def create_decimal(self, num='0'): 392359bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson """Creates a new Decimal instance but using self as context. 392459bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson 392559bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson This method implements the to-number operation of the 392659bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson IBM Decimal specification.""" 392759bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson 392859bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson if isinstance(num, basestring) and num != num.strip(): 392959bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson return self._raise_error(ConversionSyntax, 393059bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson "no trailing or leading whitespace is " 393159bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson "permitted.") 393259bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson 39337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger d = Decimal(num, context=self) 3934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if d._isnan() and len(d._int) > self.prec - self._clamp: 3935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._raise_error(ConversionSyntax, 3936353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista "diagnostic info too long in NaN") 3937dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger return d._fix(self) 39387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3939f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger def create_decimal_from_float(self, f): 3940f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger """Creates a new Decimal instance from a float but rounding using self 3941f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger as the context. 3942f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 3943f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> context = Context(prec=5, rounding=ROUND_DOWN) 3944f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> context.create_decimal_from_float(3.1415926535897932) 3945f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Decimal('3.1415') 3946f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> context = Context(prec=5, traps=[Inexact]) 3947f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger >>> context.create_decimal_from_float(3.1415926535897932) 3948f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Traceback (most recent call last): 3949f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger ... 3950f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger Inexact: None 3951f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 3952f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger """ 3953f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger d = Decimal.from_float(f) # An exact conversion 3954f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger return d._fix(self) # Apply the context rounding 3955f4d8597a59990c41bc9cc8d499d8bc919d7ebf2dRaymond Hettinger 395659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Methods 39577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def abs(self, a): 39587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the absolute value of the operand. 39597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the operand is negative, the result is the same as using the minus 396159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation on the operand. Otherwise, the result is the same as using 39627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the plus operation on the operand. 39637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39649ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('2.1')) 3965abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.1') 39669ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('-100')) 3967abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('100') 39689ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('101.5')) 3969abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('101.5') 39709ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('-101.5')) 3971abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('101.5') 39726d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.abs(-1) 39736d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 39747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 39756d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 39767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__abs__(context=self) 39777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def add(self, a, b): 39797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return the sum of the two operands. 39807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39819ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.add(Decimal('12'), Decimal('7.00')) 3982abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('19.00') 39839ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4')) 3984abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.02E+4') 39856d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.add(1, Decimal(2)) 39866d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 39876d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.add(Decimal(8), 5) 39886d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('13') 39896d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.add(5, 5) 39906d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('10') 39917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 39926d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 39936d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__add__(b, context=self) 39946d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 39956d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 39966d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 39976d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 39987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 39997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _apply(self, a): 4000dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger return str(a._fix(self)) 40017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def canonical(self, a): 4003353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the same Decimal object. 4004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista As we do not have different encodings for the same number, the 4006353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista received object already is in its canonical form. 4007353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4008353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.canonical(Decimal('2.50')) 4009abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.50') 4010353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4011353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.canonical(context=self) 4012353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 40137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def compare(self, a, b): 40147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Compares values numerically. 40157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 40167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the signs of the operands differ, a value representing each operand 40177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ('-1' if the operand is less than zero, '0' if the operand is zero or 40187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger negative zero, or '1' if the operand is greater than zero) is used in 40197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger place of that operand for the comparison instead of the actual 40207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operand. 40217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 40227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The comparison is then effected by subtracting the second operand from 40237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the first and then returning a value according to the result of the 40247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger subtraction: '-1' if the result is less than zero, '0' if the result is 40257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger zero or negative zero, or '1' if the result is greater than zero. 40267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 40279ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3')) 4028abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 40299ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1')) 4030abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 40319ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10')) 4032abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 40339ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1')) 4034abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 40359ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3')) 4036abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 40379ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1')) 4038abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 40396d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.compare(1, 2) 40406d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 40416d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.compare(Decimal(1), 2) 40426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 40436d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.compare(1, Decimal(2)) 40446d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 40457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 40466d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 40477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.compare(b, context=self) 40487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4049353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_signal(self, a, b): 4050353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values of the two operands numerically. 4051353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4052353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista It's pretty much like compare(), but all NaNs signal, with signaling 4053353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista NaNs taking precedence over quiet NaNs. 4054353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4055353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext 4056353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('2.1'), Decimal('3')) 4057abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 4058353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('2.1'), Decimal('2.1')) 4059abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4060353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.flags[InvalidOperation] = 0 4061353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 4062353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 0 4063353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('NaN'), Decimal('2.1')) 4064abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('NaN') 4065353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 4066353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1 4067353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.flags[InvalidOperation] = 0 4068353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 4069353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 0 4070353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1')) 4071abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('NaN') 4072353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 4073353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1 40746d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.compare_signal(-1, 2) 40756d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 40766d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.compare_signal(Decimal(-1), 2) 40776d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 40786d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.compare_signal(-1, Decimal(2)) 40796d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 4080353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 40816d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4082353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.compare_signal(b, context=self) 4083353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4084353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total(self, a, b): 4085353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares two operands using their abstract representation. 4086353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4087353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista This is not like the standard compare, which use their numerical 4088353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value. Note that a total ordering is defined for all possible abstract 4089353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista representations. 4090353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4091353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9')) 4092abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 4093353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12')) 4094abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 4095353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3')) 4096abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 4097353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30')) 4098abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4099353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300')) 4100abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4101353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN')) 4102abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 41036d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.compare_total(1, 2) 41046d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 41056d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.compare_total(Decimal(1), 2) 41066d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 41076d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.compare_total(1, Decimal(2)) 41086d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 4109353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 41106d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4111353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.compare_total(b) 4112353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4113353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total_mag(self, a, b): 4114353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares two operands using their abstract representation ignoring sign. 4115353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4116353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Like compare_total, but with operand's sign ignored and assumed to be 0. 4117353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 41186d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4119353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.compare_total_mag(b) 4120353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4121353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_abs(self, a): 4122353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy of the operand with the sign set to 0. 4123353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4124353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_abs(Decimal('2.1')) 4125abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.1') 4126353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_abs(Decimal('-100')) 4127abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('100') 41286d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.copy_abs(-1) 41296d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 4130353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 41316d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4132353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.copy_abs() 4133353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4134353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_decimal(self, a): 41356d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson """Returns a copy of the decimal object. 4136353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4137353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_decimal(Decimal('2.1')) 4138abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.1') 4139353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_decimal(Decimal('-1.00')) 4140abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.00') 41416d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.copy_decimal(1) 41426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 4143353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 41446d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 41456c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(a) 4146353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4147353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_negate(self, a): 4148353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy of the operand with the sign inverted. 4149353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4150353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_negate(Decimal('101.5')) 4151abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-101.5') 4152353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_negate(Decimal('-101.5')) 4153abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('101.5') 41546d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.copy_negate(1) 41556d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 4156353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 41576d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4158353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.copy_negate() 4159353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4160353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_sign(self, a, b): 4161353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Copies the second operand's sign to the first one. 4162353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4163353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In detail, it returns a copy of the first operand with the sign 4164353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista equal to the sign of the second operand. 4165353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4166353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33')) 4167abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.50') 4168353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33')) 4169abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.50') 4170353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33')) 4171abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.50') 4172353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33')) 4173abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.50') 41746d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.copy_sign(1, -2) 41756d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 41766d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.copy_sign(Decimal(1), -2) 41776d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 41786d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.copy_sign(1, Decimal(-2)) 41796d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 4180353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 41816d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4182353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.copy_sign(b) 4183353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 41847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def divide(self, a, b): 41857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Decimal division in a specified context. 41867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 41879ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1'), Decimal('3')) 4188abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.333333333') 41899ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('2'), Decimal('3')) 4190abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.666666667') 41919ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('5'), Decimal('2')) 4192abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.5') 41939ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1'), Decimal('10')) 4194abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.1') 41959ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('12'), Decimal('12')) 4196abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 41979ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2')) 4198abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('4.00') 41999ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0')) 4200abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.20') 42019ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1000'), Decimal('100')) 4202abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('10') 42039ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1000'), Decimal('1')) 4204abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1000') 42059ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2')) 4206abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.20E+6') 42076d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divide(5, 5) 42086d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 42096d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divide(Decimal(5), 5) 42106d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 42116d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divide(5, Decimal(5)) 42126d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 42137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 42146d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 42156d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__div__(b, context=self) 42166d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 42176d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 42186d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 42196d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 42207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def divide_int(self, a, b): 42227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Divides two numbers and returns the integer part of the result. 42237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42249ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3')) 4225abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 42269ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3')) 4227abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3') 42289ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3')) 4229abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3') 42306d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divide_int(10, 3) 42316d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 42326d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divide_int(Decimal(10), 3) 42336d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 42346d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divide_int(10, Decimal(3)) 42356d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 42367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 42376d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 42386d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__floordiv__(b, context=self) 42396d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 42406d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 42416d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 42426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 42437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def divmod(self, a, b): 42456d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson """Return (a // b, a % b). 4246202eb9094cc9c504adb1ec122a233a3a19b35b7cMark Dickinson 4247202eb9094cc9c504adb1ec122a233a3a19b35b7cMark Dickinson >>> ExtendedContext.divmod(Decimal(8), Decimal(3)) 4248202eb9094cc9c504adb1ec122a233a3a19b35b7cMark Dickinson (Decimal('2'), Decimal('2')) 4249202eb9094cc9c504adb1ec122a233a3a19b35b7cMark Dickinson >>> ExtendedContext.divmod(Decimal(8), Decimal(4)) 4250202eb9094cc9c504adb1ec122a233a3a19b35b7cMark Dickinson (Decimal('2'), Decimal('0')) 42516d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divmod(8, 4) 42526d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson (Decimal('2'), Decimal('0')) 42536d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divmod(Decimal(8), 4) 42546d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson (Decimal('2'), Decimal('0')) 42556d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.divmod(8, Decimal(4)) 42566d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson (Decimal('2'), Decimal('0')) 4257202eb9094cc9c504adb1ec122a233a3a19b35b7cMark Dickinson """ 42586d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 42596d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__divmod__(b, context=self) 42606d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 42616d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 42626d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 42636d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 42647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4265353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def exp(self, a): 4266353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns e ** a. 4267353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4268353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4269353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4270353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4271353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('-Infinity')) 4272abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4273353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('-1')) 4274abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.367879441') 4275353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('0')) 4276abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4277353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('1')) 4278abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.71828183') 4279353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('0.693147181')) 4280abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.00000000') 4281353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('+Infinity')) 4282abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('Infinity') 42836d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.exp(10) 42846d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('22026.4658') 4285353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 42866d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a =_convert_other(a, raiseit=True) 4287353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.exp(context=self) 4288353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4289353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def fma(self, a, b, c): 4290353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a multiplied by b, plus c. 4291353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4292353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The first two operands are multiplied together, using multiply, 4293353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the third operand is then added to the result of that 4294353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplication, using add, all with only one final rounding. 4295353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4296353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7')) 4297abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('22') 4298353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7')) 4299abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-8') 4300353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578')) 4301abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.38435736E+12') 43026d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.fma(1, 3, 4) 43036d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('7') 43046d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.fma(1, Decimal(3), 4) 43056d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('7') 43066d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.fma(1, 3, Decimal(4)) 43076d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('7') 4308353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 43096d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4310353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.fma(b, c, context=self) 4311353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4312353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_canonical(self, a): 43131a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is canonical; otherwise return False. 43141a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista 43151a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista Currently, the encoding of a Decimal instance is always 43161a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista canonical, so this method returns True for any Decimal. 4317353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4318353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_canonical(Decimal('2.50')) 43191a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4320353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 43211a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return a.is_canonical() 4322353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4323353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_finite(self, a): 43241a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is finite; otherwise return False. 4325353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 43261a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista A Decimal instance is considered finite if it is neither 43271a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista infinite nor a NaN. 4328353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4329353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('2.50')) 43301a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4331353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('-0.3')) 43321a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4333353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('0')) 43341a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4335353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('Inf')) 43361a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4337353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('NaN')) 43381a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 43396d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_finite(1) 43406d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson True 4341353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 43426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4343353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_finite() 4344353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4345353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_infinite(self, a): 43461a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is infinite; otherwise return False. 4347353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4348353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_infinite(Decimal('2.50')) 43491a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4350353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_infinite(Decimal('-Inf')) 43511a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4352353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_infinite(Decimal('NaN')) 43531a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 43546d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_infinite(1) 43556d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson False 4356353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 43576d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4358353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_infinite() 4359353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4360353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_nan(self, a): 43611a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a qNaN or sNaN; 43621a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista otherwise return False. 4363353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4364353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_nan(Decimal('2.50')) 43651a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4366353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_nan(Decimal('NaN')) 43671a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4368353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_nan(Decimal('-sNaN')) 43691a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 43706d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_nan(1) 43716d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson False 4372353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 43736d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4374353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_nan() 4375353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4376353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_normal(self, a): 43771a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a normal number; 43781a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista otherwise return False. 4379353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4380353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4381353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4382353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4383353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('2.50')) 43841a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4385353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('0.1E-999')) 43861a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4387353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('0.00')) 43881a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4389353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('-Inf')) 43901a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4391353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('NaN')) 43921a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 43936d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.is_normal(1) 43946d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson True 4395353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 43966d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4397353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_normal(context=self) 4398353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4399353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_qnan(self, a): 44001a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a quiet NaN; otherwise return False. 4401353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4402353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_qnan(Decimal('2.50')) 44031a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4404353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_qnan(Decimal('NaN')) 44051a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4406353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_qnan(Decimal('sNaN')) 44071a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 44086d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_qnan(1) 44096d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson False 4410353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 44116d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4412353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_qnan() 4413353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4414353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_signed(self, a): 44151a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is negative; otherwise return False. 4416353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4417353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_signed(Decimal('2.50')) 44181a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4419353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_signed(Decimal('-12')) 44201a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4421353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_signed(Decimal('-0')) 44221a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 44236d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_signed(8) 44246d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson False 44256d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_signed(-8) 44266d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson True 4427353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 44286d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4429353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_signed() 4430353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4431353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_snan(self, a): 44321a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a signaling NaN; 44331a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista otherwise return False. 4434353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4435353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_snan(Decimal('2.50')) 44361a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4437353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_snan(Decimal('NaN')) 44381a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4439353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_snan(Decimal('sNaN')) 44401a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 44416d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_snan(1) 44426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson False 4443353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 44446d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4445353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_snan() 4446353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4447353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_subnormal(self, a): 44481a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is subnormal; otherwise return False. 4449353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4450353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4451353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4452353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4453353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('2.50')) 44541a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4455353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('0.1E-999')) 44561a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4457353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('0.00')) 44581a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4459353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('-Inf')) 44601a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4461353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('NaN')) 44621a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 44636d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.is_subnormal(1) 44646d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson False 4465353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 44666d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4467353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_subnormal(context=self) 4468353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4469353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_zero(self, a): 44701a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a zero; otherwise return False. 4471353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4472353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_zero(Decimal('0')) 44731a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 4474353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_zero(Decimal('2.50')) 44751a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 4476353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_zero(Decimal('-0E+2')) 44771a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 44786d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_zero(1) 44796d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson False 44806d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.is_zero(0) 44816d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson True 4482353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 44836d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4484353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_zero() 4485353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4486353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def ln(self, a): 4487353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the natural (base e) logarithm of the operand. 4488353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4489353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4490353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4491353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4492353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('0')) 4493abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-Infinity') 4494353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('1.000')) 4495abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4496353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('2.71828183')) 4497abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.00000000') 4498353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('10')) 4499abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.30258509') 4500353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('+Infinity')) 4501abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('Infinity') 45026d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.ln(1) 45036d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('0') 4504353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 45056d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4506353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.ln(context=self) 4507353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4508353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def log10(self, a): 4509353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the base 10 logarithm of the operand. 4510353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4511353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4512353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4513353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4514353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('0')) 4515abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-Infinity') 4516353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('0.001')) 4517abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-3') 4518353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('1.000')) 4519abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4520353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('2')) 4521abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.301029996') 4522353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('10')) 4523abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4524353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('70')) 4525abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.84509804') 4526353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('+Infinity')) 4527abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('Infinity') 45286d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.log10(0) 45296d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-Infinity') 45306d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.log10(1) 45316d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('0') 4532353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 45336d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4534353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.log10(context=self) 4535353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4536353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logb(self, a): 4537353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ Returns the exponent of the magnitude of the operand's MSD. 4538353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4539353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the integer which is the exponent of the magnitude 4540353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of the most significant digit of the operand (as though the 4541353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista operand were truncated to a single digit while maintaining the 4542353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value of that digit and without limiting the resulting exponent). 4543353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4544353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('250')) 4545abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2') 4546353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('2.50')) 4547abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4548353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('0.03')) 4549abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-2') 4550353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('0')) 4551abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-Infinity') 45526d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.logb(1) 45536d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('0') 45546d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.logb(10) 45556d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 45566d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.logb(100) 45576d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('2') 4558353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 45596d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4560353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logb(context=self) 4561353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4562353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_and(self, a, b): 4563353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies the logical operation 'and' between each operand's digits. 4564353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4565353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operands must be both logical numbers. 4566353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4567353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0')) 4568abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4569353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1')) 4570abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4571353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0')) 4572abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4573353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1')) 4574abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4575353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010')) 4576abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1000') 4577353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10')) 4578abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('10') 4579456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_and(110, 1101) 4580456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('100') 4581456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_and(Decimal(110), 1101) 4582456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('100') 4583456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_and(110, Decimal(1101)) 4584456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('100') 4585353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 45866d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4587353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_and(b, context=self) 4588353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4589353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_invert(self, a): 4590353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Invert all the digits in the operand. 4591353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4592353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operand must be a logical number. 4593353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4594353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('0')) 4595abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('111111111') 4596353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('1')) 4597abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('111111110') 4598353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('111111111')) 4599abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4600353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('101010101')) 4601abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('10101010') 4602456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_invert(1101) 4603456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('111110010') 4604353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 46056d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4606353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_invert(context=self) 4607353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4608353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_or(self, a, b): 4609353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies the logical operation 'or' between each operand's digits. 4610353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4611353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operands must be both logical numbers. 4612353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4613353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0')) 4614abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4615353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1')) 4616abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4617353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0')) 4618abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4619353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1')) 4620abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4621353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010')) 4622abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1110') 4623353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10')) 4624abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1110') 4625456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_or(110, 1101) 4626456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('1111') 4627456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_or(Decimal(110), 1101) 4628456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('1111') 4629456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_or(110, Decimal(1101)) 4630456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('1111') 4631353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 46326d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4633353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_or(b, context=self) 4634353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4635353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_xor(self, a, b): 4636353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies the logical operation 'xor' between each operand's digits. 4637353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4638353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operands must be both logical numbers. 4639353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4640353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0')) 4641abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4642353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1')) 4643abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4644353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0')) 4645abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4646353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1')) 4647abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 4648353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010')) 4649abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('110') 4650353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10')) 4651abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1101') 4652456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_xor(110, 1101) 4653456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('1011') 4654456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_xor(Decimal(110), 1101) 4655456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('1011') 4656456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson >>> ExtendedContext.logical_xor(110, Decimal(1101)) 4657456e1652cf63dfbd3b1d0af81af9582d204ddd02Mark Dickinson Decimal('1011') 4658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 46596d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4660353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_xor(b, context=self) 4661353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 46626d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson def max(self, a, b): 46637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """max compares two values numerically and returns the maximum. 46647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If either operand is a NaN then the general rules apply. 4666c8acc882a92c1fe5df95ae73b7b8004737ed9222Andrew M. Kuchling Otherwise, the operands are compared as though by the compare 466759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation. If they are numerically equal then the left-hand operand 466859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista is chosen as the result. Otherwise the maximum (closer to positive 46697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger infinity) of the two operands is chosen as the result. 46707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46719ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.max(Decimal('3'), Decimal('2')) 4672abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3') 46739ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.max(Decimal('-10'), Decimal('3')) 4674abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3') 46759ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.max(Decimal('1.0'), Decimal('1')) 4676abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 4677d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger >>> ExtendedContext.max(Decimal('7'), Decimal('NaN')) 4678abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('7') 46796d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max(1, 2) 46806d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('2') 46816d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max(Decimal(1), 2) 46826d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('2') 46836d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max(1, Decimal(2)) 46846d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('2') 46857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 46866d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 46877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.max(b, context=self) 46887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4689353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def max_mag(self, a, b): 46906d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson """Compares the values numerically with their sign ignored. 46916d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson 46926d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN')) 46936d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('7') 46946d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10')) 46956d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-10') 46966d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max_mag(1, -2) 46976d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-2') 46986d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max_mag(Decimal(1), -2) 46996d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-2') 47006d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.max_mag(1, Decimal(-2)) 47016d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-2') 47026d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson """ 47036d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4704353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.max_mag(b, context=self) 4705353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 47066d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson def min(self, a, b): 47077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """min compares two values numerically and returns the minimum. 47087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 47097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If either operand is a NaN then the general rules apply. 4710c8acc882a92c1fe5df95ae73b7b8004737ed9222Andrew M. Kuchling Otherwise, the operands are compared as though by the compare 471159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation. If they are numerically equal then the left-hand operand 471259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista is chosen as the result. Otherwise the minimum (closer to negative 47137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger infinity) of the two operands is chosen as the result. 47147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 47159ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.min(Decimal('3'), Decimal('2')) 4716abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2') 47179ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.min(Decimal('-10'), Decimal('3')) 4718abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-10') 47199ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.min(Decimal('1.0'), Decimal('1')) 4720abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.0') 4721d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger >>> ExtendedContext.min(Decimal('7'), Decimal('NaN')) 4722abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('7') 47236d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min(1, 2) 47246d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 47256d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min(Decimal(1), 2) 47266d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 47276d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min(1, Decimal(29)) 47286d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 47297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 47306d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 47317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.min(b, context=self) 47327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4733353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def min_mag(self, a, b): 47346d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson """Compares the values numerically with their sign ignored. 47356d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson 47366d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2')) 47376d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-2') 47386d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN')) 47396d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-3') 47406d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min_mag(1, -2) 47416d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 47426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min_mag(Decimal(1), -2) 47436d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 47446d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.min_mag(1, Decimal(-2)) 47456d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 47466d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson """ 47476d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4748353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.min_mag(b, context=self) 4749353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 47507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def minus(self, a): 47517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Minus corresponds to unary prefix minus in Python. 47527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 47537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is evaluated using the same rules as subtract; the 47547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation minus(a) is calculated as subtract('0', a) where the '0' 47557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger has the same exponent as the operand. 47567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 47579ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.minus(Decimal('1.3')) 4758abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.3') 47599ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.minus(Decimal('-1.3')) 4760abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.3') 47616d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.minus(1) 47626d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 47637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 47646d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 47657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__neg__(context=self) 47667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 47677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def multiply(self, a, b): 47687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """multiply multiplies two operands. 47697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4770cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis If either operand is a special value then the general rules apply. 47716d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Otherwise, the operands are multiplied together 47726d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson ('long multiplication'), resulting in a number which may be as long as 47736d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson the sum of the lengths of the two operands. 47747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 47759ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3')) 4776abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3.60') 47779ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('7'), Decimal('3')) 4778abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('21') 47799ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8')) 4780abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.72') 47819ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0')) 4782abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0.0') 47839ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321')) 4784abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('4.28135971E+11') 47856d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.multiply(7, 7) 47866d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('49') 47876d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.multiply(Decimal(7), 7) 47886d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('49') 47896d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.multiply(7, Decimal(7)) 47906d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('49') 47917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 47926d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 47936d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__mul__(b, context=self) 47946d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 47956d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 47966d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 47976d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 47987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4799353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_minus(self, a): 4800353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the largest representable number smaller than a. 4801353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4802353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4803353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4804353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4805353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_minus(Decimal('1')) 4806abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.999999999') 4807353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_minus(Decimal('1E-1007')) 4808abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0E-1007') 4809353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_minus(Decimal('-1.00000003')) 4810abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.00000004') 4811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_minus(Decimal('Infinity')) 4812abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('9.99999999E+999') 48136d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.next_minus(1) 48146d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('0.999999999') 4815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 48166d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.next_minus(context=self) 4818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4819353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_plus(self, a): 4820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the smallest representable number larger than a. 4821353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4823353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4824353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4825353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_plus(Decimal('1')) 4826abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.00000001') 4827353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_plus(Decimal('-1E-1007')) 4828abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0E-1007') 4829353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_plus(Decimal('-1.00000003')) 4830abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.00000002') 4831353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_plus(Decimal('-Infinity')) 4832abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-9.99999999E+999') 48336d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.next_plus(1) 48346d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1.00000001') 4835353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 48366d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4837353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.next_plus(context=self) 4838353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4839353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_toward(self, a, b): 4840353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the number closest to a, in direction towards b. 4841353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4842353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the closest representable number from the first 4843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista operand (but not the first operand) that is in the direction 4844353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista towards the second operand, unless the operands have the same 4845353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value. 4846353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4847353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4848353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4849353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4850353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('1'), Decimal('2')) 4851abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.00000001') 4852353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('-1E-1007'), Decimal('1')) 4853abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0E-1007') 4854353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('-1.00000003'), Decimal('0')) 4855abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.00000002') 4856353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('1'), Decimal('0')) 4857abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.999999999') 4858353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('1E-1007'), Decimal('-100')) 4859abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0E-1007') 4860353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10')) 4861abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.00000004') 4862353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000')) 4863abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0.00') 48646d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.next_toward(0, 1) 48656d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1E-1007') 48666d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.next_toward(Decimal(0), 1) 48676d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1E-1007') 48686d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.next_toward(0, Decimal(1)) 48696d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1E-1007') 4870353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 48716d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4872353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.next_toward(b, context=self) 4873353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 48747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def normalize(self, a): 4875e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger """normalize reduces an operand to its simplest form. 48767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 48777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Essentially a plus operation with all trailing zeros removed from the 48787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result. 48797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 48809ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('2.1')) 4881abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.1') 48829ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('-2.0')) 4883abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-2') 48849ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('1.200')) 4885abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.2') 48869ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('-120')) 4887abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.2E+2') 48889ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('120.00')) 4889abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.2E+2') 48909ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('0.00')) 4891abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 48926d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.normalize(6) 48936d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('6') 48947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 48956d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 48967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.normalize(context=self) 48977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4898353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def number_class(self, a): 4899353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns an indication of the class of the operand. 4900353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4901353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The class is one of the following strings: 4902353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -sNaN 4903353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -NaN 4904353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Infinity 4905353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Normal 4906353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Subnormal 4907353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Zero 4908353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Zero 4909353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Subnormal 4910353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Normal 4911353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Infinity 4912353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4913353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = Context(ExtendedContext) 4914353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4915353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4916353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('Infinity')) 4917353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Infinity' 4918353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('1E-10')) 4919353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Normal' 4920353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('2.50')) 4921353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Normal' 4922353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('0.1E-999')) 4923353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Subnormal' 4924353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('0')) 4925353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Zero' 4926353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-0')) 4927353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Zero' 4928353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-0.1E-999')) 4929353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Subnormal' 4930353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-1E-10')) 4931353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Normal' 4932353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-2.50')) 4933353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Normal' 4934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-Infinity')) 4935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Infinity' 4936353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('NaN')) 4937353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'NaN' 4938353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-NaN')) 4939353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'NaN' 4940353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('sNaN')) 4941353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'sNaN' 49426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> c.number_class(123) 49436d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson '+Normal' 4944353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 49456d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 4946353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.number_class(context=self) 4947353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 49487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def plus(self, a): 49497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Plus corresponds to unary prefix plus in Python. 49507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 49517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is evaluated using the same rules as add; the 49527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation plus(a) is calculated as add('0', a) where the '0' 49537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger has the same exponent as the operand. 49547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 49559ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.plus(Decimal('1.3')) 4956abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.3') 49579ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.plus(Decimal('-1.3')) 4958abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1.3') 49596d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.plus(-1) 49606d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('-1') 49617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 49626d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 49637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__pos__(context=self) 49647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 49657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def power(self, a, b, modulo=None): 49667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Raises a to the power of b, to modulo if given. 49677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4968353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With two arguments, compute a**b. If a is negative then b 4969353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista must be integral. The result will be inexact unless b is 4970353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista integral and the result is finite and can be expressed exactly 4971353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in 'precision' digits. 4972353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4973353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With three arguments, compute (a**b) % modulo. For the 4974353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista three argument form, the following restrictions on the 4975353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista arguments hold: 4976353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4977353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - all three arguments must be integral 4978353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - b must be nonnegative 4979353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - at least one of a or b must be nonzero 4980353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - modulo must be nonzero and have at most 'precision' digits 4981353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4982353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result of pow(a, b, modulo) is identical to the result 4983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista that would be obtained by computing (a**b) % modulo with 4984353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista unbounded precision, but is computed more efficiently. It is 4985353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista always exact. 4986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4987353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4988353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4989353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4990353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('2'), Decimal('3')) 4991abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('8') 4992353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-2'), Decimal('3')) 4993abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-8') 4994353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('2'), Decimal('-3')) 4995abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.125') 4996353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('1.7'), Decimal('8')) 4997abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('69.7575744') 4998353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('10'), Decimal('0.301029996')) 4999abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.00000000') 5000353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('Infinity'), Decimal('-1')) 5001abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 5002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('Infinity'), Decimal('0')) 5003abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 5004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('Infinity'), Decimal('1')) 5005abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('Infinity') 5006353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('-1')) 5007abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0') 5008353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('0')) 5009abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 5010353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('1')) 5011abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-Infinity') 5012353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('2')) 5013abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('Infinity') 5014353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('0'), Decimal('0')) 5015abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('NaN') 5016353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5017353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('3'), Decimal('7'), Decimal('16')) 5018abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('11') 5019353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16')) 5020abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-11') 5021353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16')) 5022abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 5023353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16')) 5024abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('11') 5025353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789')) 5026abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('11729830') 5027353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729')) 5028abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0') 5029353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537')) 5030abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 50316d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.power(7, 7) 50326d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('823543') 50336d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.power(Decimal(7), 7) 50346d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('823543') 50356d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.power(7, Decimal(7), 2) 50366d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 50377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 50386d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 50396d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__pow__(b, modulo, context=self) 50406d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 50416d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 50426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 50436d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 50447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def quantize(self, a, b): 504659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista """Returns a value equal to 'a' (rounded), having the exponent of 'b'. 50477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The coefficient of the result is derived from that of the left-hand 504959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operand. It may be rounded using the current rounding setting (if the 50507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exponent is being increased), multiplied by a positive power of ten (if 50517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the exponent is being decreased), or is unchanged (if the exponent is 50527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger already equal to that of the right-hand operand). 50537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Unlike other operations, if the length of the coefficient after the 50557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger quantize operation would be greater than precision then an Invalid 505659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation condition is raised. This guarantees that, unless there is 505759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista an error condition, the exponent of the result of a quantize is always 50587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger equal to that of the right-hand operand. 50597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Also unlike other operations, quantize will never raise Underflow, even 50617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if the result is subnormal and inexact. 50627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50639ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001')) 5064abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.170') 50659ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01')) 5066abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.17') 50679ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1')) 5068abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.2') 50699ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0')) 5070abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2') 50719ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1')) 5072abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0E+1') 50739ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity')) 5074abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-Infinity') 50759ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity')) 5076abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('NaN') 50779ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1')) 5078abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0') 50799ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5')) 5080abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0E+5') 50819ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2')) 5082abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('NaN') 50839ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2')) 5084abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('NaN') 50859ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1')) 5086abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('217.0') 50879ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0')) 5088abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('217') 50899ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1')) 5090abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.2E+2') 50919ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2')) 5092abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2E+2') 50936d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.quantize(1, 2) 50946d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 50956d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.quantize(Decimal(1), 2) 50966d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 50976d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.quantize(1, Decimal(2)) 50986d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1') 50997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 51006d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 51017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.quantize(b, context=self) 51027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5103353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def radix(self): 5104353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Just returns 10, as this is Decimal, :) 5105353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5106353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.radix() 5107abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('10') 5108353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 5109353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(10) 5110353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 51117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def remainder(self, a, b): 51127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the remainder from integer division. 51137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result is the residue of the dividend after the operation of 511559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista calculating integer division as described for divide-integer, rounded 51160d4c06e06e5ee1f3bb1fa8068114bd700d74864aNeal Norwitz to precision digits if necessary. The sign of the result, if 511759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista non-zero, is the same as that of the original dividend. 51187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This operation will fail under the same conditions as integer division 51207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (that is, if integer division on the same two operands would fail, the 51217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger remainder cannot be calculated). 51227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51239ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3')) 5124abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.1') 51259ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('10'), Decimal('3')) 5126abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 51279ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3')) 5128abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 51299ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1')) 5130abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.2') 51319ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3')) 5132abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.1') 51339ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3')) 5134abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.0') 51356d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.remainder(22, 6) 51366d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('4') 51376d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.remainder(Decimal(22), 6) 51386d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('4') 51396d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.remainder(22, Decimal(6)) 51406d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('4') 51417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 51426d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 51436d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__mod__(b, context=self) 51446d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 51456d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 51466d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 51476d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 51487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def remainder_near(self, a, b): 51507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns to be "a - b * n", where n is the integer nearest the exact 51517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger value of "x / b" (if two integers are equally near then the even one 515259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista is chosen). If the result is equal to 0 then its sign will be the 51537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sign of a. 51547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This operation will fail under the same conditions as integer division 51567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (that is, if integer division on the same two operands would fail, the 51577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger remainder cannot be calculated). 51587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51599ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3')) 5160abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0.9') 51619ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6')) 5162abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-2') 51639ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3')) 5164abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 51659ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3')) 5166abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-1') 51679ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1')) 5168abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.2') 51699ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3')) 5170abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.1') 51719ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3')) 5172abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0.3') 51736d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.remainder_near(3, 11) 51746d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 51756d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.remainder_near(Decimal(3), 11) 51766d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 51776d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.remainder_near(3, Decimal(11)) 51786d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 51797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 51806d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 51817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.remainder_near(b, context=self) 51827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5183353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def rotate(self, a, b): 5184353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a rotated copy of a, b times. 5185353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5186353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The coefficient of the result is a rotated copy of the digits in 5187353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the coefficient of the first operand. The number of places of 5188353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotation is taken from the absolute value of the second operand, 5189353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista with the rotation being to the left if the second operand is 5190353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista positive or to the right otherwise. 5191353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5192353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('34'), Decimal('8')) 5193abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('400000003') 5194353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('12'), Decimal('9')) 5195abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('12') 5196353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2')) 5197abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('891234567') 5198353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0')) 5199abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('123456789') 5200353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2')) 5201abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('345678912') 52026d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.rotate(1333333, 1) 52036d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('13333330') 52046d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.rotate(Decimal(1333333), 1) 52056d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('13333330') 52066d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.rotate(1333333, Decimal(1)) 52076d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('13333330') 5208353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 52096d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 5210353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.rotate(b, context=self) 5211353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 52127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def same_quantum(self, a, b): 52137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns True if the two operands have the same exponent. 52147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 52157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result is never affected by either the sign or the coefficient of 52167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger either operand. 52177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 52189ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001')) 52197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger False 52209ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01')) 52217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger True 52229ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1')) 52237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger False 52249ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf')) 52257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger True 52266d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.same_quantum(10000, -1) 52276d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson True 52286d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.same_quantum(Decimal(10000), -1) 52296d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson True 52306d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.same_quantum(10000, Decimal(-1)) 52316d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson True 52327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 52336d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 52347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.same_quantum(b) 52357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5236353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def scaleb (self, a, b): 5237353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the first operand after adding the second value its exp. 5238353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5239353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2')) 5240abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.0750') 5241353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0')) 5242abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('7.50') 5243353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3')) 5244abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('7.50E+3') 52456d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.scaleb(1, 4) 52466d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1E+4') 52476d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.scaleb(Decimal(1), 4) 52486d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1E+4') 52496d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.scaleb(1, Decimal(4)) 52506d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1E+4') 5251353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 52526d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 52536d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return a.scaleb(b, context=self) 5254353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5255353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def shift(self, a, b): 5256353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a shifted copy of a, b times. 5257353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5258353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The coefficient of the result is a shifted copy of the digits 5259353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in the coefficient of the first operand. The number of places 5260353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to shift is taken from the absolute value of the second operand, 5261353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista with the shift being to the left if the second operand is 5262353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista positive or to the right otherwise. Digits shifted into the 5263353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coefficient are zeros. 5264353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5265353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('34'), Decimal('8')) 5266abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('400000000') 5267353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('12'), Decimal('9')) 5268abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 5269353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2')) 5270abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1234567') 5271353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0')) 5272abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('123456789') 5273353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2')) 5274abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('345678900') 52756d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.shift(88888888, 2) 52766d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('888888800') 52776d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.shift(Decimal(88888888), 2) 52786d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('888888800') 52796d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.shift(88888888, Decimal(2)) 52806d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('888888800') 5281353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 52826d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 5283353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.shift(b, context=self) 5284353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 52857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def sqrt(self, a): 528659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista """Square root of a non-negative number to context precision. 52877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 52887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the result must be inexact, it is rounded using the round-half-even 52897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger algorithm. 52907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 52919ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('0')) 5292abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0') 52939ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('-0')) 5294abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0') 52959ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('0.39')) 5296abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.624499800') 52979ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('100')) 5298abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('10') 52999ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('1')) 5300abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1') 53019ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('1.0')) 5302abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.0') 53039ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('1.00')) 5304abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.0') 53059ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('7')) 5306abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2.64575131') 53079ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('10')) 5308abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('3.16227766') 53096d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.sqrt(2) 53106d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('1.41421356') 53119ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.prec 53126ea484582238a65d44a76e3a831e3ba7c77a1a25Raymond Hettinger 9 53137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 53146d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 53157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.sqrt(context=self) 53167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 53177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def subtract(self, a, b): 5318f33d01d304c349a7f555779c7c99930f1203adb7Georg Brandl """Return the difference between the two operands. 53197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 53209ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07')) 5321abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.23') 53229ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30')) 5323abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('0.00') 53249ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07')) 5325abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-0.77') 53266d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.subtract(8, 5) 53276d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 53286d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.subtract(Decimal(8), 5) 53296d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 53306d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson >>> ExtendedContext.subtract(8, Decimal(5)) 53316d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson Decimal('3') 53327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 53336d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 53346d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson r = a.__sub__(b, context=self) 53356d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson if r is NotImplemented: 53366d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson raise TypeError("Unable to convert %s to Decimal" % b) 53376d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson else: 53386d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson return r 53397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 53407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def to_eng_string(self, a): 5341af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger """Convert to a string, using engineering notation if an exponent is needed. 5342af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger 5343af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger Engineering notation has an exponent which is a multiple of 3. This 5344af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger can leave up to 3 digits to the left of the decimal place and may 5345af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger require the addition of either one or two trailing zeros. 53467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 53477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is not affected by the context. 5348af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger 5349af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger >>> ExtendedContext.to_eng_string(Decimal('123E+1')) 5350af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger '1.23E+3' 5351af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger >>> ExtendedContext.to_eng_string(Decimal('123E+3')) 5352af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger '123E+3' 5353af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger >>> ExtendedContext.to_eng_string(Decimal('123E-10')) 5354af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger '12.3E-9' 5355af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger >>> ExtendedContext.to_eng_string(Decimal('-123E-12')) 5356af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger '-123E-12' 5357af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger >>> ExtendedContext.to_eng_string(Decimal('7E-7')) 5358af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger '700E-9' 5359af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger >>> ExtendedContext.to_eng_string(Decimal('7E+1')) 5360af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger '70' 5361af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger >>> ExtendedContext.to_eng_string(Decimal('0E+1')) 5362af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger '0.00E+3' 5363af0b38f8145636d9caf8da847f8c635c7ccddc30Raymond Hettinger 53647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 53656d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 53667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.to_eng_string(context=self) 53677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 53687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def to_sci_string(self, a): 53697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Converts a number to a string, using scientific notation. 53707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 53717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is not affected by the context. 53727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 53736d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 53747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__str__(context=self) 53757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5376353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_exact(self, a): 5377353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds to an integer. 5378353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5379353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista When the operand has a negative exponent, the result is the same 5380353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista as using the quantize() operation using the given operand as the 5381353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista left-hand-operand, 1E+0 as the right-hand-operand, and the precision 5382353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of the operand as the precision setting; Inexact and Rounded flags 5383353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista are allowed in this operation. The rounding mode is taken from the 5384353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context. 5385353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5386353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('2.1')) 5387abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2') 5388353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('100')) 5389abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('100') 5390353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('100.0')) 5391abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('100') 5392353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('101.5')) 5393abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('102') 5394353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('-101.5')) 5395abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-102') 5396353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('10E+5')) 5397abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.0E+6') 5398353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77')) 5399abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('7.89E+77') 5400353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('-Inf')) 5401abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-Infinity') 5402353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 54036d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 5404353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.to_integral_exact(context=self) 5405353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5406353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_value(self, a): 54077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Rounds to an integer. 54087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger When the operand has a negative exponent, the result is the same 54107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger as using the quantize() operation using the given operand as the 54117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger left-hand-operand, 1E+0 as the right-hand-operand, and the precision 54127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger of the operand as the precision setting, except that no flags will 541359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista be set. The rounding mode is taken from the context. 54147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5415353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('2.1')) 5416abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('2') 5417353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('100')) 5418abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('100') 5419353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('100.0')) 5420abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('100') 5421353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('101.5')) 5422abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('102') 5423353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('-101.5')) 5424abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-102') 5425353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('10E+5')) 5426abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('1.0E+6') 5427353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('7.89E+77')) 5428abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('7.89E+77') 5429353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('-Inf')) 5430abe32371878dcaea31c835e10144fdaa2eca6492Raymond Hettinger Decimal('-Infinity') 54317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 54326d8effb1fc6660921e5f61fdc2c898e86356c64bMark Dickinson a = _convert_other(a, raiseit=True) 5433353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.to_integral_value(context=self) 5434353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5435353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the method name changed, but we provide also the old one, for compatibility 5436353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to_integral = to_integral_value 54377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass _WorkRep(object): 54397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __slots__ = ('sign','int','exp') 544017931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger # sign: 0 or 1 5441636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # int: int or long 54427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # exp: None, int, or string 54437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __init__(self, value=None): 54457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if value is None: 54467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.sign = None 5447636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self.int = 0 54487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.exp = None 544917931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger elif isinstance(value, Decimal): 545017931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger self.sign = value._sign 545172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self.int = int(value._int) 54527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.exp = value._exp 545317931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger else: 545417931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger # assert isinstance(value, tuple) 54557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.sign = value[0] 54567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.int = value[1] 54577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.exp = value[2] 54587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __repr__(self): 54607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return "(%r, %r, %r)" % (self.sign, self.int, self.exp) 54617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __str__ = __repr__ 54637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5466e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batistadef _normalize(op1, op2, prec = 0): 54677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Normalizes op1, op2 to have the same exp and length of coefficient. 54687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 54697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Done during addition. 54707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 5471353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op1.exp < op2.exp: 54727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger tmp = op2 54737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger other = op1 54747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 54757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger tmp = op1 54767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger other = op2 54777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5478353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1). 5479353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Then adding 10**exp to tmp has the same effect (after rounding) 5480353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # as adding any positive quantity smaller than 10**exp; similarly 5481353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for subtraction. So if other is smaller than 10**exp we replace 5482353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # it with 10**exp. This avoids tmp.exp - other.exp getting too large. 5483e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista tmp_len = len(str(tmp.int)) 5484e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista other_len = len(str(other.int)) 5485e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista exp = tmp.exp + min(-1, tmp_len - prec - 2) 5486e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista if other_len + other.exp - 1 < exp: 5487e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista other.int = 1 5488e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista other.exp = exp 5489636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 5490353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista tmp.int *= 10 ** (tmp.exp - other.exp) 5491353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista tmp.exp = other.exp 54927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return op1, op2 54937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5494353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista##### Integer arithmetic functions used by ln, log10, exp and __pow__ ##### 5495353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5496353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# This function from Tim Peters was taken from here: 5497353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# http://mail.python.org/pipermail/python-list/1999-July/007758.html 5498353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# The correction being in the function definition is for speed, and 5499353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# the whole function is not resolved with math.log because of avoiding 5500353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# the use of floats. 5501353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _nbits(n, correction = { 5502353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '0': 4, '1': 3, '2': 2, '3': 2, 5503353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '4': 1, '5': 1, '6': 1, '7': 1, 5504353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '8': 0, '9': 0, 'a': 0, 'b': 0, 5505353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'c': 0, 'd': 0, 'e': 0, 'f': 0}): 5506353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Number of bits in binary representation of the positive integer n, 5507353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista or 0 if n == 0. 5508353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 5509353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n < 0: 5510353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise ValueError("The argument to _nbits should be nonnegative.") 5511353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista hex_n = "%x" % n 5512353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return 4*len(hex_n) - correction[hex_n[0]] 5513353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5514a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinsondef _decimal_lshift_exact(n, e): 5515a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson """ Given integers n and e, return n * 10**e if it's an integer, else None. 5516a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 5517a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson The computation is designed to avoid computing large powers of 10 5518a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson unnecessarily. 5519a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 5520a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson >>> _decimal_lshift_exact(3, 4) 5521a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 30000 5522a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson >>> _decimal_lshift_exact(300, -999999999) # returns None 5523a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 5524a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson """ 5525a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson if n == 0: 5526a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson return 0 5527a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson elif e >= 0: 5528a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson return n * 10**e 5529a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson else: 5530a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson # val_n = largest power of 10 dividing n. 5531a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson str_n = str(abs(n)) 5532a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson val_n = len(str_n) - len(str_n.rstrip('0')) 5533a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson return None if val_n < -e else n // 10**-e 5534a493ca3fae4dc6e07c3dc57eb7621c90ba8c7189Mark Dickinson 5535353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _sqrt_nearest(n, a): 5536353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Closest integer to the square root of the positive integer n. a is 5537353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista an initial approximation to the square root. Any positive integer 5538353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista will do for a, but the closer a is to the square root of n the 5539353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista faster convergence will be. 5540353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5541353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 5542353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n <= 0 or a <= 0: 5543353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise ValueError("Both arguments to _sqrt_nearest should be positive.") 5544353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5545353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b=0 5546353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while a != b: 5547353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b, a = a, a--n//a>>1 5548353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a 5549353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5550353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _rshift_nearest(x, shift): 5551353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given an integer x and a nonnegative integer shift, return closest 5552353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista integer to x / 2**shift; use round-to-even in case of a tie. 5553353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5554353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 5555353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b, q = 1L << shift, x >> shift 5556353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return q + (2*(x & (b-1)) + (q&1) > b) 5557353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5558353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _div_nearest(a, b): 5559353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Closest integer to a/b, a and b positive integers; rounds to even 5560353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in the case of a tie. 5561353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5562353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 5563353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q, r = divmod(a, b) 5564353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return q + (2*r + (q&1) > b) 5565353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5566353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _ilog(x, M, L = 8): 5567353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Integer approximation to M*log(x/M), with absolute error boundable 5568353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in terms only of x/M. 5569353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5570353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Given positive integers x and M, return an integer approximation to 5571353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista M * log(x/M). For L = 8 and 0.1 <= x/M <= 10 the difference 5572353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista between the approximation and the exact result is at most 22. For 5573353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15. In 5574353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista both cases these are upper bounds on the error; it will usually be 5575353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista much smaller.""" 5576353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5577353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # The basic algorithm is the following: let log1p be the function 5578353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log1p(x) = log(1+x). Then log(x/M) = log1p((x-M)/M). We use 5579353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the reduction 5580353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5581353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log1p(y) = 2*log1p(y/(1+sqrt(1+y))) 5582353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5583353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # repeatedly until the argument to log1p is small (< 2**-L in 5584353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # absolute value). For small y we can use the Taylor series 5585353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expansion 5586353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5587353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log1p(y) ~ y - y**2/2 + y**3/3 - ... - (-y)**T/T 5588353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5589353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # truncating at T such that y**T is small enough. The whole 5590353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # computation is carried out in a form of fixed-point arithmetic, 5591353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # with a real number z being represented by an integer 5592353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # approximation to z*M. To avoid loss of precision, the y below 5593353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # is actually an integer approximation to 2**R*y*M, where R is the 5594353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # number of reductions performed so far. 5595353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5596353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = x-M 5597353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # argument reduction; R = number of reductions performed 5598353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R = 0 5599353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while (R <= L and long(abs(y)) << L-R >= M or 5600353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R > L and abs(y) >> R-L >= M): 5601353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(long(M*y) << 1, 5602353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista M + _sqrt_nearest(M*(M+_rshift_nearest(y, R)), M)) 5603353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R += 1 5604353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5605353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Taylor series with T terms 5606353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista T = -int(-10*len(str(M))//(3*L)) 5607353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yshift = _rshift_nearest(y, R) 5608353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista w = _div_nearest(M, T) 5609353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for k in xrange(T-1, 0, -1): 5610353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista w = _div_nearest(M, k) - _div_nearest(yshift*w, M) 5611353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5612353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(w*y, M) 5613353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5614353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dlog10(c, e, p): 5615353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers c, e and p with c > 0, p >= 0, compute an integer 5616353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista approximation to 10**p * log10(c*10**e), with an absolute error of 5617353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista at most 1. Assumes that c*10**e is not exactly 1.""" 5618353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5619353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # increase precision by 2; compensate for this by dividing 5620353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # final result by 100 5621353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p += 2 5622353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5623353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # write c*10**e as d*10**f with either: 5624353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # f >= 0 and 1 <= d <= 10, or 5625353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # f <= 0 and 0.1 <= d <= 1. 5626353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Thus for c*10**e close to 1, f = 0 5627353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = len(str(c)) 5628353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f = e+l - (e+l >= 1) 5629353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5630353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if p > 0: 5631353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista M = 10**p 5632353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista k = e+p-f 5633353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if k >= 0: 5634353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c *= 10**k 5635353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5636353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = _div_nearest(c, 10**-k) 5637353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5638353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = _ilog(c, M) # error < 5 + 22 = 27 5639be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista log_10 = _log10_digits(p) # error < 1 5640353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = _div_nearest(log_d*M, log_10) 5641353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_tenpower = f*M # exact 5642353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5643353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = 0 # error < 2.31 564418aa388ca084e1d40aa48c8c8f1b4f730c6fe059Neal Norwitz log_tenpower = _div_nearest(f, 10**-p) # error < 0.5 5645353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5646353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(log_tenpower+log_d, 100) 5647353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5648353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dlog(c, e, p): 5649353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers c, e and p with c > 0, compute an integer 5650353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista approximation to 10**p * log(c*10**e), with an absolute error of 5651353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista at most 1. Assumes that c*10**e is not exactly 1.""" 5652353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5653353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Increase precision by 2. The precision increase is compensated 5654353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for at the end with a division by 100. 5655353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p += 2 5656353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5657353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rewrite c*10**e as d*10**f with either f >= 0 and 1 <= d <= 10, 5658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # or f <= 0 and 0.1 <= d <= 1. Then we can compute 10**p * log(c*10**e) 5659353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # as 10**p * log(d) + 10**p*f * log(10). 5660353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = len(str(c)) 5661353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f = e+l - (e+l >= 1) 5662353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5663353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute approximation to 10**p*log(d), with error < 27 5664353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if p > 0: 5665353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista k = e+p-f 5666353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if k >= 0: 5667353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c *= 10**k 5668353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5669353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = _div_nearest(c, 10**-k) # error of <= 0.5 in c 5670353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5671353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # _ilog magnifies existing error in c by a factor of at most 10 5672353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = _ilog(c, 10**p) # error < 5 + 22 = 27 5673353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5674353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p <= 0: just approximate the whole thing by 0; error < 2.31 5675353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = 0 5676353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5677be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute approximation to f*10**p*log(10), with error < 11. 5678353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if f: 5679be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista extra = len(str(abs(f)))-1 5680be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if p + extra >= 0: 5681be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # error in f * _log10_digits(p+extra) < |f| * 1 = |f| 5682be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # after division, error < |f|/10**extra + 0.5 < 10 + 0.5 < 11 5683be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista f_log_ten = _div_nearest(f*_log10_digits(p+extra), 10**extra) 5684353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5685353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f_log_ten = 0 5686353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5687353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f_log_ten = 0 5688353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5689be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # error in sum < 11+27 = 38; error after division < 0.38 + 0.5 < 1 5690353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(f_log_ten + log_d, 100) 5691353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5692be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batistaclass _Log10Memoize(object): 5693be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista """Class to compute, store, and allow retrieval of, digits of the 5694be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista constant log(10) = 2.302585.... This constant is needed by 5695be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__.""" 5696be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista def __init__(self): 5697be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista self.digits = "23025850929940456840179914546843642076011014886" 5698be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 5699be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista def getdigits(self, p): 5700be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista """Given an integer p >= 0, return floor(10**p)*log(10). 5701be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 5702be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista For example, self.getdigits(3) returns 2302. 5703be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista """ 5704be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # digits are stored as a string, for quick conversion to 5705be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # integer in the case that we've already computed enough 5706be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # digits; the stored digits should always be correct 5707be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # (truncated, not rounded to nearest). 5708be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if p < 0: 5709be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista raise ValueError("p should be nonnegative") 5710be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 5711be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if p >= len(self.digits): 5712be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute p+3, p+6, p+9, ... digits; continue until at 5713be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # least one of the extra digits is nonzero 5714be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista extra = 3 5715be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista while True: 5716be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute p+extra digits, correct to within 1ulp 5717be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista M = 10**(p+extra+2) 5718be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista digits = str(_div_nearest(_ilog(10*M, M), 100)) 5719be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if digits[-extra:] != '0'*extra: 5720be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista break 5721be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista extra += 3 5722be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # keep all reliable digits so far; remove trailing zeros 5723be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # and next nonzero digit 5724be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista self.digits = digits.rstrip('0')[:-1] 5725be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista return int(self.digits[:p+1]) 5726be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 5727be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista_log10_digits = _Log10Memoize().getdigits 5728be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 5729353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _iexp(x, M, L=8): 5730353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers x and M, M > 0, such that x/M is small in absolute 5731353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value, compute an integer approximation to M*exp(x/M). For 0 <= 5732353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista x/M <= 2.4, the absolute error in the result is bounded by 60 (and 5733353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista is usually much smaller).""" 5734353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5735353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Algorithm: to compute exp(z) for a real number z, first divide z 5736353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # by a suitable power R of 2 so that |z/2**R| < 2**-L. Then 5737353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor 5738353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # series 5739353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5740353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expm1(x) = x + x**2/2! + x**3/3! + ... 5741353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5742353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Now use the identity 5743353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5744353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expm1(2x) = expm1(x)*(expm1(x)+2) 5745353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 5746353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # R times to compute the sequence expm1(z/2**R), 5747353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expm1(z/2**(R-1)), ... , exp(z/2), exp(z). 5748353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5749353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Find R such that x/2**R/M <= 2**-L 5750353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R = _nbits((long(x)<<L)//M) 5751353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5752353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Taylor series. (2**L)**T > M 5753353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista T = -int(-10*len(str(M))//(3*L)) 5754353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(x, T) 5755353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Mshift = long(M)<<R 5756353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for i in xrange(T-1, 0, -1): 5757353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(x*(Mshift + y), Mshift * i) 5758353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5759353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Expansion 5760353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for k in xrange(R-1, -1, -1): 5761353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Mshift = long(M)<<(k+2) 5762353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(y*(y+Mshift), Mshift) 5763353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5764353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return M+y 5765353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5766353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dexp(c, e, p): 5767353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute an approximation to exp(c*10**e), with p decimal places of 5768353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista precision. 5769353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5770be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista Returns integers d, f such that: 5771353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5772353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 10**(p-1) <= d <= 10**p, and 5773353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (d-1)*10**f < exp(c*10**e) < (d+1)*10**f 5774353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5775353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In other words, d*10**f is an approximation to exp(c*10**e) with p 5776353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits of precision, and with an error in d of at most 1. This is 5777353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista almost, but not quite, the same as the error being < 1ulp: when d 5778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista = 10**(p-1) the error could be up to 10 ulp.""" 5779353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5780353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we'll call iexp with M = 10**(p+2), giving p+3 digits of precision 5781353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p += 2 5782353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5783be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute log(10) with extra precision = adjusted exponent of c*10**e 5784353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra = max(0, e + len(str(c)) - 1) 5785353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q = p + extra 5786353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5787be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute quotient c*10**e/(log(10)) = c*10**(e+q)/(log(10)*10**q), 5788353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rounding down 5789353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista shift = e+q 5790353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 5791353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista cshift = c*10**shift 5792353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5793353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista cshift = c//10**-shift 5794be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista quot, rem = divmod(cshift, _log10_digits(q)) 5795353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5796353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # reduce remainder back to original precision 5797353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rem = _div_nearest(rem, 10**extra) 5798353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5799353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # error in result of _iexp < 120; error after division < 0.62 5800353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3 5801353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5802353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dpower(xc, xe, yc, ye, p): 5803353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and 5804353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that: 5805353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5806353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 10**(p-1) <= c <= 10**p, and 5807353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (c-1)*10**e < x**y < (c+1)*10**e 5808353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5809353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in other words, c*10**e is an approximation to x**y with p digits 5810353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of precision, and with an error in c of at most 1. (This is 5811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista almost, but not quite, the same as the error being < 1ulp: when c 5812353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista == 10**(p-1) we can only guarantee error < 10ulp.) 5813353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5814353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista We assume that: x is positive and not equal to 1, and y is nonzero. 5815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 5816353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Find b such that 10**(b-1) <= |y| <= 10**b 5818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b = len(str(abs(yc))) + ye 5819353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point 5821353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista lxc = _dlog(xc, xe, p+b+1) 5822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5823353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1) 5824353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista shift = ye-b 5825353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 5826353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista pc = lxc*yc*10**shift 5827353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5828353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista pc = _div_nearest(lxc*yc, 10**-shift) 5829353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5830353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if pc == 0: 5831353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we prefer a result that isn't exactly 1; this makes it 5832353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # easier to compute a correctly rounded result in __pow__ 5833353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1: 5834353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = 10**(p-1)+1, 1-p 5835353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5836353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = 10**p-1, -p 5837353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5838353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = _dexp(pc, -(p+1), p+1) 5839353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff = _div_nearest(coeff, 10) 5840353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp += 1 5841353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5842353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return coeff, exp 5843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5844353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _log10_lb(c, correction = { 5845353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '1': 100, '2': 70, '3': 53, '4': 40, '5': 31, 5846353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '6': 23, '7': 16, '8': 10, '9': 5}): 5847353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute a lower bound for 100*log10(c) for a positive integer c.""" 5848353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c <= 0: 5849353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise ValueError("The argument to _log10_lb should be nonnegative.") 5850353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista str_c = str(c) 5851353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return 100*len(str_c) - correction[str_c[0]] 5852353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 585359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Helper Functions #################################################### 58547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 585599d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinsondef _convert_other(other, raiseit=False, allow_float=False): 5856636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger """Convert other to Decimal. 5857636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 5858636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger Verifies that it's ok to use in an implicit construction. 585999d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson If allow_float is true, allow conversion from float; this 586099d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson is used in the comparison methods (__eq__ and friends). 586199d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson 5862636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger """ 5863636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(other, Decimal): 5864636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return other 5865636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(other, (int, long)): 5866636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Decimal(other) 586799d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson if allow_float and isinstance(other, float): 586899d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson return Decimal.from_float(other) 586999d8096c174ccb025e8ff55e614ea3820f89e204Mark Dickinson 5870353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if raiseit: 5871353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise TypeError("Unable to convert %s to Decimal" % other) 5872267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return NotImplemented 5873636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 587459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Setup Specific Contexts ############################################ 58757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 58767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# The default context prototype used by Context() 5877fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger# Is mutable, so that new contexts can have different default values 58787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 58797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDefaultContext = Context( 58806ea484582238a65d44a76e3a831e3ba7c77a1a25Raymond Hettinger prec=28, rounding=ROUND_HALF_EVEN, 5881bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps=[DivisionByZero, Overflow, InvalidOperation], 5882bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger flags=[], 588399148e7eaad7741fbfb0822009b7c9b216ecc227Raymond Hettinger Emax=999999999, 588499148e7eaad7741fbfb0822009b7c9b216ecc227Raymond Hettinger Emin=-999999999, 5885e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger capitals=1 58867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger) 58877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 58887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# Pre-made alternate contexts offered by the specification 58897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# Don't change these; the user should be able to select these 58907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# contexts and be able to reproduce results from other implementations 58917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# of the spec. 58927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 58939ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond HettingerBasicContext = Context( 58947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger prec=9, rounding=ROUND_HALF_UP, 5895bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow], 5896bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger flags=[], 58977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger) 58987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 58999ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond HettingerExtendedContext = Context( 59006ea484582238a65d44a76e3a831e3ba7c77a1a25Raymond Hettinger prec=9, rounding=ROUND_HALF_EVEN, 5901bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps=[], 5902bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger flags=[], 59037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger) 59047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 59057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 590659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### crud for parsing strings ############################################# 59076a123cb7827859748a0096570dfbb5ceba0e59dcMark Dickinson# 590872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# Regular expression used for parsing numeric strings. Additional 590972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# comments: 591072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 591172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 1. Uncomment the two '\s*' lines to allow leading and/or trailing 591272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# whitespace. But note that the specification disallows whitespace in 591372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# a numeric string. 591472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 591572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 2. For finite numbers (not infinities and NaNs) the body of the 591672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# number between the optional sign and the optional exponent must have 591772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# at least one decimal digit, possibly after the decimal point. The 591872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# lookahead expression '(?=\d|\.\d)' checks this. 59197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 592072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batistaimport re 592170c3289085d08d97edbfaa626efc2d2c2ac21859Mark Dickinson_parser = re.compile(r""" # A numeric string consists of: 59227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# \s* 592370c3289085d08d97edbfaa626efc2d2c2ac21859Mark Dickinson (?P<sign>[-+])? # an optional sign, followed by either... 59247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ( 59254326ad8f72053140aa658a0392a509c9da382670Mark Dickinson (?=\d|\.\d) # ...a number (with at least one digit) 59264326ad8f72053140aa658a0392a509c9da382670Mark Dickinson (?P<int>\d*) # having a (possibly empty) integer part 59274326ad8f72053140aa658a0392a509c9da382670Mark Dickinson (\.(?P<frac>\d*))? # followed by an optional fractional part 59284326ad8f72053140aa658a0392a509c9da382670Mark Dickinson (E(?P<exp>[-+]?\d+))? # followed by an optional exponent, or... 59297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger | 593070c3289085d08d97edbfaa626efc2d2c2ac21859Mark Dickinson Inf(inity)? # ...an infinity, or... 593172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista | 593270c3289085d08d97edbfaa626efc2d2c2ac21859Mark Dickinson (?P<signal>s)? # ...an (optionally signaling) 593370c3289085d08d97edbfaa626efc2d2c2ac21859Mark Dickinson NaN # NaN 59344326ad8f72053140aa658a0392a509c9da382670Mark Dickinson (?P<diag>\d*) # with (possibly empty) diagnostic info. 59357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ) 59367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# \s* 593759bc20bb273055e2cd48a467524d21340c1771ccMark Dickinson \Z 59384326ad8f72053140aa658a0392a509c9da382670Mark Dickinson""", re.VERBOSE | re.IGNORECASE | re.UNICODE).match 59397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 59402ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista_all_zeros = re.compile('0*$').match 59412ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista_exact_half = re.compile('50*$').match 59421ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 59431ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson##### PEP3101 support functions ############################################## 5944277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson# The functions in this section have little to do with the Decimal 5945277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson# class, and could potentially be reused or adapted for other pure 59461ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson# Python numeric classes that want to implement __format__ 59471ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson# 59481ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson# A format specifier for Decimal looks like: 59491ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson# 5950277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson# [[fill]align][sign][0][minimumwidth][,][.precision][type] 59511ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 59521ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson_parse_format_specifier_regex = re.compile(r"""\A 59531ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson(?: 59541ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson (?P<fill>.)? 59551ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson (?P<align>[<>=^]) 59561ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson)? 59571ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson(?P<sign>[-+ ])? 59581ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson(?P<zeropad>0)? 59591ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson(?P<minimumwidth>(?!0)\d+)? 5960277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson(?P<thousands_sep>,)? 59611ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson(?:\.(?P<precision>0|(?!0)\d+))? 5962277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson(?P<type>[eEfFgGn%])? 59631ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson\Z 59641ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson""", re.VERBOSE) 59651ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 59667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerdel re 59677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5968277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson# The locale module is only needed for the 'n' format specifier. The 5969277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson# rest of the PEP 3101 code functions quite happily without it, so we 5970277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson# don't care too much if locale isn't present. 5971277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsontry: 5972277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson import locale as _locale 5973277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsonexcept ImportError: 5974277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson pass 5975277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 5976277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsondef _parse_format_specifier(format_spec, _localeconv=None): 59771ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson """Parse and validate a format specifier. 59781ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 59791ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson Turns a standard numeric format specifier into a dict, with the 59801ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson following entries: 59811ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 59821ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson fill: fill character to pad field to minimum width 59831ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson align: alignment type, either '<', '>', '=' or '^' 59841ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson sign: either '+', '-' or ' ' 59851ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson minimumwidth: nonnegative integer giving minimum width 5986277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson zeropad: boolean, indicating whether to pad with zeros 5987277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson thousands_sep: string to use as thousands separator, or '' 5988277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson grouping: grouping for thousands separators, in format 5989277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson used by localeconv 5990277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson decimal_point: string to use for decimal point 59911ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson precision: nonnegative integer giving precision, or None 59921ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson type: one of the characters 'eEfFgG%', or None 5993277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson unicode: boolean (always True for Python 3.x) 59941ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 59951ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson """ 59961ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson m = _parse_format_specifier_regex.match(format_spec) 59971ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if m is None: 59981ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson raise ValueError("Invalid format specifier: " + format_spec) 59991ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 60001ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # get the dictionary 60011ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson format_dict = m.groupdict() 60021ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 6003277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # zeropad; defaults for fill and alignment. If zero padding 6004277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # is requested, the fill and align fields should be absent. 60051ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson fill = format_dict['fill'] 60061ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson align = format_dict['align'] 6007277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['zeropad'] = (format_dict['zeropad'] is not None) 6008277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if format_dict['zeropad']: 6009277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if fill is not None: 60101ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson raise ValueError("Fill character conflicts with '0'" 60111ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson " in format specifier: " + format_spec) 6012277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if align is not None: 60131ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson raise ValueError("Alignment conflicts with '0' in " 60141ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson "format specifier: " + format_spec) 60151ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson format_dict['fill'] = fill or ' ' 60165cfa8044ff9fd26dc4915fc2ce5b2d005eea3261Mark Dickinson # PEP 3101 originally specified that the default alignment should 60175cfa8044ff9fd26dc4915fc2ce5b2d005eea3261Mark Dickinson # be left; it was later agreed that right-aligned makes more sense 60185cfa8044ff9fd26dc4915fc2ce5b2d005eea3261Mark Dickinson # for numeric types. See http://bugs.python.org/issue6857. 60195cfa8044ff9fd26dc4915fc2ce5b2d005eea3261Mark Dickinson format_dict['align'] = align or '>' 60201ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 6021277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # default sign handling: '-' for negative, '' for positive 60221ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if format_dict['sign'] is None: 60231ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson format_dict['sign'] = '-' 60241ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 60251ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # minimumwidth defaults to 0; precision remains None if not given 60261ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson format_dict['minimumwidth'] = int(format_dict['minimumwidth'] or '0') 60271ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if format_dict['precision'] is not None: 60281ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson format_dict['precision'] = int(format_dict['precision']) 60291ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 60301ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # if format type is 'g' or 'G' then a precision of 0 makes little 60311ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # sense; convert it to 1. Same if format type is unspecified. 60321ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if format_dict['precision'] == 0: 6033491ea55f2866cc0b4fee036069fc07748920b0cfMark Dickinson if format_dict['type'] is None or format_dict['type'] in 'gG': 60341ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson format_dict['precision'] = 1 60351ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 6036277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # determine thousands separator, grouping, and decimal separator, and 6037277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # add appropriate entries to format_dict 6038277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if format_dict['type'] == 'n': 6039277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # apart from separators, 'n' behaves just like 'g' 6040277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['type'] = 'g' 6041277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if _localeconv is None: 6042277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson _localeconv = _locale.localeconv() 6043277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if format_dict['thousands_sep'] is not None: 6044277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson raise ValueError("Explicit thousands separator conflicts with " 6045277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson "'n' type in format specifier: " + format_spec) 6046277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['thousands_sep'] = _localeconv['thousands_sep'] 6047277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['grouping'] = _localeconv['grouping'] 6048277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['decimal_point'] = _localeconv['decimal_point'] 6049277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson else: 6050277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if format_dict['thousands_sep'] is None: 6051277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['thousands_sep'] = '' 6052277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['grouping'] = [3, 0] 6053277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format_dict['decimal_point'] = '.' 6054277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 60551ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # record whether return type should be str or unicode 605620c049df37da20383bdc36e0de6e58f4ebc53659Serhiy Storchaka try: 605720c049df37da20383bdc36e0de6e58f4ebc53659Serhiy Storchaka format_dict['unicode'] = isinstance(format_spec, unicode) 605820c049df37da20383bdc36e0de6e58f4ebc53659Serhiy Storchaka except NameError: 605920c049df37da20383bdc36e0de6e58f4ebc53659Serhiy Storchaka format_dict['unicode'] = False 60601ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 60611ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson return format_dict 60621ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 6063277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsondef _format_align(sign, body, spec): 6064277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """Given an unpadded, non-aligned numeric string 'body' and sign 606524b07bcba350bb86c4d6ca446e1564647a199868Ezio Melotti string 'sign', add padding and alignment conforming to the given 6066277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format specifier dictionary 'spec' (as produced by 6067277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson parse_format_specifier). 60681ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 6069277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson Also converts result to unicode if necessary. 60701ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 60711ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson """ 60721ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # how much extra space do we have to play with? 6073277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson minimumwidth = spec['minimumwidth'] 6074277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fill = spec['fill'] 6075277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson padding = fill*(minimumwidth - len(sign) - len(body)) 60761ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 6077277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson align = spec['align'] 60781ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson if align == '<': 60791ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson result = sign + body + padding 6080b065e52bc284c2c28a967c4e82aa7ece6d09c562Mark Dickinson elif align == '>': 6081b065e52bc284c2c28a967c4e82aa7ece6d09c562Mark Dickinson result = padding + sign + body 60821ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson elif align == '=': 60831ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson result = sign + padding + body 6084277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson elif align == '^': 60851ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson half = len(padding)//2 60861ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson result = padding[:half] + sign + body + padding[half:] 6087277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson else: 6088277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson raise ValueError('Unrecognised alignment field') 60891ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 60901ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson # make sure that result is unicode if necessary 6091277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if spec['unicode']: 60921ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson result = unicode(result) 60931ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson 60941ddf1d8482f4fd8ae034bfd0221696ee2068c144Mark Dickinson return result 60950d4c06e06e5ee1f3bb1fa8068114bd700d74864aNeal Norwitz 6096277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsondef _group_lengths(grouping): 6097277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """Convert a localeconv-style grouping into a (possibly infinite) 6098277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson iterable of integers representing group lengths. 6099277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6100277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """ 6101277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # The result from localeconv()['grouping'], and the input to this 6102277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # function, should be a list of integers in one of the 6103277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # following three forms: 6104277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # 6105277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # (1) an empty list, or 6106277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # (2) nonempty list of positive integers + [0] 6107277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # (3) list of positive integers + [locale.CHAR_MAX], or 6108277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6109277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson from itertools import chain, repeat 6110277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if not grouping: 6111277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return [] 6112277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson elif grouping[-1] == 0 and len(grouping) >= 2: 6113277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return chain(grouping[:-1], repeat(grouping[-2])) 6114277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson elif grouping[-1] == _locale.CHAR_MAX: 6115277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return grouping[:-1] 6116277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson else: 6117277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson raise ValueError('unrecognised format for grouping') 6118277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6119277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsondef _insert_thousands_sep(digits, spec, min_width=1): 6120277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """Insert thousands separators into a digit string. 6121277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6122277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson spec is a dictionary whose keys should include 'thousands_sep' and 6123277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 'grouping'; typically it's the result of parsing the format 6124277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson specifier using _parse_format_specifier. 6125277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6126277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson The min_width keyword argument gives the minimum length of the 6127277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson result, which will be padded on the left with zeros if necessary. 6128277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6129277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson If necessary, the zero padding adds an extra '0' on the left to 6130277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson avoid a leading thousands separator. For example, inserting 6131277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson commas every three digits in '123456', with min_width=8, gives 6132277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson '0,123,456', even though that has length 9. 6133277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6134277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """ 6135277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6136277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson sep = spec['thousands_sep'] 6137277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson grouping = spec['grouping'] 6138277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6139277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson groups = [] 6140277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson for l in _group_lengths(grouping): 6141277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if l <= 0: 6142277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson raise ValueError("group length should be positive") 6143277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson # max(..., 1) forces at least 1 digit to the left of a separator 6144277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson l = min(max(len(digits), min_width, 1), l) 6145277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson groups.append('0'*(l - len(digits)) + digits[-l:]) 6146277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson digits = digits[:-l] 6147277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson min_width -= l 6148277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if not digits and min_width <= 0: 6149277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson break 6150b14514a153857141631d602cadf094bc932f12eeMark Dickinson min_width -= len(sep) 6151277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson else: 6152277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson l = max(len(digits), min_width, 1) 6153277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson groups.append('0'*(l - len(digits)) + digits[-l:]) 6154277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return sep.join(reversed(groups)) 6155277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6156277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsondef _format_sign(is_negative, spec): 6157277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """Determine sign character.""" 6158277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6159277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if is_negative: 6160277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return '-' 6161277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson elif spec['sign'] in ' +': 6162277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return spec['sign'] 6163277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson else: 6164277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return '' 6165277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6166277859d5910782cc31bb27f2472893b8382ad391Mark Dickinsondef _format_number(is_negative, intpart, fracpart, exp, spec): 6167277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """Format a number, given the following data: 6168277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6169277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson is_negative: true if the number is negative, else false 6170277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson intpart: string of digits that must appear before the decimal point 6171277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fracpart: string of digits that must come after the point 6172277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson exp: exponent, as an integer 6173277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson spec: dictionary resulting from parsing the format specifier 6174277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6175277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson This function uses the information in spec to: 6176277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson insert separators (decimal separator and thousands separators) 6177277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format the sign 6178277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson format the exponent 6179277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson add trailing '%' for the '%' type 6180277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson zero-pad if necessary 6181277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fill and align if necessary 6182277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson """ 6183277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6184277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson sign = _format_sign(is_negative, spec) 6185277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6186277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if fracpart: 6187277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fracpart = spec['decimal_point'] + fracpart 6188277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6189277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if exp != 0 or spec['type'] in 'eE': 6190277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson echar = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e'}[spec['type']] 6191277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fracpart += "{0}{1:+}".format(echar, exp) 6192277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if spec['type'] == '%': 6193277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson fracpart += '%' 6194277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6195277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson if spec['zeropad']: 6196277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson min_width = spec['minimumwidth'] - len(fracpart) - len(sign) 6197277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson else: 6198277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson min_width = 0 6199277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson intpart = _insert_thousands_sep(intpart, spec, min_width) 6200277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6201277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson return _format_align(sign, intpart+fracpart, spec) 6202277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 6203277859d5910782cc31bb27f2472893b8382ad391Mark Dickinson 620472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista##### Useful Constants (internal use only) ################################ 62057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 620672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# Reusable defaults 6207b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger_Infinity = Decimal('Inf') 6208b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger_NegativeInfinity = Decimal('-Inf') 6209c5de0969ca2a82c66efd30bb675f03c2324a3b27Mark Dickinson_NaN = Decimal('NaN') 6210b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger_Zero = Decimal(0) 6211b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger_One = Decimal(1) 6212b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger_NegativeOne = Decimal(-1) 6213c5de0969ca2a82c66efd30bb675f03c2324a3b27Mark Dickinson 6214b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger# _SignedInfinity[sign] is infinity w/ that sign 6215b7e835b820fc7d17a02dc4cdf3b745bb993952adRaymond Hettinger_SignedInfinity = (_Infinity, _NegativeInfinity) 62167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 62177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 62187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 62197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerif __name__ == '__main__': 62207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger import doctest, sys 62217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger doctest.testmod(sys.modules[__name__]) 6222