decimal.py revision 0f5e7bf3049408d6f4c1855807204c9f13ae98f9
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 247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger www2.hursley.ibm.com/decimal/decarith.html 257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 260ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettingerand IEEE standard 854-1987: 277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html 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 387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond 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) 457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDecimal("0") 467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal("1") 477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDecimal("1") 487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal("-.0123") 497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDecimal("-0.0123") 507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal(123456) 517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDecimal("123456") 527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal("123.45e12345678901234567890") 537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDecimal("1.2345E+12345678901234567892") 547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal("1.33") + Decimal("1.27") 557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDecimal("2.60") 567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger>>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41") 577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond 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)) 947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond 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 137eb2608415ee4eb8aaea746f6a313937e264d0cdaRaymond Hettingerimport copy as _copy 1387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 13959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista# Rounding 1400ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_DOWN = 'ROUND_DOWN' 1410ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_HALF_UP = 'ROUND_HALF_UP' 1420ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_HALF_EVEN = 'ROUND_HALF_EVEN' 1430ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_CEILING = 'ROUND_CEILING' 1440ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_FLOOR = 'ROUND_FLOOR' 1450ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_UP = 'ROUND_UP' 1460ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond HettingerROUND_HALF_DOWN = 'ROUND_HALF_DOWN' 147353750c405c9099d0be69c6af1d17037b38c4ddfFacundo BatistaROUND_05UP = 'ROUND_05UP' 1487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista# Errors 1507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DecimalException(ArithmeticError): 1525aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger """Base exception class. 1537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Used exceptions derive from this. 1557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If an exception derives from another exception besides this (such as 1567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Underflow (Inexact, Rounded, Subnormal) that indicates that it is only 1577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger called if the others are present. This isn't actually used for 1587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger anything, though. 1597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 160cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis handle -- Called when context._raise_error is called and the 161cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis trap_enabler is set. First argument is self, second is the 162cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis context. More arguments can be given, those being after 163cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis the explanation in _raise_error (For example, 164cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis context._raise_error(NewError, '(-x)!', self._sign) would 165cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis call NewError().handle(context, self._sign).) 166cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 1677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger To define a new exception, it should be sufficient to have it derive 1687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger from DecimalException. 1697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 1717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger pass 1727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Clamped(DecimalException): 1757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Exponent of a 0 changed to fit bounds. 1767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals clamped if the exponent of a result has been 1787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger altered in order to fit the constraints of a specific concrete 17959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista representation. This may occur when the exponent of a zero result would 18059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista be outside the bounds of a representation, or when a large normal 18159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista number would have an encoded exponent that cannot be represented. In 1827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger this latter case, the exponent is reduced to fit and the corresponding 1837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger number of zero digits are appended to the coefficient ("fold-down"). 1847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass InvalidOperation(DecimalException): 1877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """An invalid operation was performed. 1887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Various bad things cause this: 1907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Something creates a signaling NaN 1927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -INF + INF 19359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista 0 * (+-)INF 19459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista (+-)INF / (+-)INF 1957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x % 0 1967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (+-)INF % x 1977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x._rescale( non-integer ) 1987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sqrt(-x) , x > 0 1997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 ** 0 2007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x ** (non-integer) 2017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger x ** (+-)INF 2027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger An operand is invalid 203353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 204353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result of the operation after these is a quiet positive NaN, 205353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista except when the cause is a signaling NaN, in which case the result is 206353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista also a quiet NaN, but with the original sign, and an optional 207353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista diagnostic information. 2087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 2107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if args: 2110f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True) 2120f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista return ans._fix_nan(context) 2137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return NaN 2147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass ConversionSyntax(InvalidOperation): 2167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Trying to convert badly formed string. 2177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals invalid-operation if an string is being 2197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger converted to a number and it does not conform to the numeric string 22059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista syntax. The result is [0,qNaN]. 2217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 222cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis def handle(self, context, *args): 223353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return NaN 2247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DivisionByZero(DecimalException, ZeroDivisionError): 2267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Division by 0. 2277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals division-by-zero if division of a finite number 2297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger by zero was attempted (during a divide-integer or divide operation, or a 2307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger power operation with negative right-hand operand), and the dividend was 2317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger not zero. 2327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result of the operation is [sign,inf], where sign is the exclusive 2347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger or of the signs of the operands for divide, or is 1 for an odd power of 2357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -0, for power. 2367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 237cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 238cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista def handle(self, context, sign, *args): 2397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return Infsign[sign] 2407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DivisionImpossible(InvalidOperation): 2427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Cannot perform the division adequately. 2437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals invalid-operation if the integer result of a 2457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger divide-integer or remainder operation had too many digits (would be 24659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista longer than precision). The result is [0,qNaN]. 2477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 248cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 2497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 250cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return NaN 2517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass DivisionUndefined(InvalidOperation, ZeroDivisionError): 2537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Undefined result of division. 2547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals invalid-operation if division by zero was 2567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger attempted (during a divide-integer, divide, or remainder operation), and 25759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista the dividend is also zero. The result is [0,qNaN]. 2587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 259cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 260cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista def handle(self, context, *args): 2617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return NaN 2627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Inexact(DecimalException): 2647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Had to round, losing information. 2657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals inexact whenever the result of an operation is 2677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger not exact (that is, it needed to be rounded and any discarded digits 26859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista were non-zero), or if an overflow or underflow condition occurs. The 2697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result in all cases is unchanged. 2707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The inexact signal may be tested (or trapped) to determine if a given 2727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation (or sequence of operations) was inexact. 2737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass InvalidContext(InvalidOperation): 2767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Invalid context. Unknown rounding, for example. 2777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals invalid-operation if an invalid context was 27959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista detected during an operation. This can occur if contexts are not checked 2807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger on creation and either the precision exceeds the capability of the 2817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger underlying concrete representation or an unknown or unsupported rounding 28259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista was specified. These aspects of the context need only be checked when 28359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista the values are required to be used. The result is [0,qNaN]. 2847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 285cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 2867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, *args): 2877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return NaN 2887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Rounded(DecimalException): 2907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Number got rounded (not necessarily changed during rounding). 2917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals rounded whenever the result of an operation is 2937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounded (that is, some zero or non-zero digits were discarded from the 29459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista coefficient), or if an overflow or underflow condition occurs. The 2957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result in all cases is unchanged. 2967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The rounded signal may be tested (or trapped) to determine if a given 2987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation (or sequence of operations) caused a loss of precision. 2997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 3007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Subnormal(DecimalException): 3027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Exponent < Emin before rounding. 3037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals subnormal whenever the result of a conversion or 3057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation is subnormal (that is, its adjusted exponent is less than 30659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista Emin, before any rounding). The result in all cases is unchanged. 3077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The subnormal signal may be tested (or trapped) to determine if a given 3097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger or operation (or sequence of operations) yielded a subnormal result. 3107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 3117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Overflow(Inexact, Rounded): 3137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Numerical overflow. 3147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals overflow if the adjusted exponent of a result 3167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (from a conversion or from an operation that is not an attempt to divide 3177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger by zero), after rounding, would be greater than the largest value that 3187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger can be handled by the implementation (the value Emax). 3197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result depends on the rounding mode: 3217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger For round-half-up and round-half-even (and for round-half-down and 3237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger round-up, if implemented), the result of the operation is [sign,inf], 32459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista where sign is the sign of the intermediate result. For round-down, the 3257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result is the largest finite number that can be represented in the 32659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista current precision, with the sign of the intermediate result. For 3277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger round-ceiling, the result is the same as for round-down if the sign of 32859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista the intermediate result is 1, or is [0,inf] otherwise. For round-floor, 3297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the result is the same as for round-down if the sign of the intermediate 33059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded 3317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger will also be raised. 3320f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista """ 333cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 3347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def handle(self, context, sign, *args): 3357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN, 336353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ROUND_HALF_DOWN, ROUND_UP): 3377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return Infsign[sign] 3387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if sign == 0: 3397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding == ROUND_CEILING: 3407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return Infsign[sign] 34172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, '9'*context.prec, 34272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context.Emax-context.prec+1) 3437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if sign == 1: 3447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding == ROUND_FLOOR: 3457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return Infsign[sign] 34672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, '9'*context.prec, 34772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context.Emax-context.prec+1) 3487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Underflow(Inexact, Rounded, Subnormal): 3517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Numerical underflow with result rounded to 0. 3527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This occurs and signals underflow if a result is inexact and the 3547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger adjusted exponent of the result would be smaller (more negative) than 3557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the smallest value that can be handled by the implementation (the value 35659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista Emin). That is, the result is both inexact and subnormal. 3577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result after an underflow will be a subnormal number rounded, if 35959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista necessary, so that its exponent is not less than Etiny. This may result 3607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger in 0 with the sign of the intermediate result and an exponent of Etiny. 3617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger In all cases, Inexact, Rounded, and Subnormal will also be raised. 3637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 3647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3655aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger# List of public traps and flags 366fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger_signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded, 367cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis Underflow, InvalidOperation, Subnormal] 3687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3695aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger# Map conditions (per the spec) to signals 3705aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger_condition_map = {ConversionSyntax:InvalidOperation, 3715aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger DivisionImpossible:InvalidOperation, 3725aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger DivisionUndefined:InvalidOperation, 3735aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger InvalidContext:InvalidOperation} 3747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 37559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Context Functions ################################################## 3767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 377ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger# The getcontext() and setcontext() function manage access to a thread-local 378ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger# current context. Py2.4 offers direct support for thread locals. If that 379ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger# is not available, use threading.currentThread() which is slower but will 3807e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger# work for older Pythons. If threads are not part of the build, create a 381cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis# mock threading object with threading.local() returning the module namespace. 3827e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger 3837e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettingertry: 3847e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger import threading 3857e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettingerexcept ImportError: 3867e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger # Python was compiled without threads; create a mock object instead 3877e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger import sys 38859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista class MockThreading(object): 3897e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger def local(self, sys=sys): 3907e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger return sys.modules[__name__] 3917e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger threading = MockThreading() 3927e71fa5cfa250968eafdb77356621e3a9bbb0648Raymond Hettinger del sys, MockThreading 393ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 394ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettingertry: 395ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger threading.local 396ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 397ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettingerexcept AttributeError: 398ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 39959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # To fix reloading, force it to create a new context 40059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Old contexts have different exceptions in their dicts, making problems. 401ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger if hasattr(threading.currentThread(), '__decimal_context__'): 402ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger del threading.currentThread().__decimal_context__ 403ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 404ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def setcontext(context): 405ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Set this thread's context to context.""" 406ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger if context in (DefaultContext, BasicContext, ExtendedContext): 4079fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger context = context.copy() 40861992efc4bb413ae7a19752215eca5af09be6b6dRaymond Hettinger context.clear_flags() 4097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger threading.currentThread().__decimal_context__ = context 410ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 411ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def getcontext(): 412ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Returns this thread's context. 413ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 414ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger If this thread does not yet have a context, returns 415ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger a new context and sets this thread's context. 416ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger New contexts are copies of DefaultContext. 417ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """ 418ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger try: 419ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return threading.currentThread().__decimal_context__ 420ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger except AttributeError: 421ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger context = Context() 422ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger threading.currentThread().__decimal_context__ = context 423ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return context 424ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 425ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettingerelse: 426ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 427ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger local = threading.local() 4289fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger if hasattr(local, '__decimal_context__'): 4299fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger del local.__decimal_context__ 430ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 431ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def getcontext(_local=local): 432ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Returns this thread's context. 433ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 434ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger If this thread does not yet have a context, returns 435ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger a new context and sets this thread's context. 436ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger New contexts are copies of DefaultContext. 437ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """ 438ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger try: 439ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return _local.__decimal_context__ 440ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger except AttributeError: 441ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger context = Context() 442ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger _local.__decimal_context__ = context 443ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger return context 444ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 445ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger def setcontext(context, _local=local): 446ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger """Set this thread's context to context.""" 447ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger if context in (DefaultContext, BasicContext, ExtendedContext): 4489fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger context = context.copy() 44961992efc4bb413ae7a19752215eca5af09be6b6dRaymond Hettinger context.clear_flags() 450ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger _local.__decimal_context__ = context 451ef66debd7ef590304466d13dde4082632750fa7cRaymond Hettinger 452cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis del threading, local # Don't contaminate the namespace 4537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4548b6999b4c5fab174090be263ba90f193bdede141Nick Coghlandef localcontext(ctx=None): 4558b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """Return a context manager for a copy of the supplied context 4568b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 4578b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan Uses a copy of the current context if no context is specified 4588b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan The returned context manager creates a local decimal context 4598b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan in a with statement: 4608b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan def sin(x): 4618b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan with localcontext() as ctx: 4628b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ctx.prec += 2 4638b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # Rest of sin calculation algorithm 4648b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # uses a precision 2 greater than normal 46559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista return +s # Convert result to normal precision 4668b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 4678b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan def sin(x): 4688b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan with localcontext(ExtendedContext): 4698b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # Rest of sin calculation algorithm 4708b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # uses the Extended Context from the 4718b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # General Decimal Arithmetic Specification 47259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista return +s # Convert result to normal context 4738b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 4748b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """ 475681d86743c21773bb00508dbacb05ed9012388d8Neal Norwitz # The string below can't be included in the docstring until Python 2.6 4768b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan # as the doctest module doesn't understand __future__ statements 4778b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """ 4788b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> from __future__ import with_statement 4798b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> print getcontext().prec 4808b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 28 4818b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> with localcontext(): 4828b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... ctx = getcontext() 483495df4716fce4156c1eb110f9342297164eecbb6Raymond Hettinger ... ctx.prec += 2 4848b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... print ctx.prec 4858b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... 4868b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 30 4878b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> with localcontext(ExtendedContext): 4888b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... print getcontext().prec 4898b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan ... 4908b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 9 4918b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan >>> print getcontext().prec 4928b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 28 4938b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """ 494ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan if ctx is None: ctx = getcontext() 495ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan return _ContextManager(ctx) 4968b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan 4977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 49859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Decimal class ####################################################### 4997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Decimal(object): 5017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Floating point class for decimal arithmetic.""" 5027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 503636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger __slots__ = ('_exp','_int','_sign', '_is_special') 504636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # Generally, the value of the Decimal instance is given by 505636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # (-1)**_sign * _int * 10**_exp 506636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # Special values are signified by _is_special == True 5077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 508dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger # We're immutable, so use __new__ not __init__ 509636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger def __new__(cls, value="0", context=None): 5107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Create a decimal point instance. 5117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger >>> Decimal('3.14') # string input 5137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3.14") 51459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) 5157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3.14") 5167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger >>> Decimal(314) # int or long 5177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("314") 5187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger >>> Decimal(Decimal(314)) # another decimal instance 5197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("314") 5207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 5217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 52272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # Note that the coefficient, self._int, is actually stored as 52372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # a string rather than as a tuple of digits. This speeds up 52472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # the "digits to integer" and "integer to digits" conversions 52572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # that are used in almost every arithmetic operation on 52672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # Decimals. This is an internal detail: the as_tuple function 52772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # and the Decimal constructor still deal with tuples of 52872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista # digits. 52972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 530636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self = object.__new__(cls) 531636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 5320d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # From a string 5330d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # REs insist on real strings, so we can too. 5340d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if isinstance(value, basestring): 5350d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista m = _parser(value) 5360d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if m is None: 5370d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if context is None: 5380d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista context = getcontext() 5390d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista return context._raise_error(ConversionSyntax, 5400d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista "Invalid literal for Decimal: %r" % value) 541636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 5420d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if m.group('sign') == "-": 5430d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = 1 5440d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5450d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = 0 5460d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista intpart = m.group('int') 5470d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if intpart is not None: 5480d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # finite number 5490d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista fracpart = m.group('frac') 5500d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista exp = int(m.group('exp') or '0') 5510d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if fracpart is not None: 5520d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = (intpart+fracpart).lstrip('0') or '0' 5530d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = exp - len(fracpart) 5540d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5550d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = intpart.lstrip('0') or '0' 5560d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = exp 5570d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = False 5580d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5590d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista diag = m.group('diag') 5600d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if diag is not None: 5610d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # NaN 5620d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = diag.lstrip('0') 5630d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if m.group('signal'): 5640d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = 'N' 5650d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5660d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = 'n' 5670d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista else: 5680d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # infinity 5690d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = '0' 5700d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = 'F' 5710d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = True 572636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self 573636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 574636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # From an integer 5757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if isinstance(value, (int,long)): 576636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if value >= 0: 577636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._sign = 0 578636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger else: 579636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._sign = 1 580636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._exp = 0 58172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = str(abs(value)) 5820d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = False 5830d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista return self 5840d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista 5850d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # From another decimal 5860d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if isinstance(value, Decimal): 5870d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = value._exp 5880d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = value._sign 5890d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = value._int 5900d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = value._is_special 5910d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista return self 5920d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista 5930d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista # From an internal working value 5940d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista if isinstance(value, _WorkRep): 5950d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._sign = value.sign 5960d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._int = str(value.int) 5970d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._exp = int(value.exp) 5980d157a0154140dc6dcd491d62df3a2b66367be15Facundo Batista self._is_special = False 599636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self 600636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 601636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # tuple/list conversion (possibly from as_tuple()) 602636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(value, (list,tuple)): 603636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if len(value) != 3: 6049b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError('Invalid tuple size in creation of Decimal ' 6059b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista 'from list or tuple. The list or tuple ' 6069b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista 'should have exactly three elements.') 6079b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # process sign. The isinstance test rejects floats 6089b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if not (isinstance(value[0], (int, long)) and value[0] in (0,1)): 6099b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError("Invalid sign. The first value in the tuple " 6109b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "should be an integer; either 0 for a " 6119b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "positive number or 1 for a negative number.") 612636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._sign = value[0] 6139b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if value[2] == 'F': 6149b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # infinity: value[1] is ignored 61572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = '0' 616636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._exp = value[2] 617636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self._is_special = True 618636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger else: 6199b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # process and validate the digits in value[1] 6209b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista digits = [] 6219b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista for digit in value[1]: 6229b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if isinstance(digit, (int, long)) and 0 <= digit <= 9: 6239b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # skip leading zeros 6249b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if digits or digit != 0: 6259b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista digits.append(digit) 6269b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista else: 6279b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError("The second value in the tuple must " 6289b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "be composed of integers in the range " 6299b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "0 through 9.") 6309b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista if value[2] in ('n', 'N'): 6319b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # NaN: digits form the diagnostic 63272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = ''.join(map(str, digits)) 6339b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._exp = value[2] 6349b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._is_special = True 6359b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista elif isinstance(value[2], (int, long)): 6369b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista # finite number: digits give the coefficient 63772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = ''.join(map(str, digits or [0])) 6389b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._exp = value[2] 6399b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista self._is_special = False 6409b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista else: 6419b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista raise ValueError("The third value in the tuple must " 6429b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "be an integer, or one of the " 6439b5e23148badbb0d11fffd05cf9d432f35631a4aFacundo Batista "strings 'F', 'n', 'N'.") 644636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self 645636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 646636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(value, float): 647636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger raise TypeError("Cannot convert float to Decimal. " + 648636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger "First convert the float to a string") 6497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 650636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger raise TypeError("Cannot convert %r to Decimal" % value) 6517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 6527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _isnan(self): 6537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether the number is not actually one. 6547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 6557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 if a number 6560f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista 1 if NaN 6577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2 if sNaN 6587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 659636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 660636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger exp = self._exp 661636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if exp == 'n': 662636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return 1 663636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger elif exp == 'N': 664636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return 2 6657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 6667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 6677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _isinfinity(self): 6687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether the number is infinite 6697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 6707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 if finite or not a number 6717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1 if +INF 6727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -1 if -INF 6737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 6747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp == 'F': 6757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign: 6767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return -1 6777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 1 6787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 6797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 680353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _check_nans(self, other=None, context=None): 6817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether the number is not actually one. 6827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 6837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self, other are sNaN, signal 6847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self, other are NaN return nan 6857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 6867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 6877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Done before operations. 6887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 6897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 690636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self_is_nan = self._isnan() 691636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other is None: 692636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other_is_nan = False 693636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger else: 694636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other_is_nan = other._isnan() 695636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 696636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self_is_nan or other_is_nan: 697636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 698636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 699636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 700636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self_is_nan == 2: 701636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, 'sNaN', 7020f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista self) 703636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other_is_nan == 2: 704636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, 'sNaN', 7050f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista other) 706636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self_is_nan: 707353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._fix_nan(context) 708636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 709353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other._fix_nan(context) 7107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 7117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __nonzero__(self): 7131a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is nonzero; otherwise return False. 7147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7151a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista NaNs and infinities are considered nonzero. 7167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 71772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self._is_special or self._int != '0' 7187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 719353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def __cmp__(self, other): 720636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 721267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 722353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Never return NotImplemented 723353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return 1 7247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 725636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 726353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # check for nans, without raising on a signaling nan 727353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isnan() or other._isnan(): 72859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista return 1 # Comparison involving NaN's always reports self > other 729636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 730636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # INF = INF 731636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return cmp(self._isinfinity(), other._isinfinity()) 7327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 733353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # check for zeros; note that cmp(0, -0) should return 0 734353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 735353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 736353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return 0 737353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 738353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return -((-1)**other._sign) 739353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 740353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return (-1)**self._sign 7417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 74259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If different signs, neg one is less 7437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if other._sign < self._sign: 7447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return -1 7457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign < other._sign: 7467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 1 7477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 748636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self_adjusted = self.adjusted() 749636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other_adjusted = other.adjusted() 750353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_adjusted == other_adjusted: 75172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self_padded = self._int + '0'*(self._exp - other._exp) 75272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista other_padded = other._int + '0'*(other._exp - self._exp) 753353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return cmp(self_padded, other_padded) * (-1)**self._sign 754353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self_adjusted > other_adjusted: 7557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return (-1)**self._sign 756353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: # self_adjusted < other_adjusted 7577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return -((-1)**self._sign) 7587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7590aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger def __eq__(self, other): 7600aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger if not isinstance(other, (Decimal, int, long)): 761267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return NotImplemented 7620aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger return self.__cmp__(other) == 0 7630aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger 7640aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger def __ne__(self, other): 7650aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger if not isinstance(other, (Decimal, int, long)): 766267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return NotImplemented 7670aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger return self.__cmp__(other) != 0 7680aeac107cadd1472e5ea109f7f29e6a6527ae1ecRaymond Hettinger 7697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def compare(self, other, context=None): 7707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Compares one to another. 7717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -1 => a < b 7737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 0 => a = b 7747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1 => a > b 7757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger NaN => one is NaN 7767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Like __cmp__, but returns Decimal instances. 7777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 7797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 78059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Compare(NaN, NaN) = NaN 781636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if (self._is_special or other and other._is_special): 782636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 783636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 784636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 7857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 786353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(self.__cmp__(other)) 7877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 7887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __hash__(self): 7897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """x.__hash__() <==> hash(x)""" 7907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Decimal integers must hash the same as the ints 7917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Non-integer decimals are normalized and hashed as strings 7921fb9f528bd69efa135bacda917ec7bb166068d5dGeorg Brandl # Normalization assures that hash(100E-1) == hash(10) 793bea3f6f5c788eb4f0f7639913ff89654152864c0Raymond Hettinger if self._is_special: 794bea3f6f5c788eb4f0f7639913ff89654152864c0Raymond Hettinger if self._isnan(): 795bea3f6f5c788eb4f0f7639913ff89654152864c0Raymond Hettinger raise TypeError('Cannot hash a NaN value.') 796bea3f6f5c788eb4f0f7639913ff89654152864c0Raymond Hettinger return hash(str(self)) 7978c202440699cef19602acc24822366d0d7c32083Facundo Batista if not self: 7988c202440699cef19602acc24822366d0d7c32083Facundo Batista return 0 7998c202440699cef19602acc24822366d0d7c32083Facundo Batista if self._isinteger(): 8008c202440699cef19602acc24822366d0d7c32083Facundo Batista op = _WorkRep(self.to_integral_value()) 8018c202440699cef19602acc24822366d0d7c32083Facundo Batista # to make computation feasible for Decimals with large 8028c202440699cef19602acc24822366d0d7c32083Facundo Batista # exponent, we use the fact that hash(n) == hash(m) for 8038c202440699cef19602acc24822366d0d7c32083Facundo Batista # any two nonzero integers n and m such that (i) n and m 8048c202440699cef19602acc24822366d0d7c32083Facundo Batista # have the same sign, and (ii) n is congruent to m modulo 8058c202440699cef19602acc24822366d0d7c32083Facundo Batista # 2**64-1. So we can replace hash((-1)**s*c*10**e) with 8068c202440699cef19602acc24822366d0d7c32083Facundo Batista # hash((-1)**s*c*pow(10, e, 2**64-1). 8078c202440699cef19602acc24822366d0d7c32083Facundo Batista return hash((-1)**op.sign*op.int*pow(10, op.exp, 2**64-1)) 8087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return hash(str(self.normalize())) 8097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def as_tuple(self): 8117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Represents the number as a triple tuple. 8127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger To show the internals exactly as they are. 8147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 81572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return (self._sign, tuple(map(int, self._int)), self._exp) 8167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __repr__(self): 8187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Represents the number as an instance of Decimal.""" 8197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Invariant: eval(repr(d)) == d 8207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 'Decimal("%s")' % str(self) 8217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def __str__(self, eng=False, context=None): 8237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return string representation of the number in scientific notation. 8247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Captures all of the information in the underlying representation. 8267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 8277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 82862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista sign = ['', '-'][self._sign] 829e5a0a9609faea9afdc6f81f1f6dfa07b1437e3aeRaymond Hettinger if self._is_special: 83062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if self._exp == 'F': 83162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + 'Infinity' 83262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif self._exp == 'n': 83362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + 'NaN' + self._int 83462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista else: # self._exp == 'N' 83562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + 'sNaN' + self._int 83662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 83762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # number of digits of self._int to left of decimal point 83862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista leftdigits = self._exp + len(self._int) 83962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 84062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # dotplace is number of digits of self._int to the left of the 84162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # decimal point in the mantissa of the output string (that is, 84262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # after adjusting the exponent) 84362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if self._exp <= 0 and leftdigits > -6: 84462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # no exponent required 84562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista dotplace = leftdigits 84662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif not eng: 84762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # usual scientific notation: 1 digit on left of the point 8487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger dotplace = 1 84962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif self._int == '0': 85062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # engineering notation, zero 85162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista dotplace = (leftdigits + 1) % 3 - 1 8527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 85362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista # engineering notation, nonzero 85462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista dotplace = (leftdigits - 1) % 3 + 1 85562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 85662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if dotplace <= 0: 85762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista intpart = '0' 85862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista fracpart = '.' + '0'*(-dotplace) + self._int 85962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista elif dotplace >= len(self._int): 86062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista intpart = self._int+'0'*(dotplace-len(self._int)) 86162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista fracpart = '' 86262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista else: 86362edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista intpart = self._int[:dotplace] 86462edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista fracpart = '.' + self._int[dotplace:] 86562edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if leftdigits == dotplace: 86662edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista exp = '' 86762edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista else: 86862edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista if context is None: 86962edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista context = getcontext() 87062edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace) 87162edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista 87262edb71556d26f924a0e2e949af9cd3c211dea23Facundo Batista return sign + intpart + fracpart + exp 8737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def to_eng_string(self, context=None): 8757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Convert to engineering-type string. 8767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Engineering notation has an exponent which is a multiple of 3, so there 8787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger are up to 3 digits left of the decimal place. 8797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Same rules for when in exponential and when as a value as in __str__. 8817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 882353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self.__str__(eng=True, context=context) 8837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __neg__(self, context=None): 8857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns a copy with the sign switched. 8867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Rounds, if it has reason. 8887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 889636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 890636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 891636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 892636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 8937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 8947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 8957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # -Decimal('0') is Decimal('0'), not Decimal('-0') 8960f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista ans = self.copy_abs() 8977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 898353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self.copy_negate() 899636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 900636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 901636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 902e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 9037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __pos__(self, context=None): 9057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns a copy, unless it is a sNaN. 9067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Rounds the number (if more then precision digits) 9087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 909636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 910636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 911636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 912636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 9137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 9157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # + (-0) = 0 9160f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista ans = self.copy_abs() 917353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 918353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = Decimal(self) 9197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 920636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 921636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 922e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 9237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 924e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista def __abs__(self, round=True, context=None): 9257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the absolute value of self. 9267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 927e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista If the keyword argument 'round' is false, do not round. The 928e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista expression self.__abs__(round=False) is equivalent to 929e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.copy_abs(). 9307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 931e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista if not round: 932e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return self.copy_abs() 933e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista 934636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 935636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 936636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 937636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 9387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign: 9407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = self.__neg__(context=context) 9417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 9427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = self.__pos__(context=context) 9437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 9457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __add__(self, other, context=None): 9477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns self + other. 9487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger -INF + INF (or the reverse) cause InvalidOperation errors. 9507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 951636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 952267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 953267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 954636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 9557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context is None: 9567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = getcontext() 9577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 958636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 959636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 960636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 961636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 9627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 963636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity(): 96459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If both INF, same sign => same as both, opposite => error. 965636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._sign != other._sign and other._isinfinity(): 966636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, '-INF + INF') 967636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Decimal(self) 968636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other._isinfinity(): 96959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista return Decimal(other) # Can't both be infinity here 9707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp = min(self._exp, other._exp) 9727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger negativezero = 0 9737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context.rounding == ROUND_FLOOR and self._sign != other._sign: 97459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If the answer is 0, the sign should be negative, in this case. 9757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger negativezero = 1 9767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self and not other: 9787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sign = min(self._sign, other._sign) 9797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if negativezero: 9807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sign = 1 98172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(sign, '0', exp) 982e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 9847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 98599b5548298ffc2ae5f03bd9ef873ce45a9844f0eFacundo Batista exp = max(exp, other._exp - context.prec-1) 986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = other._rescale(exp, context.rounding) 987e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 9887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 9897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not other: 99099b5548298ffc2ae5f03bd9ef873ce45a9844f0eFacundo Batista exp = max(exp, self._exp - context.prec-1) 991353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(exp, context.rounding) 992e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 9937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 9947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op1 = _WorkRep(self) 9967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op2 = _WorkRep(other) 997e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista op1, op2 = _normalize(op1, op2, context.prec) 9987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 9997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result = _WorkRep() 10007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if op1.sign != op2.sign: 10017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Equal and opposite 100217931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op1.int == op2.int: 100372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(negativezero, '0', exp) 1004e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 1005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 100617931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op1.int < op2.int: 10077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op1, op2 = op2, op1 100859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # OK, now abs(op1) > abs(op2) 100917931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op1.sign == 1: 101017931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger result.sign = 1 10117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger op1.sign, op2.sign = op2.sign, op1.sign 10127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 101317931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger result.sign = 0 101459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # So we know the sign, and op1 > 0. 101517931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger elif op1.sign == 1: 10167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result.sign = 1 101717931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger op1.sign, op2.sign = (0, 0) 101817931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger else: 101917931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger result.sign = 0 102059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Now, op1 > abs(op2) > 0 10217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 102217931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger if op2.sign == 0: 1023636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger result.int = op1.int + op2.int 10247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 1025636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger result.int = op1.int - op2.int 10267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result.exp = op1.exp 10287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = Decimal(result) 1029e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 10307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 10317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __radd__ = __add__ 10337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __sub__(self, other, context=None): 1035353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return self - other""" 1036636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1037267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1038267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 10397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1040636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 1041636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context=context) 1042636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1043636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 10447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1045353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self - other is computed as self + other.copy_negate() 1046353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self.__add__(other.copy_negate(), context=context) 10477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rsub__(self, other, context=None): 1049353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return other - self""" 1050636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1051267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1052267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 10537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1054353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other.__sub__(self, context=context) 10557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __mul__(self, other, context=None): 10577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return self * other. 10587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (+-) INF * 0 (or its reverse) raise InvalidOperation. 10607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1061636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1062267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1063267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 1064636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 10657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context is None: 10667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = getcontext() 10677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1068d87ac8f24da5dce860b559b69e6af59edd2c3eecRaymond Hettinger resultsign = self._sign ^ other._sign 10697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1070636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 1071636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 1072636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 1073636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 1074636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1075636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity(): 1076636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if not other: 1077636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, '(+-)INF * 0') 1078636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Infsign[resultsign] 1079636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1080636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other._isinfinity(): 1081636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if not self: 1082636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, '0 * (+-)INF') 1083636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Infsign[resultsign] 10847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger resultexp = self._exp + other._exp 10867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Special case for multiplying by zero 10887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self or not other: 108972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, '0', resultexp) 1090e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista # Fixing in case the exponent is out of bounds 1091e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 10927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 10937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 10947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Special case for multiplying by power of 10 109572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if self._int == '1': 109672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, other._int, resultexp) 1097e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 10987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 109972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if other._int == '1': 110072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, self._int, resultexp) 1101e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 11027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 11037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1104636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger op1 = _WorkRep(self) 1105636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger op2 = _WorkRep(other) 1106636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 110772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(resultsign, str(op1.int * op2.int), resultexp) 1108e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista ans = ans._fix(context) 11097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 11117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __rmul__ = __mul__ 11127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __div__(self, other, context=None): 11147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return self / other.""" 1115636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1116267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1117cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return NotImplemented 1118636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 11197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if context is None: 11207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = getcontext() 11217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1122636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger sign = self._sign ^ other._sign 1123636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1124636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 1125636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(other, context) 1126636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 11277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 11287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1129636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity() and other._isinfinity(): 11307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF') 11317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 11327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._isinfinity(): 1133636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Infsign[sign] 1134636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1135636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if other._isinfinity(): 1136636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context._raise_error(Clamped, 'Division by infinity') 113772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, '0', context.Etiny()) 1138636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1139636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # Special cases for zeroes 1140636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if not other: 1141cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self: 1142cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionUndefined, '0 / 0') 1143636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(DivisionByZero, 'x / 0', sign) 11447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1145cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self: 1146cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista exp = self._exp - other._exp 1147cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff = 0 1148cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1149cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # OK, so neither = 0, INF or NaN 1150cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista shift = len(other._int) - len(self._int) + context.prec + 1 1151cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista exp = self._exp - other._exp - shift 1152cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op1 = _WorkRep(self) 1153cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op2 = _WorkRep(other) 1154cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if shift >= 0: 1155cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff, remainder = divmod(op1.int * 10**shift, op2.int) 1156cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1157cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff, remainder = divmod(op1.int, op2.int * 10**-shift) 1158cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if remainder: 1159cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # result is not exact; adjust to ensure correct rounding 1160cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if coeff % 5 == 0: 1161cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff += 1 1162cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1163cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # result is exact; get as close to ideal exponent as possible 1164cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ideal_exp = self._exp - other._exp 1165cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista while exp < ideal_exp and coeff % 10 == 0: 1166cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista coeff //= 10 1167cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista exp += 1 11687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 116972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(sign, str(coeff), exp) 1170cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans._fix(context) 11717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1172cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista __truediv__ = __div__ 11737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1174cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista def _divide(self, other, context): 1175cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista """Return (self // other, self % other), to context.prec precision. 11767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1177cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista Assumes that neither self nor other is a NaN, that self is not 1178cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista infinite and that other is nonzero. 1179cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista """ 1180cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista sign = self._sign ^ other._sign 1181cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other._isinfinity(): 1182cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ideal_exp = self._exp 1183cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1184cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ideal_exp = min(self._exp, other._exp) 11857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1186cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista expdiff = self.adjusted() - other.adjusted() 1187cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self or other._isinfinity() or expdiff <= -2: 118872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return (_dec_from_triple(sign, '0', 0), 1189cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista self._rescale(ideal_exp, context.rounding)) 1190cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if expdiff <= context.prec: 1191cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op1 = _WorkRep(self) 1192cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op2 = _WorkRep(other) 1193cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if op1.exp >= op2.exp: 1194cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op1.int *= 10**(op1.exp - op2.exp) 1195cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1196cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista op2.int *= 10**(op2.exp - op1.exp) 1197cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista q, r = divmod(op1.int, op2.int) 1198cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if q < 10**context.prec: 119972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return (_dec_from_triple(sign, str(q), 0), 120072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista _dec_from_triple(self._sign, str(r), ideal_exp)) 12017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1202cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista # Here the quotient is too large to be representable 1203cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = context._raise_error(DivisionImpossible, 1204cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 'quotient too large in //, % or divmod') 1205cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans, ans 12067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rdiv__(self, other, context=None): 12087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Swaps self/other and returns __div__.""" 1209636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1210267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1211267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 12127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return other.__div__(self, context=context) 12137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __rtruediv__ = __rdiv__ 12147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __divmod__(self, other, context=None): 12167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1217cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista Return (self // other, self % other) 12187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1219cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista other = _convert_other(other) 1220cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other is NotImplemented: 1221cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return other 1222cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1223cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if context is None: 1224cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context = getcontext() 1225cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1226cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = self._check_nans(other, context) 1227cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if ans: 1228cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return (ans, ans) 1229cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1230cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista sign = self._sign ^ other._sign 1231cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self._isinfinity(): 1232cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other._isinfinity(): 1233cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = context._raise_error(InvalidOperation, 'divmod(INF, INF)') 1234cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans, ans 1235cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1236cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return (Infsign[sign], 1237cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context._raise_error(InvalidOperation, 'INF % x')) 1238cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1239cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not other: 1240cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not self: 1241cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = context._raise_error(DivisionUndefined, 'divmod(0, 0)') 1242cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans, ans 1243cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1244cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return (context._raise_error(DivisionByZero, 'x // 0', sign), 1245cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context._raise_error(InvalidOperation, 'x % 0')) 1246cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1247cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista quotient, remainder = self._divide(other, context) 1248e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista remainder = remainder._fix(context) 1249cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return quotient, remainder 12507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rdivmod__(self, other, context=None): 12527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Swaps self/other and returns __divmod__.""" 1253636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1254267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1255267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 12567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return other.__divmod__(self, context=context) 12577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __mod__(self, other, context=None): 12597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 12607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self % other 12617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1262636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1263267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1264267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 12657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1266cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if context is None: 1267cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context = getcontext() 12687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1269cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = self._check_nans(other, context) 1270cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if ans: 1271cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans 12727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1273cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self._isinfinity(): 1274cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(InvalidOperation, 'INF % x') 1275cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista elif not other: 1276cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self: 1277cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(InvalidOperation, 'x % 0') 1278cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1279cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionUndefined, '0 % 0') 1280cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1281cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista remainder = self._divide(other, context)[1] 1282e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista remainder = remainder._fix(context) 1283cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return remainder 12847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rmod__(self, other, context=None): 12867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Swaps self/other and returns __mod__.""" 1287636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1288267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1289267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 12907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return other.__mod__(self, context=context) 12917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 12927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def remainder_near(self, other, context=None): 12937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 12947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Remainder nearest to 0- abs(remainder-near) <= other/2 12957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1296636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 1297636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 12987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1299353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 13007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1301353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 1302353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 1303353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 13047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1305353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self == +/-infinity -> InvalidOperation 1306353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 1307353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1308353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'remainder_near(infinity, x)') 13097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1310353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # other == 0 -> either InvalidOperation or DivisionUndefined 1311353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 1312353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self: 1313353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1314353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'remainder_near(x, 0)') 1315353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1316353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(DivisionUndefined, 1317353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'remainder_near(0, 0)') 13187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1319353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # other = +/-infinity -> remainder = self 1320353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinfinity(): 1321353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = Decimal(self) 1322353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 13237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1324353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self = 0 -> remainder = self, with ideal exponent 1325353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ideal_exponent = min(self._exp, other._exp) 1326353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 132772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(self._sign, '0', ideal_exponent) 1328353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 13297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1330353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # catch most cases of large or small quotient 1331353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista expdiff = self.adjusted() - other.adjusted() 1332353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if expdiff >= context.prec + 1: 1333353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expdiff >= prec+1 => abs(self/other) > 10**prec 1334cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionImpossible) 1335353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if expdiff <= -2: 1336353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expdiff <= -2 => abs(self/other) < 0.1 1337353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(ideal_exponent, context.rounding) 1338353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 13397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1340353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adjust both arguments to have the same exponent, then divide 1341353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op1 = _WorkRep(self) 1342353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op2 = _WorkRep(other) 1343353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op1.exp >= op2.exp: 1344353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op1.int *= 10**(op1.exp - op2.exp) 13457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 1346353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op2.int *= 10**(op2.exp - op1.exp) 1347353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q, r = divmod(op1.int, op2.int) 1348353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # remainder is r*10**ideal_exponent; other is +/-op2.int * 1349353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 10**ideal_exponent. Apply correction to ensure that 1350353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # abs(remainder) <= abs(other)/2 1351353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if 2*r + (q&1) > op2.int: 1352353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista r -= op2.int 1353353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q += 1 1354353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1355353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if q >= 10**context.prec: 1356cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionImpossible) 1357353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1358353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result has same sign as self unless r is negative 1359353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = self._sign 1360353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if r < 0: 1361353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = 1-sign 1362353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista r = -r 13637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 136472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(sign, str(r), ideal_exponent) 1365353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 13667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 13677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __floordiv__(self, other, context=None): 13687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """self // other""" 1369cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista other = _convert_other(other) 1370cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other is NotImplemented: 1371cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return other 1372cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1373cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if context is None: 1374cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista context = getcontext() 1375cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1376cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista ans = self._check_nans(other, context) 1377cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if ans: 1378cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return ans 1379cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1380cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self._isinfinity(): 1381cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if other._isinfinity(): 1382cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(InvalidOperation, 'INF // INF') 1383cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1384cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return Infsign[self._sign ^ other._sign] 1385cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1386cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if not other: 1387cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista if self: 1388cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionByZero, 'x // 0', 1389cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista self._sign ^ other._sign) 1390cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista else: 1391cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionUndefined, '0 // 0') 1392cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista 1393cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return self._divide(other, context)[0] 13947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 13957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __rfloordiv__(self, other, context=None): 13967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Swaps self/other and returns __floordiv__.""" 1397636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1398267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1399267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 14007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return other.__floordiv__(self, context=context) 14017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __float__(self): 14037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Float representation.""" 14047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return float(str(self)) 14057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __int__(self): 140746b0802ccd9a9c47b27a285526fbb2a8bee5b8cbBrett Cannon """Converts self to an int, truncating if necessary.""" 1408636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 1409636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isnan(): 1410636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 1411636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidContext) 1412636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger elif self._isinfinity(): 141359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista raise OverflowError("Cannot convert infinity to long") 1414353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista s = (-1)**self._sign 14157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp >= 0: 141672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return s*int(self._int)*10**self._exp 1417605ed0248375bbf87bbd1d80afc7c6918fd3ce2bRaymond Hettinger else: 141872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return s*int(self._int[:self._exp] or '0') 14197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __long__(self): 14217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Converts to a long. 14227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Equivalent to long(int(self)) 14247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 14257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return long(self.__int__()) 14267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1427353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _fix_nan(self, context): 1428353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Decapitate the payload of a NaN to fit the context""" 1429353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista payload = self._int 1430353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1431353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # maximum length of payload is precision if _clamp=0, 1432353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # precision-1 if _clamp=1. 1433353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista max_payload_len = context.prec - context._clamp 1434353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if len(payload) > max_payload_len: 143572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista payload = payload[len(payload)-max_payload_len:].lstrip('0') 143672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, payload, self._exp, True) 14376c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 1438353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1439dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger def _fix(self, context): 14407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Round if it is necessary to keep self within prec precision. 14417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Rounds and fixes the exponent. Does not raise on a sNaN. 14437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 14447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Arguments: 14457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self - Decimal instance 14467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context - context used. 14477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1448636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 1449353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 1450353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isnan(): 1451353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # decapitate payload if necessary 1452353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._fix_nan(context) 1453353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1454353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self is +/-Infinity; return unaltered 14556c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 14567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1457353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if self is zero then exponent should be between Etiny and 1458353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Emax if _clamp==0, and between Etiny and Etop if _clamp==1. 1459353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Etiny = context.Etiny() 1460353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Etop = context.Etop() 14617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 1462353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_max = [context.Emax, Etop][context._clamp] 1463353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista new_exp = min(max(self._exp, Etiny), exp_max) 1464353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if new_exp != self._exp: 1465353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Clamped) 146672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, '0', new_exp) 14677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 14686c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 14697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1470353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp_min is the smallest allowable exponent of the result, 1471353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # equal to max(self.adjusted()-context.prec+1, Etiny) 1472353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_min = len(self._int) + self._exp - context.prec 1473353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if exp_min > Etop: 1474353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # overflow: exp_min > Etop iff self.adjusted() > Emax 1475353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 1476353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 1477353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(Overflow, 'above Emax', self._sign) 1478353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_is_subnormal = exp_min < Etiny 1479353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_subnormal: 1480353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Subnormal) 1481353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_min = Etiny 14827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1483353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # round if self has too many digits 1484353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp < exp_min: 14857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context._raise_error(Rounded) 14862ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista digits = len(self._int) + self._exp - exp_min 14872ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if digits < 0: 14882ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista self = _dec_from_triple(self._sign, '1', exp_min-1) 14892ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista digits = 0 14902ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista this_function = getattr(self, self._pick_rounding_function[context.rounding]) 14912ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista changed = this_function(digits) 14922ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = self._int[:digits] or '0' 14932ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if changed == 1: 14942ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = str(int(coeff)+1) 14952ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista ans = _dec_from_triple(self._sign, coeff, exp_min) 14962ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista 14972ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if changed: 1498353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 1499353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_subnormal: 1500353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Underflow) 1501353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not ans: 1502353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # raise Clamped on underflow to 0 1503353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Clamped) 1504353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif len(ans._int) == context.prec+1: 1505353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we get here only if rescaling rounds the 1506353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # cofficient up to exactly 10**context.prec 1507353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans._exp < Etop: 150872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(ans._sign, 150972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans._int[:-1], ans._exp+1) 1510353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1511353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Inexact and Rounded have already been raised 1512353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = context._raise_error(Overflow, 'above Emax', 1513353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self._sign) 15147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 15157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1516353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fold down if _clamp == 1 and self has too few digits 1517353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context._clamp == 1 and self._exp > Etop: 1518353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Clamped) 151972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self_padded = self._int + '0'*(self._exp - Etop) 152072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, self_padded, Etop) 15217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1522353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # here self was representable to begin with; return unchanged 15236c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 15247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 15257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger _pick_rounding_function = {} 15267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1527353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for each of the rounding functions below: 1528353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self is a finite, nonzero Decimal 1529353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # prec is an integer satisfying 0 <= prec < len(self._int) 15302ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # 15312ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # each function returns either -1, 0, or 1, as follows: 15322ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # 1 indicates that self should be rounded up (away from zero) 15332ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # 0 indicates that self should be truncated, and that all the 15342ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # digits to be truncated are zeros (so the value is unchanged) 15352ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista # -1 indicates that there are nonzero digits to be truncated 15367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1537353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_down(self, prec): 1538353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Also known as round-towards-0, truncate.""" 15392ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if _all_zeros(self._int, prec): 15402ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return 0 15412ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista else: 15422ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 15437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1544353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_up(self, prec): 1545353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds away from 0.""" 15462ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 15477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1548353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_half_up(self, prec): 1549353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds 5 up (away from 0)""" 155072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if self._int[prec] in '56789': 15512ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return 1 15522ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista elif _all_zeros(self._int, prec): 15532ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return 0 1554353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 15552ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 15567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1557353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_half_down(self, prec): 15587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Round 5 down""" 15592ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if _exact_half(self._int, prec): 15602ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 15612ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista else: 15622ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return self._round_half_up(prec) 15637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1564353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_half_even(self, prec): 1565353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Round 5 to even, rest to nearest.""" 15662ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if _exact_half(self._int, prec) and \ 15672ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista (prec == 0 or self._int[prec-1] in '02468'): 15682ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -1 1569353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 15702ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return self._round_half_up(prec) 15717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1572353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_ceiling(self, prec): 15737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Rounds up (not away from 0 if negative.)""" 15747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign: 1575353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._round_down(prec) 15767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 15772ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 15787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1579353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_floor(self, prec): 15807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Rounds down (not towards 0 if negative)""" 15817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self._sign: 1582353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._round_down(prec) 15837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 15842ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 15857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1586353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _round_05up(self, prec): 1587353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Round down unless digit prec-1 is 0 or 5.""" 15882ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if prec and self._int[prec-1] not in '05': 1589353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._round_down(prec) 15902ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista else: 15912ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return -self._round_down(prec) 1592353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1593353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def fma(self, other, third, context=None): 1594353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Fused multiply-add. 15957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1596353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Returns self*other+third with no rounding of the intermediate 1597353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista product self*other. 1598353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1599353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self and other are multiplied together, with no rounding of 1600353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the result. The third operand is then added to the result, 1601353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista and a single final rounding is performed. 16027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 1603353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1604353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 16057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 160658f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista # compute product; raise InvalidOperation if either operand is 160758f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista # a signaling NaN or if the product is zero times infinity. 160858f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if self._is_special or other._is_special: 160958f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if context is None: 161058f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista context = getcontext() 161158f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if self._exp == 'N': 16120f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista return context._raise_error(InvalidOperation, 'sNaN', self) 161358f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if other._exp == 'N': 16140f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista return context._raise_error(InvalidOperation, 'sNaN', other) 161558f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if self._exp == 'n': 161658f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = self 161758f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista elif other._exp == 'n': 161858f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = other 161958f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista elif self._exp == 'F': 162058f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if not other: 162158f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista return context._raise_error(InvalidOperation, 162258f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista 'INF * 0 in fma') 162358f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = Infsign[self._sign ^ other._sign] 162458f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista elif other._exp == 'F': 162558f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista if not self: 162658f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista return context._raise_error(InvalidOperation, 162758f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista '0 * INF in fma') 162858f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = Infsign[self._sign ^ other._sign] 162958f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista else: 163058f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista product = _dec_from_triple(self._sign ^ other._sign, 163158f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista str(int(self._int) * int(other._int)), 163258f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista self._exp + other._exp) 16337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 163458f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista third = _convert_other(third, raiseit=True) 163558f6f2e0c9fad1ef9ee921980888dc52da8100ceFacundo Batista return product.__add__(third, context) 16367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1637353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _power_modulo(self, other, modulo, context=None): 1638353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Three argument version of __pow__""" 16397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1640353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if can't convert other and modulo to Decimal, raise 1641353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # TypeError; there's no point returning NotImplemented (no 1642353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # equivalent of __rpow__ for three argument pow) 1643353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 1644353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo = _convert_other(modulo, raiseit=True) 16457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1646353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 1647353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 16487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1649353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # deal with NaNs: if there are any sNaNs then first one wins, 1650353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (i.e. behaviour for NaNs is identical to that of fma) 1651353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_is_nan = self._isnan() 1652353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other_is_nan = other._isnan() 1653353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo_is_nan = modulo._isnan() 1654353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan or other_is_nan or modulo_is_nan: 1655353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan == 2: 1656353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 16570f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista self) 1658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_is_nan == 2: 1659353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 16600f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista other) 1661353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if modulo_is_nan == 2: 1662353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 16630f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista modulo) 1664353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan: 16656c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return self._fix_nan(context) 1666353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_is_nan: 16676c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return other._fix_nan(context) 16686c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return modulo._fix_nan(context) 1669353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1670353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # check inputs: we apply same restrictions as Python's pow() 1671353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (self._isinteger() and 1672353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other._isinteger() and 1673353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo._isinteger()): 1674353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1675353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'pow() 3rd argument not allowed ' 1676353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'unless all arguments are integers') 1677353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other < 0: 1678353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1679353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'pow() 2nd argument cannot be ' 1680353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'negative when 3rd argument specified') 1681353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not modulo: 1682353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1683353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'pow() 3rd argument cannot be 0') 1684353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1685353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # additional restriction for decimal: the modulus must be less 1686353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # than 10**prec in absolute value 1687353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if modulo.adjusted() >= context.prec: 1688353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1689353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'insufficient precision: pow() 3rd ' 1690353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'argument must not have more than ' 1691353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'precision digits') 1692353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1693353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # define 0**0 == NaN, for consistency with two-argument pow 1694353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (even though it hurts!) 1695353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other and not self: 1696353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1697353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'at least one of pow() 1st argument ' 1698353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'and 2nd argument must be nonzero ;' 1699353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '0**0 is not defined') 1700353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1701353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute sign of result 1702353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._iseven(): 1703353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = 0 1704353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1705353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = self._sign 1706353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1707353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # convert modulo to a Python integer, and self and other to 1708353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Decimal integers (i.e. force their exponents to be >= 0) 1709353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo = abs(int(modulo)) 1710353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = _WorkRep(self.to_integral_value()) 1711353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exponent = _WorkRep(other.to_integral_value()) 1712353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1713353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute result using integer pow() 1714353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo 1715353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for i in xrange(exponent.exp): 1716353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = pow(base, 10, modulo) 1717353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista base = pow(base, exponent.int, modulo) 1718353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 171972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(sign, str(base), 0) 1720353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1721353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _power_exact(self, other, p): 1722353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Attempt to compute self**other exactly. 1723353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1724353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Given Decimals self and other and an integer p, attempt to 1725353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista compute an exact result for the power self**other, with p 1726353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits of precision. Return None if self**other is not 1727353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exactly representable in p digits. 1728353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1729353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Assumes that elimination of special cases has already been 1730353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista performed: self and other must both be nonspecial; self must 1731353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista be positive and not numerically equal to 1; other must be 1732353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista nonzero. For efficiency, other._exp should not be too large, 1733353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista so that 10**abs(other._exp) is a feasible calculation.""" 1734353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1735353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # In the comments below, we write x for the value of self and 1736353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # y for the value of other. Write x = xc*10**xe and y = 1737353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # yc*10**ye. 1738353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1739353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # The main purpose of this method is to identify the *failure* 1740353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # of x**y to be exactly representable with as little effort as 1741353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # possible. So we look for cheap and easy tests that 1742353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # eliminate the possibility of x**y being exact. Only if all 1743353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # these tests are passed do we go on to actually compute x**y. 1744353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1745353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Here's the main idea. First normalize both x and y. We 1746353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # express y as a rational m/n, with m and n relatively prime 1747353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # and n>0. Then for x**y to be exactly representable (at 1748353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # *any* precision), xc must be the nth power of a positive 1749353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # integer and xe must be divisible by n. If m is negative 1750353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # then additionally xc must be a power of either 2 or 5, hence 1751353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # a power of 2**n or 5**n. 1752353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1753353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # There's a limit to how small |y| can be: if y=m/n as above 1754353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # then: 1755353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1756353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (1) if xc != 1 then for the result to be representable we 1757353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # need xc**(1/n) >= 2, and hence also xc**|y| >= 2. So 1758353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <= 1759353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 2**(1/|y|), hence xc**|y| < 2 and the result is not 1760353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # representable. 1761353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1762353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1. Hence if 1763353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # |y| < 1/|xe| then the result is not representable. 1764353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1765353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Note that since x is not equal to 1, at least one of (1) and 1766353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (2) must apply. Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) < 1767353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye. 1768353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1769353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # There's also a limit to how large y can be, at least if it's 1770353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # positive: the normalized result will have coefficient xc**y, 1771353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # so if it's representable then xc**y < 10**p, and y < 1772353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p/log10(xc). Hence if y*log10(xc) >= p then the result is 1773353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # not exactly representable. 1774353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1775353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye, 1776353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # so |y| < 1/xe and the result is not representable. 1777353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y| 1778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # < 1/nbits(xc). 1779353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1780353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista x = _WorkRep(self) 1781353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc, xe = x.int, x.exp 1782353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while xc % 10 == 0: 1783353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc //= 10 1784353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe += 1 1785353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1786353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _WorkRep(other) 1787353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc, ye = y.int, y.exp 1788353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while yc % 10 == 0: 1789353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc //= 10 1790353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ye += 1 1791353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1792353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # case where xc == 1: result is 10**(xe*y), with xe*y 1793353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # required to be an integer 1794353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc == 1: 1795353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ye >= 0: 1796353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exponent = xe*yc*10**ye 1797353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1798353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exponent, remainder = divmod(xe*yc, 10**-ye) 1799353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if remainder: 1800353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1801353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if y.sign == 1: 1802353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exponent = -exponent 1803353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if other is a nonnegative integer, use ideal exponent 1804353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger() and other._sign == 0: 1805353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ideal_exponent = self._exp*int(other) 1806353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = min(exponent-ideal_exponent, p-1) 1807353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1808353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = 0 180972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, '1' + '0'*zeros, exponent-zeros) 1810353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # case where y is negative: xc must be either a power 1812353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # of 2 or a power of 5. 1813353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if y.sign == 1: 1814353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista last_digit = xc % 10 1815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if last_digit in (2,4,6,8): 1816353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # quick test for power of 2 1817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc & -xc != xc: 1818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1819353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # now xc is a power of 2; e is its exponent 1820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = _nbits(xc)-1 1821353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # find e*y and xe*y; both must be integers 1822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ye >= 0: 1823353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y_as_int = yc*10**ye 1824353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = e*y_as_int 1825353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe = xe*y_as_int 1826353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1827353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ten_pow = 10**-ye 1828353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e, remainder = divmod(e*yc, ten_pow) 1829353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if remainder: 1830353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1831353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe, remainder = divmod(xe*yc, ten_pow) 1832353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if remainder: 1833353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1834353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1835353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if e*65 >= p*93: # 93/65 > log(10)/log(5) 1836353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1837353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = 5**e 1838353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1839353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif last_digit == 5: 1840353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # e >= log_5(xc) if xc is a power of 5; we have 1841353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # equality all the way up to xc=5**2658 1842353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = _nbits(xc)*28//65 1843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc, remainder = divmod(5**e, xc) 1844353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if remainder: 1845353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1846353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while xc % 5 == 0: 1847353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc //= 5 1848353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e -= 1 1849353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ye >= 0: 1850353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y_as_integer = yc*10**ye 1851353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = e*y_as_integer 1852353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe = xe*y_as_integer 1853353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1854353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ten_pow = 10**-ye 1855353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e, remainder = divmod(e*yc, ten_pow) 1856353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if remainder: 1857353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1858353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe, remainder = divmod(xe*yc, ten_pow) 1859353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if remainder: 1860353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1861353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if e*3 >= p*10: # 10/3 > log(10)/log(2) 1862353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1863353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = 2**e 1864353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1865353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 18667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1867353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc >= 10**p: 1868353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1869353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe = -e-xe 187072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, str(xc), xe) 18717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1872353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # now y is positive; find m and n such that y = m/n 1873353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ye >= 0: 1874353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m, n = yc*10**ye, 1 1875353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1876353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xe != 0 and len(str(abs(yc*xe))) <= -ye: 1877353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1878353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc_bits = _nbits(xc) 1879353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc != 1 and len(str(abs(yc)*xc_bits)) <= -ye: 1880353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1881353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m, n = yc, 10**(-ye) 1882353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while m % 2 == n % 2 == 0: 1883353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m //= 2 1884353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n //= 2 1885353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while m % 5 == n % 5 == 0: 1886353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista m //= 5 1887353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n //= 5 1888353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1889353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute nth root of xc*10**xe 1890353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n > 1: 1891353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if 1 < xc < 2**n then xc isn't an nth power 1892353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc != 1 and xc_bits <= n: 1893353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1894353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1895353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe, rem = divmod(xe, n) 1896353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rem != 0: 1897353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1898353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1899353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute nth root of xc using Newton's method 1900353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista a = 1L << -(-_nbits(xc)//n) # initial estimate 1901353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 1902353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q, r = divmod(xc, a**(n-1)) 1903353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if a <= q: 1904353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 1905353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1906353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista a = (a*(n-1) + q)//n 1907353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (a == q and r == 0): 1908353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1909353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = a 1910353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1911353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # now xc*10**xe is the nth root of the original xc*10**xe 1912353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute mth power of xc*10**xe 1913353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1914353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if m > p*100//_log10_lb(xc) then m > p/log10(xc), hence xc**m > 1915353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 10**p and the result is not representable. 1916353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc > 1 and m > p*100//_log10_lb(xc): 1917353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1918353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc = xc**m 1919353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xe *= m 1920353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if xc > 10**p: 1921353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return None 1922353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1923353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # by this point the result *is* exactly representable 1924353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adjust the exponent to get as close as possible to the ideal 1925353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exponent, if necessary 1926353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista str_xc = str(xc) 1927353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger() and other._sign == 0: 1928353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ideal_exponent = self._exp*int(other) 1929353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = min(xe-ideal_exponent, p-len(str_xc)) 1930353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1931353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista zeros = 0 193272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, str_xc+'0'*zeros, xe-zeros) 1933cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 1934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def __pow__(self, other, modulo=None, context=None): 1935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return self ** other [ % modulo]. 19367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1937353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With two arguments, compute self**other. 19387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1939353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With three arguments, compute (self**other) % modulo. For the 1940353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista three argument form, the following restrictions on the 1941353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista arguments hold: 19427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1943353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - all three arguments must be integral 1944353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - other must be nonnegative 1945353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - either self or other (or both) must be nonzero 1946353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - modulo must be nonzero and must have at most p digits, 1947353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista where p is the context precision. 19487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1949353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista If any of these restrictions is violated the InvalidOperation 1950353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista flag is raised. 1951353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1952353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result of pow(self, other, modulo) is identical to the 1953353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista result that would be obtained by computing (self**other) % 1954353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista modulo with unbounded precision, but is computed more 1955353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista efficiently. It is always exact. 1956353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 1957353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1958353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if modulo is not None: 1959353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._power_modulo(other, modulo, context) 19607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1961636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger other = _convert_other(other) 1962267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger if other is NotImplemented: 1963267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return other 19647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1965353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 1966353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 19677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1968353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # either argument is a NaN => result is NaN 1969353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 1970353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 1971353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 19727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1973353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity) 1974353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other: 1975353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 1976353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, '0 ** 0') 1977353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1978353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 19797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 1980353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result has sign 1 iff self._sign is 1 and other is an odd integer 1981353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista result_sign = 0 1982353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 1: 1983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger(): 1984353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other._iseven(): 1985353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista result_sign = 1 1986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 1987353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # -ve**noninteger = NaN 1988353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (-0)**noninteger = 0**noninteger 1989353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self: 1990353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 1991353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'x ** y with x negative and y not an integer') 1992353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # negate self, without doing any unwanted rounding 199372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self = self.copy_negate() 1994353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1995353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 0**(+ve or Inf)= 0; 0**(-ve or -Inf) = Infinity 1996353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 1997353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._sign == 0: 199872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '0', 0) 1999353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2000353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Infsign[result_sign] 2001353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Inf**(+ve or Inf) = Inf; Inf**(-ve or -Inf) = 0 2003353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 2004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._sign == 0: 2005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Infsign[result_sign] 2006353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 200772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '0', 0) 2008353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2009353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1**other = 1, but the choice of exponent and the flags 2010353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # depend on the exponent of self, and on whether other is a 2011353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # positive integer, a negative integer, or neither 2012353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self == Dec_p1: 2013353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinteger(): 2014353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp = max(self._exp*max(int(other), 0), 2015353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1-context.prec) but evaluating int(other) directly 2016353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # is dangerous until we know other is small (other 2017353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # could be 1e999999999) 2018353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._sign == 1: 2019353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplier = 0 2020353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif other > context.prec: 2021353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplier = context.prec 2022353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2023353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplier = int(other) 2024353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2025353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp = self._exp * multiplier 2026353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if exp < 1-context.prec: 2027353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp = 1-context.prec 2028353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 2029353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2030353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 2031353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 2032353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp = 1-context.prec 2033353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 203472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '1'+'0'*-exp, exp) 2035353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2036353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute adjusted exponent of self 2037353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_adj = self.adjusted() 2038353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2039353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self ** infinity is infinity if self > 1, 0 if self < 1 2040353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self ** -infinity is infinity if self < 1, 0 if self > 1 2041353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._isinfinity(): 2042353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if (other._sign == 0) == (self_adj < 0): 204372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(result_sign, '0', 0) 2044353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2045353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Infsign[result_sign] 2046353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2047353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # from here on, the result always goes through the call 2048353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # to _fix at the end of this function. 2049353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = None 2050353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2051353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # crude test to catch cases of extreme overflow/underflow. If 2052353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(self)*other >= 10**bound and bound >= len(str(Emax)) 2053353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # then 10**bound >= 10**len(str(Emax)) >= Emax+1 and hence 2054353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self**other >= 10**(Emax+1), so overflow occurs. The test 2055353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for underflow is similar. 2056353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista bound = self._log10_exp_bound() + other.adjusted() 2057353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if (self_adj >= 0) == (other._sign == 0): 2058353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self > 1 and other +ve, or self < 1 and other -ve 2059353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # possibility of overflow 2060353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if bound >= len(str(context.Emax)): 206172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(result_sign, '1', context.Emax+1) 2062353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2063353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self > 1 and other -ve, or self < 1 and other +ve 2064353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # possibility of underflow to 0 2065353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Etiny = context.Etiny() 2066353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if bound >= len(str(-Etiny)): 206772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(result_sign, '1', Etiny-1) 2068353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2069353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # try for an exact result with precision +1 2070353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans is None: 2071353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._power_exact(other, context.prec + 1) 2072353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans is not None and result_sign == 1: 207372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(1, ans._int, ans._exp) 2074353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2075353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # usual case: inexact result, x**y computed directly as exp(y*log(x)) 2076353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans is None: 2077353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 2078353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista x = _WorkRep(self) 2079353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista xc, xe = x.int, x.exp 2080353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _WorkRep(other) 2081353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc, ye = y.int, y.exp 2082353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if y.sign == 1: 2083353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yc = -yc 2084353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2085353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute correctly rounded result: start with precision +3, 2086353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # then increase precision until result is unambiguously roundable 2087353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra = 3 2088353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2089353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = _dpower(xc, xe, yc, ye, p+extra) 2090353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(coeff))-p-1)): 2091353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 2092353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra += 3 2093353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 209472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(result_sign, str(coeff), exp) 2095353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2096353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the specification says that for non-integer other we need to 2097353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # raise Inexact, even when the result is actually exact. In 2098353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the same way, we need to raise Underflow here if the result 2099353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # is subnormal. (The call to _fix will take care of raising 2100353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Rounded and Subnormal, as usual.) 2101353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not other._isinteger(): 2102353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 2103353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # pad with zeros up to length context.prec+1 if necessary 2104353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if len(ans._int) <= context.prec: 2105353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista expdiff = context.prec+1 - len(ans._int) 210672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(ans._sign, ans._int+'0'*expdiff, 210772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans._exp-expdiff) 2108353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans.adjusted() < context.Emin: 2109353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Underflow) 2110353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2111353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # unlike exp, ln and log10, the power function respects the 2112353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rounding mode; no need to use ROUND_HALF_EVEN here 2113353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 2114353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2115353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2116353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def __rpow__(self, other, context=None): 2117353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Swaps self/other and returns __pow__.""" 2118353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other) 2119353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other is NotImplemented: 2120353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other 2121353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return other.__pow__(self, context=context) 2122353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2123353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def normalize(self, context=None): 2124353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Normalize- strip trailing 0s, change anything equal to 0 to 0e0""" 2125353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2126353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2127353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2128353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2129353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 2130353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2131353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2132353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2133353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2134353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista dup = self._fix(context) 2135353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if dup._isinfinity(): 2136353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return dup 2137353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2138353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not dup: 213972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(dup._sign, '0', 0) 2140353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp_max = [context.Emax, context.Etop()][context._clamp] 2141353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista end = len(dup._int) 21427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp = dup._exp 214372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista while dup._int[end-1] == '0' and exp < exp_max: 21447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp += 1 21457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger end -= 1 214672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(dup._sign, dup._int[:end], exp) 21477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2148bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista def quantize(self, exp, rounding=None, context=None, watchexp=True): 21497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Quantize self so its exponent is the same as that of exp. 21507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 21517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Similar to self._rescale(exp._exp) but with error checking. 21527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2153bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista exp = _convert_other(exp, raiseit=True) 2154bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista 2155353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2156353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2157353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rounding is None: 2158353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context.rounding 2159353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2160636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or exp._is_special: 2161636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(exp, context) 2162636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 2163636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 21647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2165636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if exp._isinfinity() or self._isinfinity(): 2166636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if exp._isinfinity() and self._isinfinity(): 21676c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) # if both are inf, it is OK 2168636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return context._raise_error(InvalidOperation, 2169636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 'quantize with one INF') 2170353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2171bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista # if we're not watching exponents, do a simple rescale 2172bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista if not watchexp: 2173bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista ans = self._rescale(exp._exp, rounding) 2174bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista # raise Inexact and Rounded where appropriate 2175bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista if ans._exp > self._exp: 2176bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista context._raise_error(Rounded) 2177bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista if ans != self: 2178bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista context._raise_error(Inexact) 2179bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista return ans 2180bd2fe839db886de6ce9631acd8aa9796b413a565Facundo Batista 2181353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp._exp should be between Etiny and Emax 2182353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (context.Etiny() <= exp._exp <= context.Emax): 2183353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2184353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'target exponent out of bounds in quantize') 2185353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2186353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 218772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(self._sign, '0', exp._exp) 2188353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 2189353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2190353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_adjusted = self.adjusted() 2191353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_adjusted > context.Emax: 2192353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2193353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'exponent of quantize result too large for current context') 2194353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_adjusted - exp._exp + 1 > context.prec: 2195353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2196353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'quantize result has too many digits for current context') 2197353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2198353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(exp._exp, rounding) 2199353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans.adjusted() > context.Emax: 2200353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2201353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'exponent of quantize result too large for current context') 2202353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if len(ans._int) > context.prec: 2203353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2204353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'quantize result has too many digits for current context') 2205353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2206353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # raise appropriate flags 2207353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans._exp > self._exp: 2208353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 2209353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans != self: 2210353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 2211353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans and ans.adjusted() < context.Emin: 2212353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Subnormal) 2213353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2214353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # call to fix takes care of any necessary folddown 2215353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 2216353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 22177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 22187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def same_quantum(self, other): 22191a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self and other have the same exponent; otherwise 22201a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return False. 22217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 22221a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista If either operand is a special value, the following rules are used: 22231a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista * return True if both operands are infinities 22241a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista * return True if both operands are NaNs 22251a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista * otherwise, return False. 22267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 22271a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista other = _convert_other(other, raiseit=True) 2228636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 22291a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return (self.is_nan() and other.is_nan() or 22301a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista self.is_infinite() and other.is_infinite()) 22317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self._exp == other._exp 22327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2233353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _rescale(self, exp, rounding): 2234353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rescale self so that the exponent is exp, either by padding with zeros 2235353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista or by truncating digits, using the given rounding mode. 2236353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2237353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Specials are returned without change. This operation is 2238353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista quiet: it raises no flags, and uses no information from the 2239353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context. 22407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 22417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exp = exp to scale to (an integer) 2242353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = rounding mode 22437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2244636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 22456c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 22467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 224772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, '0', exp) 22487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2249353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp >= exp: 2250353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # pad answer with zeros if necessary 225172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, 225272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int + '0'*(self._exp - exp), exp) 22537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2254353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # too many digits; round and lose data. If self.adjusted() < 2255353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp-1, replace self by 10**(exp-1) before rounding 2256353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits = len(self._int) + self._exp - exp 22577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if digits < 0: 225872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self = _dec_from_triple(self._sign, '1', exp-1) 2259353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits = 0 2260353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista this_function = getattr(self, self._pick_rounding_function[rounding]) 22612ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista changed = this_function(digits) 22622ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = self._int[:digits] or '0' 22632ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista if changed == 1: 22642ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista coeff = str(int(coeff)+1) 22652ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista return _dec_from_triple(self._sign, coeff, exp) 22667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2267353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_exact(self, rounding=None, context=None): 2268353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds to a nearby integer. 22697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2270353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista If no rounding mode is specified, take the rounding mode from 2271353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the context. This method raises the Rounded and Inexact flags 2272353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista when appropriate. 22737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2274353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista See also: to_integral_value, which does exactly the same as 2275353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista this method except that it doesn't raise Inexact or Rounded. 2276353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2277636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 2278636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 2279636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 2280636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 22816c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 22827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp >= 0: 22836c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 2284353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 228572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, '0', 0) 2286636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 2287636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 2288353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rounding is None: 2289353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context.rounding 2290353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 2291353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._rescale(0, rounding) 2292353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans != self: 2293353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 22947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return ans 22957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2296353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_value(self, rounding=None, context=None): 2297353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds to the nearest integer, without raising inexact, rounded.""" 2298353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2299353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2300353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if rounding is None: 2301353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context.rounding 2302353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 2303353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2304353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2305353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 23066c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 2307353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp >= 0: 23086c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 2309353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2310353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._rescale(0, rounding) 23117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2312353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the method name changed, but we provide also the old one, for compatibility 2313353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to_integral = to_integral_value 2314353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2315353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def sqrt(self, context=None): 2316353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return the square root of self.""" 2317636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special: 2318636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger ans = self._check_nans(context=context) 2319636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if ans: 2320636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return ans 23217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2322636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._isinfinity() and self._sign == 0: 2323636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Decimal(self) 23247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 23257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if not self: 2326353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exponent = self._exp // 2. sqrt(-0) = -0 232772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(self._sign, '0', self._exp // 2) 2328353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans._fix(context) 23297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2330636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if context is None: 2331636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger context = getcontext() 2332636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 23337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._sign == 1: 23347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0') 23357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2336353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # At this point self represents a positive number. Let p be 2337353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the desired precision and express self in the form c*100**e 2338353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # with c a positive real number and e an integer, c and e 2339353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # being chosen so that 100**(p-1) <= c < 100**p. Then the 2340353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (exact) square root of self is sqrt(c)*10**e, and 10**(p-1) 2341353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # <= sqrt(c) < 10**p, so the closest representable Decimal at 2342353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # precision p is n*10**e where n = round_half_even(sqrt(c)), 2343353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the closest integer to sqrt(c) with the even integer chosen 2344353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # in the case of a tie. 2345353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 2346353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # To ensure correct rounding in all cases, we use the 2347353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # following trick: we compute the square root to an extra 2348353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # place (precision p+1 instead of precision p), rounding down. 2349353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Then, if the result is inexact and its last digit is 0 or 5, 2350353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we increase the last digit to 1 or 6 respectively; if it's 2351353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exact we leave the last digit alone. Now the final round to 2352353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p places (or fewer in the case of underflow) will round 2353353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # correctly and raise the appropriate flags. 2354353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2355353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # use an extra digit of precision 2356353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista prec = context.prec+1 2357353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2358353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # write argument in the form c*100**e where e = self._exp//2 2359353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # is the 'ideal' exponent, to be used if the square root is 2360353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exactly representable. l is the number of 'digits' of c in 2361353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # base 100, so that 100**(l-1) <= c < 100**l. 2362353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2363353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e = op.exp >> 1 2364353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op.exp & 1: 2365353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = op.int * 10 2366353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = (len(self._int) >> 1) + 1 23677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 2368353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = op.int 2369353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = len(self._int)+1 >> 1 2370353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2371353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rescale so that c has exactly prec base 100 'digits' 2372353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista shift = prec-l 2373353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 2374353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c *= 100**shift 2375353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exact = True 23767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 2377353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, remainder = divmod(c, 100**-shift) 2378353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exact = not remainder 2379353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e -= shift 2380353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2381353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # find n = floor(sqrt(c)) using Newton's method 2382353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n = 10**prec 2383353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2384353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q = c//n 2385353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n <= q: 23867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger break 2387353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2388353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n = n + q >> 1 2389353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exact = exact and n*n == c 2390353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2391353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if exact: 2392353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is exact; rescale to use ideal exponent e 2393353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 2394353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # assert n % 10**shift == 0 2395353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n //= 10**shift 2396353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2397353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n *= 10**-shift 2398353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista e += shift 23997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 2400353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is not exact; fix last digit as described above 2401353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n % 5 == 0: 2402353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista n += 1 24037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 240472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, str(n), e) 24057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2406353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # round, and fit to current context 2407353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 2408353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 2409dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger ans = ans._fix(context) 2410353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 24117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2412353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 24137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 24147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def max(self, other, context=None): 24157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the larger value. 24167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2417353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Like max(self, other) except if one is not a number, returns 24187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger NaN (and signals if one is sNaN). Also rounds. 24197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2420353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 2421636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 24226c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 24236c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 24246c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 2425636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 242659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If one operand is a quiet NaN and the other is number, then the 2427636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # number is always returned 2428636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger sn = self._isnan() 2429636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger on = other._isnan() 2430636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if sn or on: 2431636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if on == 1 and sn != 2: 24326c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return self._fix_nan(context) 2433636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if sn == 1 and on != 2: 24346c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return other._fix_nan(context) 2435636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self._check_nans(other, context) 24367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2437d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger c = self.__cmp__(other) 2438d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger if c == 0: 243959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If both operands are finite and equal in numerical value 2440d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # then an ordering is applied: 2441d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # 244259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If the signs differ then max returns the operand with the 2443d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # positive sign and min returns the operand with the negative sign 2444d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger # 244559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If the signs are the same then the exponent is used to select 2446353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the result. This is exactly the ordering used in compare_total. 2447353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 2448353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2449353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 24507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = other 2451353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2452353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 2453636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 2454e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 24557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 24567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def min(self, other, context=None): 24577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the smaller value. 24587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 245959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista Like min(self, other) except if one is not a number, returns 24607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger NaN (and signals if one is sNaN). Also rounds. 24617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 2462353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 2463636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 24646c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 24656c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 24666c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 2467636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if self._is_special or other._is_special: 246859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If one operand is a quiet NaN and the other is number, then the 2469636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # number is always returned 2470636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger sn = self._isnan() 2471636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger on = other._isnan() 2472636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if sn or on: 2473636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if on == 1 and sn != 2: 24746c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return self._fix_nan(context) 2475636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if sn == 1 and on != 2: 24766c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return other._fix_nan(context) 2477636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return self._check_nans(other, context) 24787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2479d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger c = self.__cmp__(other) 2480d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger if c == 0: 2481353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 2482353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2483353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 2484353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 2485353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 24867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ans = other 2487636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 2488e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 24897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 24907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _isinteger(self): 24917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns whether self is an integer""" 2492353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special: 2493353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return False 24947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if self._exp >= 0: 24957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return True 24967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rest = self._int[self._exp:] 249772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return rest == '0'*len(rest) 24987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 24997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _iseven(self): 2500353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns True if self is even. Assumes self is an integer.""" 2501353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self or self._exp > 0: 2502353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return True 250372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self._int[-1+self._exp] in '02468' 25047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 25057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def adjusted(self): 25067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return the adjusted exponent of self""" 25077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger try: 25087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self._exp + len(self._int) - 1 250959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # If NaN or Infinity, self._exp is string 25107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger except TypeError: 25117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return 0 25127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 2513353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def canonical(self, context=None): 2514353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the same Decimal object. 2515353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2516353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista As we do not have different encodings for the same number, the 2517353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista received object already is in its canonical form. 2518353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2519353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self 2520353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2521353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_signal(self, other, context=None): 2522353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares self to the other operand numerically. 2523353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2524353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista It's pretty much like compare(), but all NaNs signal, with signaling 2525353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista NaNs taking precedence over quiet NaNs. 2526353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2527353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2528353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2529353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2530353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_is_nan = self._isnan() 2531353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other_is_nan = other._isnan() 2532353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan == 2: 2533353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 25340f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista self) 2535353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_is_nan == 2: 2536353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'sNaN', 25370f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista other) 2538353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_is_nan: 2539353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'NaN in compare_signal', 25400f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista self) 2541353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_is_nan: 2542353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 'NaN in compare_signal', 25430f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista other) 2544353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self.compare(other, context=context) 2545353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2546353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total(self, other): 2547353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares self to other using the abstract representations. 2548353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2549353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista This is not like the standard compare, which use their numerical 2550353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value. Note that a total ordering is defined for all possible abstract 2551353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista representations. 2552353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2553353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if one is negative and the other is positive, it's easy 2554353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign and not other._sign: 2555353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2556353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._sign and other._sign: 2557353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2558353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign = self._sign 2559353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2560353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # let's handle both NaN types 2561353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista self_nan = self._isnan() 2562353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other_nan = other._isnan() 2563353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan or other_nan: 2564353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == other_nan: 2565353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._int < other._int: 2566353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2567353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2568353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2569353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2570353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._int > other._int: 2571353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2572353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2573353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2574353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2575353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_0 2576353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2577353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2578353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 1: 2579353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2580353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 1: 2581353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2582353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 2: 2583353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2584353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 2: 2585353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2586353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2587353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 1: 2588353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2589353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 1: 2590353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2591353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self_nan == 2: 2592353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2593353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other_nan == 2: 2594353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2595353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2596353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self < other: 2597353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2598353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self > other: 2599353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2600353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2601353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp < other._exp: 2602353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2603353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2604353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2605353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2606353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._exp > other._exp: 2607353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sign: 2608353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_n1 2609353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2610353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2611353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_0 2612353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2613353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2614353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total_mag(self, other): 2615353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares self to other using abstract repr., ignoring sign. 2616353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2617353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Like compare_total, but with operand's sign ignored and assumed to be 0. 2618353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2619353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista s = self.copy_abs() 2620353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista o = other.copy_abs() 2621353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return s.compare_total(o) 2622353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2623353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_abs(self): 2624353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy with the sign set to 0. """ 262572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, self._int, self._exp, self._is_special) 2626353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2627353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_negate(self): 2628353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy with the sign inverted.""" 2629353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 263072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, self._int, self._exp, self._is_special) 2631353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 263272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(1, self._int, self._exp, self._is_special) 2633353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2634353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_sign(self, other): 2635353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns self with the sign of other.""" 263672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(other._sign, self._int, 263772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._exp, self._is_special) 2638353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2639353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def exp(self, context=None): 2640353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns e ** self.""" 2641353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2642353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2643353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2644353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2645353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(NaN) = NaN 2646353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2647353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2648353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2649353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2650353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(-Infinity) = 0 2651353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == -1: 2652353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_0 2653353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2654353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(0) = 1 2655353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 2656353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_p1 2657353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exp(Infinity) = Infinity 2659353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 2660353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(self) 2661353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2662353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the result is now guaranteed to be inexact (the true 2663353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # mathematical result is transcendental). There's no need to 2664353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # raise Rounded and Inexact here---they'll always be raised as 2665353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # a result of the call to _fix. 2666353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 2667353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista adj = self.adjusted() 2668353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2669353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we only need to do any computation for quite a small range 2670353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # of adjusted exponents---for example, -29 <= adj <= 10 for 2671353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the default context. For smaller exponent the result is 2672353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # indistinguishable from 1 at the given precision, while for 2673353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # larger exponent the result either overflows or underflows. 2674353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 0 and adj > len(str((context.Emax+1)*3)): 2675353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # overflow 267672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '1', context.Emax+1) 2677353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self._sign == 1 and adj > len(str((-context.Etiny()+1)*3)): 2678353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # underflow to 0 267972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '1', context.Etiny()-1) 2680353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self._sign == 0 and adj < -p: 2681353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p+1 digits; final round will raise correct flags 268272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '1' + '0'*(p-1) + '1', -p) 2683353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif self._sign == 1 and adj < -p-1: 2684353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p+1 digits; final round will raise correct flags 268572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, '9'*(p+1), -p-1) 2686353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # general case 2687353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2688353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2689353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 2690353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op.sign == 1: 2691353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = -c 2692353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2693353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute correctly rounded result: increase precision by 2694353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 3 digits at a time until we get an unambiguously 2695353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # roundable result 2696353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra = 3 2697353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2698353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = _dexp(c, e, p+extra) 2699353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(coeff))-p-1)): 2700353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 2701353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra += 3 2702353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 270372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(0, str(coeff), exp) 2704353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2705353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # at this stage, ans should round correctly with *any* 2706353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rounding mode, not just with ROUND_HALF_EVEN 2707353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 2708353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 2709353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 2710353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 2711353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2712353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2713353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2714353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_canonical(self): 27151a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is canonical; otherwise return False. 27161a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista 27171a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista Currently, the encoding of a Decimal instance is always 27181a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista canonical, so this method returns True for any Decimal. 27191a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """ 27201a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return True 2721353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2722353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_finite(self): 27231a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is finite; otherwise return False. 2724353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 27251a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista A Decimal instance is considered finite if it is neither 27261a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista infinite nor a NaN. 2727353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 27281a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return not self._is_special 2729353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2730353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_infinite(self): 27311a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is infinite; otherwise return False.""" 27321a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp == 'F' 2733353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2734353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_nan(self): 27351a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a qNaN or sNaN; otherwise return False.""" 27361a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp in ('n', 'N') 2737353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2738353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_normal(self, context=None): 27391a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a normal number; otherwise return False.""" 27401a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista if self._is_special or not self: 27411a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return False 2742353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2743353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 27441a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return context.Emin <= self.adjusted() <= context.Emax 2745353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2746353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_qnan(self): 27471a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a quiet NaN; otherwise return False.""" 27481a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp == 'n' 2749353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2750353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_signed(self): 27511a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is negative; otherwise return False.""" 27521a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._sign == 1 2753353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2754353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_snan(self): 27551a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a signaling NaN; otherwise return False.""" 27561a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self._exp == 'N' 2757353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2758353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_subnormal(self, context=None): 27591a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is subnormal; otherwise return False.""" 27601a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista if self._is_special or not self: 27611a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return False 2762353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2763353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 27641a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return self.adjusted() < context.Emin 2765353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2766353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_zero(self): 27671a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if self is a zero; otherwise return False.""" 276872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return not self._is_special and self._int == '0' 2769353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2770353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _ln_exp_bound(self): 2771353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute a lower bound for the adjusted exponent of self.ln(). 2772353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In other words, compute r such that self.ln() >= 10**r. Assumes 2773353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista that self is finite and positive and that self != 1. 2774353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2775353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2776353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for 0.1 <= x <= 10 we use the inequalities 1-1/x <= ln(x) <= x-1 2777353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista adj = self._exp + len(self._int) - 1 2778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj >= 1: 2779353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # argument >= 10; we use 23/10 = 2.3 as a lower bound for ln(10) 2780353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str(adj*23//10)) - 1 2781353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj <= -2: 2782353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # argument <= 0.1 2783353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str((-1-adj)*23//10)) - 1 2784353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2785353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 2786353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj == 0: 2787353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1 < self < 10 2788353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista num = str(c-10**-e) 2789353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista den = str(c) 2790353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(num) - len(den) - (num < den) 2791353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adj == -1, 0.1 <= self < 1 2792353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return e + len(str(10**-e - c)) - 1 2793353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2794353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2795353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def ln(self, context=None): 2796353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the natural (base e) logarithm of self.""" 2797353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2798353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2799353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2800353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2801353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(NaN) = NaN 2802353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2803353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2804353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2805353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2806353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(0.0) == -Infinity 2807353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 2808353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return negInf 2809353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2810353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(Infinity) = Infinity 2811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 2812353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Inf 2813353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2814353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(1.0) == 0.0 2815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self == Dec_p1: 2816353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Dec_0 2817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # ln(negative) raises InvalidOperation 2819353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 1: 2820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2821353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'ln of a negative value') 2822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2823353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is irrational, so necessarily inexact 2824353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2825353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 2826353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 2827353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2828353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # correctly rounded result: repeatedly increase precision by 3 2829353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # until we get an unambiguously roundable result 2830353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places = p - self._ln_exp_bound() + 2 # at least p+3 places 2831353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2832353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff = _dlog(c, e, places) 2833353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # assert len(str(abs(coeff)))-p >= 1 2834353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(abs(coeff)))-p-1)): 2835353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 2836353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places += 3 283772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places) 2838353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2839353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 2840353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 2841353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 2842353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 2843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2844353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2845353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _log10_exp_bound(self): 2846353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute a lower bound for the adjusted exponent of self.log10(). 2847353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In other words, find r such that self.log10() >= 10**r. 2848353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Assumes that self is finite and positive and that self != 1. 2849353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2850353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2851353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # For x >= 10 or x < 0.1 we only need a bound on the integer 2852353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # part of log10(self), and this comes directly from the 2853353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # exponent of x. For 0.1 <= x <= 10 we use the inequalities 2854353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1-1/x <= log(x) <= x-1. If x > 1 we have |log10(x)| > 2855353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # (1-1/x)/2.31 > 0. If x < 1 then |log10(x)| > (1-x)/2.31 > 0 2856353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2857353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista adj = self._exp + len(self._int) - 1 2858353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj >= 1: 2859353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self >= 10 2860353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str(adj))-1 2861353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj <= -2: 2862353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # self < 0.1 2863353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(str(-1-adj))-1 2864353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2865353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 2866353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if adj == 0: 2867353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 1 < self < 10 2868353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista num = str(c-10**-e) 2869353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista den = str(231*c) 2870353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(num) - len(den) - (num < den) + 2 2871353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # adj == -1, 0.1 <= self < 1 2872353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista num = str(10**-e-c) 2873353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return len(num) + e - (num < "231") - 1 2874353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2875353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def log10(self, context=None): 2876353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the base 10 logarithm of self.""" 2877353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2878353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2879353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2880353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2881353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(NaN) = NaN 2882353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2883353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2884353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2885353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2886353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(0.0) == -Infinity 2887353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 2888353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return negInf 2889353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2890353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(Infinity) = Infinity 2891353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 2892353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Inf 2893353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2894353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(negative or -Infinity) raises InvalidOperation 2895353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign == 1: 2896353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation, 2897353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'log10 of a negative value') 2898353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2899353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log10(10**n) = n 290072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if self._int[0] == '1' and self._int[1:] == '0'*(len(self._int) - 1): 2901353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # answer may need rounding 2902353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = Decimal(self._exp + len(self._int) - 1) 2903353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 2904353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result is irrational, so necessarily inexact 2905353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista op = _WorkRep(self) 2906353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c, e = op.int, op.exp 2907353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p = context.prec 2908353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2909353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # correctly rounded result: repeatedly increase precision 2910353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # until result is unambiguously roundable 2911353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places = p-self._log10_exp_bound()+2 2912353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while True: 2913353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff = _dlog10(c, e, places) 2914353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # assert len(str(abs(coeff)))-p >= 1 2915353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if coeff % (5*10**(len(str(abs(coeff)))-p-1)): 2916353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista break 2917353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista places += 3 291872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places) 2919353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2920353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context._shallow_copy() 2921353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rounding = context._set_rounding(ROUND_HALF_EVEN) 2922353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = ans._fix(context) 2923353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context.rounding = rounding 2924353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2925353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2926353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logb(self, context=None): 2927353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ Returns the exponent of the magnitude of self's MSD. 2928353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2929353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the integer which is the exponent of the magnitude 2930353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of the most significant digit of self (as though it were truncated 2931353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to a single digit while maintaining the value of that digit and 2932353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista without limiting the resulting exponent). 2933353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # logb(NaN) = NaN 2935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 2936353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 2937353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 2938353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2939353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2940353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2941353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2942353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # logb(+/-Inf) = +Inf 2943353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 2944353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Inf 2945353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2946353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # logb(0) = -Inf, DivisionByZero 2947353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self: 2948cce8df2f678b1e1ec11c86cf2fabf12c2077577dFacundo Batista return context._raise_error(DivisionByZero, 'logb(0)', 1) 2949353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2950353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # otherwise, simply return the adjusted exponent of self, as a 2951353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Decimal. Note that no attempt is made to fit the result 2952353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # into the current context. 2953353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(self.adjusted()) 2954353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2955353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _islogical(self): 2956353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Return True if self is a logical operand. 2957353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2958353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista For being logical, it must be a finite numbers with a sign of 0, 2959353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista an exponent of 0, and a coefficient whose digits must all be 2960353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista either 0 or 1. 2961353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 2962353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign != 0 or self._exp != 0: 2963353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return False 2964353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for dig in self._int: 296572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista if dig not in '01': 2966353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return False 2967353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return True 2968353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2969353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def _fill_logical(self, context, opa, opb): 2970353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista dif = context.prec - len(opa) 2971353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if dif > 0: 297272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista opa = '0'*dif + opa 2973353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif dif < 0: 2974353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista opa = opa[-context.prec:] 2975353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista dif = context.prec - len(opb) 2976353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if dif > 0: 297772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista opb = '0'*dif + opb 2978353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif dif < 0: 2979353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista opb = opb[-context.prec:] 2980353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return opa, opb 2981353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2982353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_and(self, other, context=None): 2983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies an 'and' operation between self and other's digits.""" 2984353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2985353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 2986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._islogical() or not other._islogical(): 2987353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 2988353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2989353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fill to context.prec 2990353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (opa, opb) = self._fill_logical(context, self._int, other._int) 2991353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2992353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # make the operation, and clean starting zeroes 299372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista result = "".join([str(int(a)&int(b)) for a,b in zip(opa,opb)]) 299472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, result.lstrip('0') or '0', 0) 2995353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 2996353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_invert(self, context=None): 2997353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Invert all its digits.""" 2998353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 2999353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 300072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0), 300172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context) 3002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3003353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_or(self, other, context=None): 3004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies an 'or' operation between self and other's digits.""" 3005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3006353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3007353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._islogical() or not other._islogical(): 3008353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3009353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3010353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fill to context.prec 3011353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (opa, opb) = self._fill_logical(context, self._int, other._int) 3012353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3013353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # make the operation, and clean starting zeroes 301472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista result = "".join(str(int(a)|int(b)) for a,b in zip(opa,opb)) 301572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, result.lstrip('0') or '0', 0) 3016353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3017353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_xor(self, other, context=None): 3018353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies an 'xor' operation between self and other's digits.""" 3019353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3020353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3021353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not self._islogical() or not other._islogical(): 3022353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3023353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3024353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # fill to context.prec 3025353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (opa, opb) = self._fill_logical(context, self._int, other._int) 3026353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3027353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # make the operation, and clean starting zeroes 302872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista result = "".join(str(int(a)^int(b)) for a,b in zip(opa,opb)) 302972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, result.lstrip('0') or '0', 0) 3030353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3031353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def max_mag(self, other, context=None): 3032353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values numerically with their sign ignored.""" 3033353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 3034353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 30356c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 30366c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 30376c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 3038353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special or other._is_special: 3039353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # If one operand is a quiet NaN and the other is number, then the 3040353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # number is always returned 3041353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sn = self._isnan() 3042353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista on = other._isnan() 3043353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sn or on: 3044353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if on == 1 and sn != 2: 30456c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return self._fix_nan(context) 3046353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sn == 1 and on != 2: 30476c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return other._fix_nan(context) 3048353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._check_nans(other, context) 3049353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3050353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.copy_abs().__cmp__(other.copy_abs()) 3051353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == 0: 3052353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 3053353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3054353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 3055353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = other 3056353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3057353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 3058353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3059e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 3060353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3061353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def min_mag(self, other, context=None): 3062353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values numerically with their sign ignored.""" 3063353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 3064353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 30656c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista if context is None: 30666c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista context = getcontext() 30676c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista 3068353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._is_special or other._is_special: 3069353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # If one operand is a quiet NaN and the other is number, then the 3070353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # number is always returned 3071353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sn = self._isnan() 3072353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista on = other._isnan() 3073353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sn or on: 3074353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if on == 1 and sn != 2: 30756c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return self._fix_nan(context) 3076353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if sn == 1 and on != 2: 30776c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return other._fix_nan(context) 3078353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._check_nans(other, context) 3079353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3080353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.copy_abs().__cmp__(other.copy_abs()) 3081353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == 0: 3082353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = self.compare_total(other) 3083353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3084353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c == -1: 3085353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self 3086353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3087353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = other 3088353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3089e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista return ans._fix(context) 3090353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3091353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_minus(self, context=None): 3092353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the largest representable number smaller than itself.""" 3093353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3094353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3095353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3096353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 3097353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3098353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3099353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3100353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == -1: 3101353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return negInf 3102353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 310372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(0, '9'*context.prec, context.Etop()) 3104353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3105353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context.copy() 3106353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._set_rounding(ROUND_FLOOR) 3107353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._ignore_all_flags() 3108353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista new_self = self._fix(context) 3109353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if new_self != self: 3110353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return new_self 311172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.__sub__(_dec_from_triple(0, '1', context.Etiny()-1), 311272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context) 3113353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3114353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_plus(self, context=None): 3115353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the smallest representable number larger than itself.""" 3116353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3117353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3118353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3119353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(context=context) 3120353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3121353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3122353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3123353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == 1: 3124353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Inf 3125353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity() == -1: 312672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(1, '9'*context.prec, context.Etop()) 3127353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3128353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = context.copy() 3129353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._set_rounding(ROUND_CEILING) 3130353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._ignore_all_flags() 3131353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista new_self = self._fix(context) 3132353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if new_self != self: 3133353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return new_self 313472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.__add__(_dec_from_triple(0, '1', context.Etiny()-1), 313572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista context) 3136353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3137353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_toward(self, other, context=None): 3138353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the number closest to self, in the direction towards other. 3139353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3140353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the closest representable number to self 3141353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (excluding self) that is in the direction towards other, 3142353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista unless both have the same value. If the two operands are 3143353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista numerically equal, then the result is a copy of self with the 3144353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista sign set to be the same as the sign of other. 3145353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3146353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista other = _convert_other(other, raiseit=True) 3147353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3148353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3149353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3150353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3151353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3152353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3153353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3154353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3155353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista comparison = self.__cmp__(other) 3156353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if comparison == 0: 315772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self.copy_sign(other) 3158353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3159353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if comparison == -1: 3160353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self.next_plus(context) 3161353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: # comparison == 1 3162353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self.next_minus(context) 3163353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3164353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # decide which flags to raise using value of ans 3165353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans._isinfinity(): 3166353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Overflow, 3167353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'Infinite result from next_toward', 3168353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans._sign) 3169353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 3170353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 3171353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista elif ans.adjusted() < context.Emin: 3172353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Underflow) 3173353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Subnormal) 3174353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Rounded) 3175353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Inexact) 3176353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # if precision == 1 then we don't raise Clamped for a 3177353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # result 0E-Etiny. 3178353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not ans: 3179353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context._raise_error(Clamped) 3180353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3181353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3182353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3183353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def number_class(self, context=None): 3184353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns an indication of the class of self. 3185353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3186353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The class is one of the following strings: 31870f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista sNaN 31880f5e7bf3049408d6f4c1855807204c9f13ae98f9Facundo Batista NaN 3189353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Infinity 3190353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Normal 3191353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Subnormal 3192353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Zero 3193353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Zero 3194353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Subnormal 3195353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Normal 3196353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Infinity 3197353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3198353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_snan(): 3199353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "sNaN" 3200353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_qnan(): 3201353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "NaN" 3202353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista inf = self._isinfinity() 3203353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if inf == 1: 3204353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Infinity" 3205353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if inf == -1: 3206353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Infinity" 3207353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_zero(): 3208353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 3209353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Zero" 3210353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3211353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Zero" 3212353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3213353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3214353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self.is_subnormal(context=context): 3215353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 3216353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Subnormal" 3217353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3218353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Subnormal" 3219353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # just a normal, regular, boring number, :) 3220353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._sign: 3221353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "-Normal" 3222353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 3223353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return "+Normal" 3224353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3225353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def radix(self): 3226353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Just returns 10, as this is Decimal, :)""" 3227353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(10) 3228353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3229353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def rotate(self, other, context=None): 3230353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a rotated copy of self, value-of-other times.""" 3231353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3232353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3233353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3234353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3235353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3236353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3237353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3238353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._exp != 0: 3239353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3240353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (-context.prec <= int(other) <= context.prec): 3241353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3242353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3243353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 32446c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 3245353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3246353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # get values, pad if necessary 3247353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista torot = int(other) 3248353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotdig = self._int 3249353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista topad = context.prec - len(rotdig) 3250353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if topad: 325172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotdig = '0'*topad + rotdig 3252353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3253353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # let's rotate! 3254353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotated = rotdig[torot:] + rotdig[:torot] 325572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, 325672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotated.lstrip('0') or '0', self._exp) 3257353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3258353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def scaleb (self, other, context=None): 3259353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns self operand after adding the second value to its exp.""" 3260353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3261353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3262353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3263353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3264353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3265353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3266353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3267353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._exp != 0: 3268353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3269353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista liminf = -2 * (context.Emax + context.prec) 3270353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista limsup = 2 * (context.Emax + context.prec) 3271353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (liminf <= int(other) <= limsup): 3272353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3273353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3274353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 32756c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 3276353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 327772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista d = _dec_from_triple(self._sign, self._int, self._exp + int(other)) 3278353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista d = d._fix(context) 3279353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return d 3280353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3281353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def shift(self, other, context=None): 3282353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a shifted copy of self, value-of-other times.""" 3283353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if context is None: 3284353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context = getcontext() 3285353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3286353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista ans = self._check_nans(other, context) 3287353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ans: 3288353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return ans 3289353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3290353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if other._exp != 0: 3291353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3292353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not (-context.prec <= int(other) <= context.prec): 3293353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return context._raise_error(InvalidOperation) 3294353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3295353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if self._isinfinity(): 32966c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 3297353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3298353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # get values, pad if necessary 3299353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista torot = int(other) 3300353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if not torot: 33016c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(self) 3302353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotdig = self._int 3303353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista topad = context.prec - len(rotdig) 3304353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if topad: 330572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotdig = '0'*topad + rotdig 3306353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3307353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # let's shift! 3308353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if torot < 0: 3309353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotated = rotdig[:torot] 3310353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 331172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotated = rotdig + '0'*torot 3312353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotated = rotated[-context.prec:] 3313353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 331472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return _dec_from_triple(self._sign, 331572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista rotated.lstrip('0') or '0', self._exp) 3316353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 331759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Support for pickling, copy, and deepcopy 33187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __reduce__(self): 33197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return (self.__class__, (str(self),)) 33207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 33217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __copy__(self): 33227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if type(self) == Decimal: 3323cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis return self # I'm immutable; therefore I am my own clone 33247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self.__class__(str(self)) 33257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 33267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __deepcopy__(self, memo): 33277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if type(self) == Decimal: 3328cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis return self # My components are also immutable 33297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return self.__class__(str(self)) 33307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 333172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batistadef _dec_from_triple(sign, coefficient, exponent, special=False): 333272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista """Create a decimal instance directly, without any validation, 333372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista normalization (e.g. removal of leading zeros) or argument 333472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista conversion. 333572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 333672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista This function is for *internal use only*. 333772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista """ 333872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 333972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self = object.__new__(Decimal) 334072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._sign = sign 334172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._int = coefficient 334272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._exp = exponent 334372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self._is_special = special 334472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 334572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista return self 334672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista 334759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Context class ####################################################### 3348cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis 33497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3350cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis# get rounding method function: 335159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batistarounding_functions = [name for name in Decimal.__dict__.keys() 335259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista if name.startswith('_round_')] 33537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerfor name in rounding_functions: 335459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # name is like _round_half_even, goes to the global ROUND_HALF_EVEN value. 33557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger globalname = name[1:].upper() 33567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger val = globals()[globalname] 33577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal._pick_rounding_function[val] = name 33587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 33599ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettingerdel name, val, globalname, rounding_functions 33607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3361ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlanclass _ContextManager(object): 33628b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """Context manager class to support localcontext(). 33631a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum 3364ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan Sets a copy of the supplied context in __enter__() and restores 33658b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan the previous decimal context in __exit__() 33668b6999b4c5fab174090be263ba90f193bdede141Nick Coghlan """ 33671a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum def __init__(self, new_context): 3368ced1218dd1b69ac848f8c79d1afaada5669af33cNick Coghlan self.new_context = new_context.copy() 33691a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum def __enter__(self): 33701a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum self.saved_context = getcontext() 33711a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum setcontext(self.new_context) 33721a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum return self.new_context 33731a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum def __exit__(self, t, v, tb): 33741a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum setcontext(self.saved_context) 33751a5e21e0334a6d4e1c756575023c7157fc9ee306Guido van Rossum 33767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass Context(object): 33777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Contains the context for a Decimal instance. 33787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 33797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Contains: 33807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger prec - precision (for use in rounding, division, square roots..) 338159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista rounding - rounding type (how you round) 3382bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps - If traps[exception] = 1, then the exception is 33837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger raised when it is caused. Otherwise, a value is 33847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger substituted in. 33857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger flags - When an exception is caused, flags[exception] is incremented. 33867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (Whether or not the trap_enabler is set) 33877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Should be reset by user of Decimal instance. 33880ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettinger Emin - Minimum exponent 33890ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettinger Emax - Maximum exponent 33907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger capitals - If 1, 1*10^1 is printed as 1E+1. 33917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If 0, printed as 1e1 3392e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger _clamp - If 1, change exponents if too high (Default 0) 33937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 33949ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger 33957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __init__(self, prec=None, rounding=None, 3396abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger traps=None, flags=None, 33970ea241e9e2d2accb625c4b1d3af6dd1f8df23a75Raymond Hettinger Emin=None, Emax=None, 3398e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger capitals=None, _clamp=0, 3399abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger _ignored_flags=None): 3400abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger if flags is None: 3401abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger flags = [] 3402abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger if _ignored_flags is None: 3403abf8a56e68a38f5bcff1fa1aa3742ff35854dd45Raymond Hettinger _ignored_flags = [] 3404bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger if not isinstance(flags, dict): 3405fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger flags = dict([(s,s in flags) for s in _signals]) 3406b91af521fddac47b14c57cd9ba736775334e6379Raymond Hettinger del s 3407bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger if traps is not None and not isinstance(traps, dict): 3408fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger traps = dict([(s,s in traps) for s in _signals]) 3409b91af521fddac47b14c57cd9ba736775334e6379Raymond Hettinger del s 34107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger for name, val in locals().items(): 34117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if val is None: 3412eb2608415ee4eb8aaea746f6a313937e264d0cdaRaymond Hettinger setattr(self, name, _copy.copy(getattr(DefaultContext, name))) 34137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 34147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger setattr(self, name, val) 34157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger del self.self 34167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3417b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger def __repr__(self): 3418bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger """Show the current context.""" 3419b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger s = [] 342059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista s.append('Context(prec=%(prec)d, rounding=%(rounding)s, ' 342159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d' 342259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista % vars(self)) 342359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista names = [f.__name__ for f, v in self.flags.items() if v] 342459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista s.append('flags=[' + ', '.join(names) + ']') 342559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista names = [t.__name__ for t, v in self.traps.items() if v] 342659c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista s.append('traps=[' + ', '.join(names) + ']') 3427b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger return ', '.join(s) + ')' 3428b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger 3429d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger def clear_flags(self): 3430d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger """Reset all flags to zero""" 3431d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger for flag in self.flags: 3432b1b605ef546c45eac3c53db369ea6e881f05dc8bRaymond Hettinger self.flags[flag] = 0 3433d9c0a7ae94ef81e69d927d834131bad760d4d99dRaymond Hettinger 34349fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger def _shallow_copy(self): 34359fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger """Returns a shallow copy from self.""" 3436e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista nc = Context(self.prec, self.rounding, self.traps, 3437e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.flags, self.Emin, self.Emax, 3438e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.capitals, self._clamp, self._ignored_flags) 34397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return nc 34409fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger 34419fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger def copy(self): 34429fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger """Returns a deep copy from self.""" 344359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista nc = Context(self.prec, self.rounding, self.traps.copy(), 3444e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.flags.copy(), self.Emin, self.Emax, 3445e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista self.capitals, self._clamp, self._ignored_flags) 34469fce44bc8cfb57d247e81ed18ac521db0fa015f5Raymond Hettinger return nc 34479ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger __copy__ = copy 34487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34495aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger def _raise_error(self, condition, explanation = None, *args): 34507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Handles an error 34517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the flag is in _ignored_flags, returns the default response. 34537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Otherwise, it increments the flag, then, if the corresponding 34547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger trap_enabler is set, it reaises the exception. Otherwise, it returns 34557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the default value after incrementing the flag. 34567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 34575aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger error = _condition_map.get(condition, condition) 34587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if error in self._ignored_flags: 345959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Don't touch the flag 34607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return error().handle(self, *args) 34617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.flags[error] += 1 3463bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger if not self.traps[error]: 346459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # The errors define how to handle themselves. 34655aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger return condition().handle(self, *args) 34667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Errors should only be risked on copies of the context 346859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # self._ignored_flags = [] 34697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger raise error, explanation 34707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _ignore_all_flags(self): 34727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Ignore all flags, if they are raised""" 3473fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger return self._ignore_flags(*_signals) 34747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _ignore_flags(self, *flags): 34767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Ignore the flags, if they are raised""" 34777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # Do not mutate-- This way, copies of a context leave the original 34787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # alone. 34797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self._ignored_flags = (self._ignored_flags + list(flags)) 34807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return list(flags) 34817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _regard_flags(self, *flags): 34837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Stop ignoring the flags, if they are raised""" 34847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if flags and isinstance(flags[0], (tuple,list)): 34857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger flags = flags[0] 34867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger for flag in flags: 34877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self._ignored_flags.remove(flag) 34887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34895aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger def __hash__(self): 34905aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger """A Context cannot be hashed.""" 34915aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger # We inherit object.__hash__, so we must deny this explicitly 349259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista raise TypeError("Cannot hash a Context.") 34935aa478badfabb005e6f17c98fcf007a0bc54eeccRaymond Hettinger 34947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def Etiny(self): 34957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns Etiny (= Emin - prec + 1)""" 34967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return int(self.Emin - self.prec + 1) 34977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 34987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def Etop(self): 3499e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger """Returns maximum exponent (= Emax - prec + 1)""" 35007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return int(self.Emax - self.prec + 1) 35017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _set_rounding(self, type): 35037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Sets the rounding type. 35047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Sets the rounding type, and returns the current (previous) 35067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounding type. Often used like: 35077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context = context.copy() 35097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # so you don't change the calling context 35107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # if an error occurs in the middle. 35117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounding = context._set_rounding(ROUND_UP) 35127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger val = self.__sub__(other, context=context) 35137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger context._set_rounding(rounding) 35147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This will make it round up for that operation. 35167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 35177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger rounding = self.rounding 35187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.rounding= type 35197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return rounding 35207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3521fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger def create_decimal(self, num='0'): 35227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Creates a new Decimal instance but using self as context.""" 35237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger d = Decimal(num, context=self) 3524353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if d._isnan() and len(d._int) > self.prec - self._clamp: 3525353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return self._raise_error(ConversionSyntax, 3526353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista "diagnostic info too long in NaN") 3527dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger return d._fix(self) 35287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 352959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista # Methods 35307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def abs(self, a): 35317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the absolute value of the operand. 35327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the operand is negative, the result is the same as using the minus 353459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation on the operand. Otherwise, the result is the same as using 35357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the plus operation on the operand. 35367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35379ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('2.1')) 35387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.1") 35399ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('-100')) 35407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("100") 35419ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('101.5')) 35427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("101.5") 35439ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.abs(Decimal('-101.5')) 35447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("101.5") 35457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 35467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__abs__(context=self) 35477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def add(self, a, b): 35497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Return the sum of the two operands. 35507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35519ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.add(Decimal('12'), Decimal('7.00')) 35527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("19.00") 35539ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4')) 35547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.02E+4") 35557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 35567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__add__(b, context=self) 35577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def _apply(self, a): 3559dab988dd23e6328dadfe8408cd96abf4b017380dRaymond Hettinger return str(a._fix(self)) 35607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3561353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def canonical(self, a): 3562353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the same Decimal object. 3563353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3564353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista As we do not have different encodings for the same number, the 3565353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista received object already is in its canonical form. 3566353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3567353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.canonical(Decimal('2.50')) 3568353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2.50") 3569353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3570353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.canonical(context=self) 3571353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 35727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def compare(self, a, b): 35737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Compares values numerically. 35747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the signs of the operands differ, a value representing each operand 35767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ('-1' if the operand is less than zero, '0' if the operand is zero or 35777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger negative zero, or '1' if the operand is greater than zero) is used in 35787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger place of that operand for the comparison instead of the actual 35797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operand. 35807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The comparison is then effected by subtracting the second operand from 35827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the first and then returning a value according to the result of the 35837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger subtraction: '-1' if the result is less than zero, '0' if the result is 35847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger zero or negative zero, or '1' if the result is greater than zero. 35857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 35869ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3')) 35877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-1") 35889ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1')) 35897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0") 35909ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10')) 35917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0") 35929ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1')) 35937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 35949ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3')) 35957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 35969ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1')) 35977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-1") 35987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 35997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.compare(b, context=self) 36007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3601353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_signal(self, a, b): 3602353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values of the two operands numerically. 3603353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3604353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista It's pretty much like compare(), but all NaNs signal, with signaling 3605353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista NaNs taking precedence over quiet NaNs. 3606353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3607353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext 3608353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('2.1'), Decimal('3')) 3609353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1") 3610353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('2.1'), Decimal('2.1')) 3611353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3612353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.flags[InvalidOperation] = 0 3613353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 3614353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 0 3615353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('NaN'), Decimal('2.1')) 3616353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("NaN") 3617353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 3618353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1 3619353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.flags[InvalidOperation] = 0 3620353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 3621353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 0 3622353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1')) 3623353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("NaN") 3624353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> print c.flags[InvalidOperation] 3625353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 1 3626353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3627353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.compare_signal(b, context=self) 3628353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3629353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total(self, a, b): 3630353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares two operands using their abstract representation. 3631353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3632353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista This is not like the standard compare, which use their numerical 3633353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value. Note that a total ordering is defined for all possible abstract 3634353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista representations. 3635353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3636353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9')) 3637353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1") 3638353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12')) 3639353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1") 3640353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3')) 3641353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1") 3642353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30')) 3643353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3644353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300')) 3645353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 3646353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN')) 3647353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1") 3648353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3649353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.compare_total(b) 3650353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3651353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def compare_total_mag(self, a, b): 3652353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares two operands using their abstract representation ignoring sign. 3653353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3654353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Like compare_total, but with operand's sign ignored and assumed to be 0. 3655353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3656353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.compare_total_mag(b) 3657353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_abs(self, a): 3659353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy of the operand with the sign set to 0. 3660353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3661353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_abs(Decimal('2.1')) 3662353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2.1") 3663353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_abs(Decimal('-100')) 3664353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("100") 3665353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3666353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.copy_abs() 3667353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3668353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_decimal(self, a): 3669353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy of the decimal objet. 3670353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3671353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_decimal(Decimal('2.1')) 3672353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2.1") 3673353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_decimal(Decimal('-1.00')) 3674353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1.00") 3675353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 36766c398da0e79cbd68ec4625f00f37a8f2a0a26e5cFacundo Batista return Decimal(a) 3677353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3678353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_negate(self, a): 3679353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a copy of the operand with the sign inverted. 3680353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3681353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_negate(Decimal('101.5')) 3682353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-101.5") 3683353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_negate(Decimal('-101.5')) 3684353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("101.5") 3685353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3686353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.copy_negate() 3687353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3688353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def copy_sign(self, a, b): 3689353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Copies the second operand's sign to the first one. 3690353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3691353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In detail, it returns a copy of the first operand with the sign 3692353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista equal to the sign of the second operand. 3693353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3694353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33')) 3695353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.50") 3696353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33')) 3697353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.50") 3698353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33')) 3699353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1.50") 3700353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33')) 3701353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1.50") 3702353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3703353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.copy_sign(b) 3704353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 37057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def divide(self, a, b): 37067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Decimal division in a specified context. 37077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 37089ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1'), Decimal('3')) 37097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.333333333") 37109ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('2'), Decimal('3')) 37117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.666666667") 37129ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('5'), Decimal('2')) 37137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.5") 37149ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1'), Decimal('10')) 37157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.1") 37169ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('12'), Decimal('12')) 37177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 37189ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2')) 37197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("4.00") 37209ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0')) 37217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.20") 37229ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1000'), Decimal('100')) 37237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("10") 37249ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('1000'), Decimal('1')) 37257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1000") 37269ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2')) 37277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.20E+6") 37287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 37297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__div__(b, context=self) 37307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 37317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def divide_int(self, a, b): 37327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Divides two numbers and returns the integer part of the result. 37337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 37349ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3')) 37357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0") 37369ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3')) 37377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3") 37389ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3')) 37397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3") 37407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 37417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__floordiv__(b, context=self) 37427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 37437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def divmod(self, a, b): 37447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__divmod__(b, context=self) 37457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 3746353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def exp(self, a): 3747353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns e ** a. 3748353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3749353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 3750353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 3751353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 3752353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('-Infinity')) 3753353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3754353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('-1')) 3755353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0.367879441") 3756353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('0')) 3757353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 3758353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('1')) 3759353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2.71828183") 3760353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('0.693147181')) 3761353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2.00000000") 3762353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.exp(Decimal('+Infinity')) 3763353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("Infinity") 3764353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3765353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.exp(context=self) 3766353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3767353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def fma(self, a, b, c): 3768353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a multiplied by b, plus c. 3769353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3770353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The first two operands are multiplied together, using multiply, 3771353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the third operand is then added to the result of that 3772353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista multiplication, using add, all with only one final rounding. 3773353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3774353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7')) 3775353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("22") 3776353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7')) 3777353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-8") 3778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578')) 3779353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.38435736E+12") 3780353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3781353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.fma(b, c, context=self) 3782353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3783353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_canonical(self, a): 37841a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is canonical; otherwise return False. 37851a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista 37861a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista Currently, the encoding of a Decimal instance is always 37871a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista canonical, so this method returns True for any Decimal. 3788353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3789353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_canonical(Decimal('2.50')) 37901a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3791353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 37921a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista return a.is_canonical() 3793353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3794353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_finite(self, a): 37951a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is finite; otherwise return False. 3796353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 37971a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista A Decimal instance is considered finite if it is neither 37981a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista infinite nor a NaN. 3799353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3800353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('2.50')) 38011a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3802353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('-0.3')) 38031a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3804353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('0')) 38051a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3806353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('Inf')) 38071a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3808353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_finite(Decimal('NaN')) 38091a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3810353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_finite() 3812353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3813353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_infinite(self, a): 38141a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is infinite; otherwise return False. 3815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3816353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_infinite(Decimal('2.50')) 38171a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_infinite(Decimal('-Inf')) 38191a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_infinite(Decimal('NaN')) 38211a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3823353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_infinite() 3824353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3825353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_nan(self, a): 38261a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a qNaN or sNaN; 38271a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista otherwise return False. 3828353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3829353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_nan(Decimal('2.50')) 38301a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3831353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_nan(Decimal('NaN')) 38321a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3833353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_nan(Decimal('-sNaN')) 38341a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3835353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3836353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_nan() 3837353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3838353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_normal(self, a): 38391a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a normal number; 38401a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista otherwise return False. 3841353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3842353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 3843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 3844353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 3845353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('2.50')) 38461a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3847353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('0.1E-999')) 38481a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3849353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('0.00')) 38501a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3851353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('-Inf')) 38521a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3853353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_normal(Decimal('NaN')) 38541a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3855353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3856353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_normal(context=self) 3857353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3858353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_qnan(self, a): 38591a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a quiet NaN; otherwise return False. 3860353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3861353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_qnan(Decimal('2.50')) 38621a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3863353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_qnan(Decimal('NaN')) 38641a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3865353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_qnan(Decimal('sNaN')) 38661a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3867353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3868353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_qnan() 3869353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3870353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_signed(self, a): 38711a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is negative; otherwise return False. 3872353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3873353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_signed(Decimal('2.50')) 38741a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3875353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_signed(Decimal('-12')) 38761a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3877353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_signed(Decimal('-0')) 38781a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3879353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3880353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_signed() 3881353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3882353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_snan(self, a): 38831a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a signaling NaN; 38841a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista otherwise return False. 3885353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3886353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_snan(Decimal('2.50')) 38871a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3888353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_snan(Decimal('NaN')) 38891a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3890353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_snan(Decimal('sNaN')) 38911a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3892353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3893353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_snan() 3894353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3895353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_subnormal(self, a): 38961a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is subnormal; otherwise return False. 3897353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3898353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 3899353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 3900353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 3901353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('2.50')) 39021a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3903353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('0.1E-999')) 39041a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3905353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('0.00')) 39061a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3907353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('-Inf')) 39081a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3909353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.is_subnormal(Decimal('NaN')) 39101a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3911353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3912353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_subnormal(context=self) 3913353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3914353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def is_zero(self, a): 39151a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista """Return True if the operand is a zero; otherwise return False. 3916353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3917353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_zero(Decimal('0')) 39181a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3919353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_zero(Decimal('2.50')) 39201a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista False 3921353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.is_zero(Decimal('-0E+2')) 39221a191df14dc2f37933ef32f553fcaa7a5ac77cf7Facundo Batista True 3923353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3924353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.is_zero() 3925353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3926353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def ln(self, a): 3927353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the natural (base e) logarithm of the operand. 3928353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3929353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 3930353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 3931353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 3932353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('0')) 3933353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-Infinity") 3934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('1.000')) 3935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3936353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('2.71828183')) 3937353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.00000000") 3938353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('10')) 3939353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2.30258509") 3940353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.ln(Decimal('+Infinity')) 3941353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("Infinity") 3942353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3943353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.ln(context=self) 3944353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3945353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def log10(self, a): 3946353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the base 10 logarithm of the operand. 3947353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3948353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 3949353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 3950353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 3951353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('0')) 3952353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-Infinity") 3953353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('0.001')) 3954353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-3") 3955353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('1.000')) 3956353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3957353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('2')) 3958353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0.301029996") 3959353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('10')) 3960353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 3961353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('70')) 3962353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.84509804") 3963353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.log10(Decimal('+Infinity')) 3964353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("Infinity") 3965353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3966353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.log10(context=self) 3967353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3968353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logb(self, a): 3969353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ Returns the exponent of the magnitude of the operand's MSD. 3970353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3971353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the integer which is the exponent of the magnitude 3972353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of the most significant digit of the operand (as though the 3973353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista operand were truncated to a single digit while maintaining the 3974353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value of that digit and without limiting the resulting exponent). 3975353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3976353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('250')) 3977353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2") 3978353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('2.50')) 3979353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3980353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('0.03')) 3981353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-2") 3982353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logb(Decimal('0')) 3983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-Infinity") 3984353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 3985353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logb(context=self) 3986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3987353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_and(self, a, b): 3988353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies the logical operation 'and' between each operand's digits. 3989353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3990353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operands must be both logical numbers. 3991353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 3992353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0')) 3993353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3994353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1')) 3995353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3996353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0')) 3997353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 3998353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1')) 3999353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 4000353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010')) 4001353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1000") 4002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10')) 4003353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("10") 4004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_and(b, context=self) 4006353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4007353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_invert(self, a): 4008353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Invert all the digits in the operand. 4009353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4010353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operand must be a logical number. 4011353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4012353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('0')) 4013353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("111111111") 4014353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('1')) 4015353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("111111110") 4016353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('111111111')) 4017353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 4018353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_invert(Decimal('101010101')) 4019353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("10101010") 4020353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4021353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_invert(context=self) 4022353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4023353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_or(self, a, b): 4024353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies the logical operation 'or' between each operand's digits. 4025353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4026353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operands must be both logical numbers. 4027353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4028353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0')) 4029353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 4030353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1')) 4031353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 4032353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0')) 4033353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 4034353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1')) 4035353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 4036353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010')) 4037353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1110") 4038353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10')) 4039353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1110") 4040353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4041353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_or(b, context=self) 4042353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4043353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def logical_xor(self, a, b): 4044353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Applies the logical operation 'xor' between each operand's digits. 4045353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4046353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The operands must be both logical numbers. 4047353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4048353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0')) 4049353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 4050353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1')) 4051353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 4052353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0')) 4053353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 4054353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1')) 4055353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 4056353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010')) 4057353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("110") 4058353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10')) 4059353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1101") 4060353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4061353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.logical_xor(b, context=self) 4062353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 40637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def max(self, a,b): 40647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """max compares two values numerically and returns the maximum. 40657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 40667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If either operand is a NaN then the general rules apply. 40677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Otherwise, the operands are compared as as though by the compare 406859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation. If they are numerically equal then the left-hand operand 406959c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista is chosen as the result. Otherwise the maximum (closer to positive 40707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger infinity) of the two operands is chosen as the result. 40717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 40729ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.max(Decimal('3'), Decimal('2')) 40737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3") 40749ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.max(Decimal('-10'), Decimal('3')) 40757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3") 40769ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.max(Decimal('1.0'), Decimal('1')) 4077d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger Decimal("1") 4078d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger >>> ExtendedContext.max(Decimal('7'), Decimal('NaN')) 4079d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger Decimal("7") 40807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 40817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.max(b, context=self) 40827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4083353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def max_mag(self, a, b): 4084353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values numerically with their sign ignored.""" 4085353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.max_mag(b, context=self) 4086353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 40877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def min(self, a,b): 40887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """min compares two values numerically and returns the minimum. 40897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 40907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If either operand is a NaN then the general rules apply. 40917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Otherwise, the operands are compared as as though by the compare 409259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation. If they are numerically equal then the left-hand operand 409359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista is chosen as the result. Otherwise the minimum (closer to negative 40947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger infinity) of the two operands is chosen as the result. 40957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 40969ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.min(Decimal('3'), Decimal('2')) 40977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2") 40989ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.min(Decimal('-10'), Decimal('3')) 40997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-10") 41009ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.min(Decimal('1.0'), Decimal('1')) 41017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.0") 4102d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger >>> ExtendedContext.min(Decimal('7'), Decimal('NaN')) 4103d6c700a320eacd6f04cbcc60996b84e765766890Raymond Hettinger Decimal("7") 41047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 41057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.min(b, context=self) 41067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4107353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def min_mag(self, a, b): 4108353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compares the values numerically with their sign ignored.""" 4109353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.min_mag(b, context=self) 4110353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 41117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def minus(self, a): 41127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Minus corresponds to unary prefix minus in Python. 41137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 41147c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is evaluated using the same rules as subtract; the 41157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation minus(a) is calculated as subtract('0', a) where the '0' 41167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger has the same exponent as the operand. 41177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 41189ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.minus(Decimal('1.3')) 41197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-1.3") 41209ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.minus(Decimal('-1.3')) 41217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.3") 41227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 41237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__neg__(context=self) 41247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 41257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def multiply(self, a, b): 41267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """multiply multiplies two operands. 41277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4128cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis If either operand is a special value then the general rules apply. 4129cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis Otherwise, the operands are multiplied together ('long multiplication'), 4130cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis resulting in a number which may be as long as the sum of the lengths 4131cfe3128cd58c4a59d236527184b40352ae35e44aMartin v. Löwis of the two operands. 41327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 41339ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3')) 41347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3.60") 41359ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('7'), Decimal('3')) 41367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("21") 41379ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8')) 41387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.72") 41399ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0')) 41407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0.0") 41419ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321')) 41427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("4.28135971E+11") 41437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 41447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__mul__(b, context=self) 41457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4146353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_minus(self, a): 4147353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the largest representable number smaller than a. 4148353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4149353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4150353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4151353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4152353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_minus(Decimal('1')) 4153353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0.999999999") 4154353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_minus(Decimal('1E-1007')) 4155353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0E-1007") 4156353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_minus(Decimal('-1.00000003')) 4157353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1.00000004") 4158353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_minus(Decimal('Infinity')) 4159353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("9.99999999E+999") 4160353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4161353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.next_minus(context=self) 4162353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4163353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_plus(self, a): 4164353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the smallest representable number larger than a. 4165353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4166353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4167353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4168353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4169353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_plus(Decimal('1')) 4170353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.00000001") 4171353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_plus(Decimal('-1E-1007')) 4172353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-0E-1007") 4173353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.next_plus(Decimal('-1.00000003')) 4174353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1.00000002") 4175353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_plus(Decimal('-Infinity')) 4176353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-9.99999999E+999") 4177353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4178353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.next_plus(context=self) 4179353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4180353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def next_toward(self, a, b): 4181353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the number closest to a, in direction towards b. 4182353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4183353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result is the closest representable number from the first 4184353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista operand (but not the first operand) that is in the direction 4185353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista towards the second operand, unless the operands have the same 4186353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value. 4187353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4188353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4189353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4190353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4191353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('1'), Decimal('2')) 4192353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.00000001") 4193353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('-1E-1007'), Decimal('1')) 4194353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-0E-1007") 4195353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('-1.00000003'), Decimal('0')) 4196353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1.00000002") 4197353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('1'), Decimal('0')) 4198353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0.999999999") 4199353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('1E-1007'), Decimal('-100')) 4200353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0E-1007") 4201353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10')) 4202353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-1.00000004") 4203353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000')) 4204353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-0.00") 4205353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4206353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.next_toward(b, context=self) 4207353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 42087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def normalize(self, a): 4209e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger """normalize reduces an operand to its simplest form. 42107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Essentially a plus operation with all trailing zeros removed from the 42127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger result. 42137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42149ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('2.1')) 42157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.1") 42169ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('-2.0')) 42177c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-2") 42189ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('1.200')) 42197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.2") 42209ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('-120')) 42217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-1.2E+2") 42229ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('120.00')) 42237c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.2E+2") 42249ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.normalize(Decimal('0.00')) 42257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0") 42267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 42277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.normalize(context=self) 42287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4229353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def number_class(self, a): 4230353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns an indication of the class of the operand. 4231353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4232353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The class is one of the following strings: 4233353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -sNaN 4234353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -NaN 4235353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Infinity 4236353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Normal 4237353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Subnormal 4238353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista -Zero 4239353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Zero 4240353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Subnormal 4241353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Normal 4242353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista +Infinity 4243353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4244353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = Context(ExtendedContext) 4245353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4246353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4247353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('Infinity')) 4248353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Infinity' 4249353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('1E-10')) 4250353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Normal' 4251353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('2.50')) 4252353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Normal' 4253353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('0.1E-999')) 4254353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Subnormal' 4255353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('0')) 4256353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '+Zero' 4257353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-0')) 4258353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Zero' 4259353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-0.1E-999')) 4260353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Subnormal' 4261353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-1E-10')) 4262353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Normal' 4263353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-2.50')) 4264353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Normal' 4265353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-Infinity')) 4266353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '-Infinity' 4267353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('NaN')) 4268353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'NaN' 4269353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('-NaN')) 4270353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'NaN' 4271353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.number_class(Decimal('sNaN')) 4272353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'sNaN' 4273353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4274353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.number_class(context=self) 4275353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 42767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def plus(self, a): 42777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Plus corresponds to unary prefix plus in Python. 42787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is evaluated using the same rules as add; the 42807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger operation plus(a) is calculated as add('0', a) where the '0' 42817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger has the same exponent as the operand. 42827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42839ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.plus(Decimal('1.3')) 42847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.3") 42859ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.plus(Decimal('-1.3')) 42867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-1.3") 42877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 42887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__pos__(context=self) 42897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 42907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def power(self, a, b, modulo=None): 42917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Raises a to the power of b, to modulo if given. 42927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4293353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With two arguments, compute a**b. If a is negative then b 4294353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista must be integral. The result will be inexact unless b is 4295353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista integral and the result is finite and can be expressed exactly 4296353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in 'precision' digits. 4297353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4298353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista With three arguments, compute (a**b) % modulo. For the 4299353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista three argument form, the following restrictions on the 4300353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista arguments hold: 4301353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4302353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - all three arguments must be integral 4303353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - b must be nonnegative 4304353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - at least one of a or b must be nonzero 4305353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista - modulo must be nonzero and have at most 'precision' digits 4306353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4307353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The result of pow(a, b, modulo) is identical to the result 4308353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista that would be obtained by computing (a**b) % modulo with 4309353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista unbounded precision, but is computed more efficiently. It is 4310353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista always exact. 4311353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4312353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c = ExtendedContext.copy() 4313353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emin = -999 4314353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.Emax = 999 4315353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('2'), Decimal('3')) 43167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("8") 4317353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-2'), Decimal('3')) 4318353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-8") 4319353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('2'), Decimal('-3')) 43207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.125") 4321353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('1.7'), Decimal('8')) 43227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("69.7575744") 4323353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('10'), Decimal('0.301029996')) 4324353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2.00000000") 4325353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('Infinity'), Decimal('-1')) 43267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0") 4327353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('Infinity'), Decimal('0')) 43287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 4329353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('Infinity'), Decimal('1')) 43307c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("Infinity") 4331353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('-1')) 43327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0") 4333353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('0')) 43347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 4335353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('1')) 43367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-Infinity") 4337353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-Infinity'), Decimal('2')) 43387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("Infinity") 4339353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('0'), Decimal('0')) 43407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("NaN") 4341353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4342353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('3'), Decimal('7'), Decimal('16')) 4343353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("11") 4344353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16')) 4345353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-11") 4346353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16')) 4347353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 4348353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16')) 4349353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("11") 4350353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789')) 4351353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("11729830") 4352353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729')) 4353353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-0") 4354353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537')) 4355353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1") 43567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 43577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__pow__(b, modulo, context=self) 43587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 43597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def quantize(self, a, b): 436059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista """Returns a value equal to 'a' (rounded), having the exponent of 'b'. 43617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 43627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The coefficient of the result is derived from that of the left-hand 436359c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operand. It may be rounded using the current rounding setting (if the 43647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger exponent is being increased), multiplied by a positive power of ten (if 43657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger the exponent is being decreased), or is unchanged (if the exponent is 43667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger already equal to that of the right-hand operand). 43677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 43687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Unlike other operations, if the length of the coefficient after the 43697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger quantize operation would be greater than precision then an Invalid 437059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista operation condition is raised. This guarantees that, unless there is 437159c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista an error condition, the exponent of the result of a quantize is always 43727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger equal to that of the right-hand operand. 43737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 43747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Also unlike other operations, quantize will never raise Underflow, even 43757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if the result is subnormal and inexact. 43767c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 43779ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001')) 43787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.170") 43799ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01')) 43807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.17") 43819ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1')) 43827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.2") 43839ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0')) 43847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2") 43859ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1')) 43867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0E+1") 43879ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity')) 43887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-Infinity") 43899ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity')) 43907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("NaN") 43919ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1')) 43927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0") 43939ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5')) 43947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0E+5") 43959ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2')) 43967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("NaN") 43979ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2')) 43987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("NaN") 43999ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1')) 44007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("217.0") 44019ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0')) 44027c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("217") 44039ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1')) 44047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.2E+2") 44059ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2')) 44067c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2E+2") 44077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 44087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.quantize(b, context=self) 44097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4410353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def radix(self): 4411353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Just returns 10, as this is Decimal, :) 4412353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4413353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.radix() 4414353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("10") 4415353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4416353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return Decimal(10) 4417353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 44187c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def remainder(self, a, b): 44197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns the remainder from integer division. 44207c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 44217c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result is the residue of the dividend after the operation of 442259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista calculating integer division as described for divide-integer, rounded 44230d4c06e06e5ee1f3bb1fa8068114bd700d74864aNeal Norwitz to precision digits if necessary. The sign of the result, if 442459c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista non-zero, is the same as that of the original dividend. 44257c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 44267c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This operation will fail under the same conditions as integer division 44277c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (that is, if integer division on the same two operands would fail, the 44287c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger remainder cannot be calculated). 44297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 44309ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3')) 44317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.1") 44329ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('10'), Decimal('3')) 44337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 44349ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3')) 44357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-1") 44369ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1')) 44377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.2") 44389ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3')) 44397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.1") 44409ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3')) 44417c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.0") 44427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 44437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__mod__(b, context=self) 44447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 44457c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def remainder_near(self, a, b): 44467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns to be "a - b * n", where n is the integer nearest the exact 44477c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger value of "x / b" (if two integers are equally near then the even one 444859c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista is chosen). If the result is equal to 0 then its sign will be the 44497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger sign of a. 44507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 44517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger This operation will fail under the same conditions as integer division 44527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger (that is, if integer division on the same two operands would fail, the 44537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger remainder cannot be calculated). 44547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 44559ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3')) 44567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0.9") 44579ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6')) 44587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-2") 44599ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3')) 44607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 44619ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3')) 44627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-1") 44639ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1')) 44647c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.2") 44659ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3')) 44667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.1") 44679ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3')) 44687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0.3") 44697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 44707c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.remainder_near(b, context=self) 44717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4472353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def rotate(self, a, b): 4473353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a rotated copy of a, b times. 4474353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4475353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The coefficient of the result is a rotated copy of the digits in 4476353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista the coefficient of the first operand. The number of places of 4477353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rotation is taken from the absolute value of the second operand, 4478353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista with the rotation being to the left if the second operand is 4479353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista positive or to the right otherwise. 4480353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4481353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('34'), Decimal('8')) 4482353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("400000003") 4483353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('12'), Decimal('9')) 4484353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("12") 4485353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2')) 4486353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("891234567") 4487353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0')) 4488353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("123456789") 4489353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2')) 4490353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("345678912") 4491353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4492353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.rotate(b, context=self) 4493353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 44947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def same_quantum(self, a, b): 44957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Returns True if the two operands have the same exponent. 44967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 44977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The result is never affected by either the sign or the coefficient of 44987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger either operand. 44997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45009ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001')) 45017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger False 45029ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01')) 45037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger True 45049ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1')) 45057c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger False 45069ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf')) 45077c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger True 45087c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 45097c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.same_quantum(b) 45107c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4511353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def scaleb (self, a, b): 4512353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns the first operand after adding the second value its exp. 4513353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4514353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2')) 4515353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0.0750") 4516353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0')) 4517353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("7.50") 4518353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3')) 4519353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("7.50E+3") 4520353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4521353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.scaleb (b, context=self) 4522353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4523353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def shift(self, a, b): 4524353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Returns a shifted copy of a, b times. 4525353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4526353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista The coefficient of the result is a shifted copy of the digits 4527353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in the coefficient of the first operand. The number of places 4528353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to shift is taken from the absolute value of the second operand, 4529353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista with the shift being to the left if the second operand is 4530353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista positive or to the right otherwise. Digits shifted into the 4531353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coefficient are zeros. 4532353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4533353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('34'), Decimal('8')) 4534353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("400000000") 4535353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('12'), Decimal('9')) 4536353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("0") 4537353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2')) 4538353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1234567") 4539353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0')) 4540353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("123456789") 4541353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2')) 4542353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("345678900") 4543353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4544353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.shift(b, context=self) 4545353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 45467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def sqrt(self, a): 454759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista """Square root of a non-negative number to context precision. 45487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45497c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger If the result must be inexact, it is rounded using the round-half-even 45507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger algorithm. 45517c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45529ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('0')) 45537c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0") 45549ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('-0')) 45557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0") 45569ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('0.39')) 45577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.624499800") 45589ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('100')) 45597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("10") 45609ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('1')) 45617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1") 45629ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('1.0')) 45637c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.0") 45649ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('1.00')) 45657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.0") 45669ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('7')) 45677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2.64575131") 45689ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.sqrt(Decimal('10')) 45697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("3.16227766") 45709ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.prec 45716ea484582238a65d44a76e3a831e3ba7c77a1a25Raymond Hettinger 9 45727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 45737c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.sqrt(context=self) 45747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def subtract(self, a, b): 4576f33d01d304c349a7f555779c7c99930f1203adb7Georg Brandl """Return the difference between the two operands. 45777c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45789ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07')) 45797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.23") 45809ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30')) 45817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("0.00") 45829ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond Hettinger >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07')) 45837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-0.77") 45847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 45857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__sub__(b, context=self) 45867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def to_eng_string(self, a): 45887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Converts a number to a string, using scientific notation. 45897c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is not affected by the context. 45917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 45927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.to_eng_string(context=self) 45937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def to_sci_string(self, a): 45957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Converts a number to a string, using scientific notation. 45967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 45977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger The operation is not affected by the context. 45987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 45997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return a.__str__(context=self) 46007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4601353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_exact(self, a): 4602353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Rounds to an integer. 4603353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4604353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista When the operand has a negative exponent, the result is the same 4605353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista as using the quantize() operation using the given operand as the 4606353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista left-hand-operand, 1E+0 as the right-hand-operand, and the precision 4607353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of the operand as the precision setting; Inexact and Rounded flags 4608353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista are allowed in this operation. The rounding mode is taken from the 4609353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista context. 4610353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4611353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('2.1')) 4612353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("2") 4613353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('100')) 4614353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("100") 4615353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('100.0')) 4616353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("100") 4617353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('101.5')) 4618353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("102") 4619353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('-101.5')) 4620353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-102") 4621353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('10E+5')) 4622353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("1.0E+6") 4623353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77')) 4624353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("7.89E+77") 4625353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_exact(Decimal('-Inf')) 4626353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Decimal("-Infinity") 4627353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4628353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.to_integral_exact(context=self) 4629353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4630353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista def to_integral_value(self, a): 46317c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Rounds to an integer. 46327c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46337c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger When the operand has a negative exponent, the result is the same 46347c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger as using the quantize() operation using the given operand as the 46357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger left-hand-operand, 1E+0 as the right-hand-operand, and the precision 46367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger of the operand as the precision setting, except that no flags will 463759c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista be set. The rounding mode is taken from the context. 46387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4639353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('2.1')) 46407c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("2") 4641353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('100')) 46427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("100") 4643353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('100.0')) 46447c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("100") 4645353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('101.5')) 46467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("102") 4647353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('-101.5')) 46487c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-102") 4649353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('10E+5')) 46507c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("1.0E+6") 4651353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('7.89E+77')) 46527c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("7.89E+77") 4653353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista >>> ExtendedContext.to_integral_value(Decimal('-Inf')) 46547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Decimal("-Infinity") 46557c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 4656353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a.to_integral_value(context=self) 4657353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4658353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the method name changed, but we provide also the old one, for compatibility 4659353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista to_integral = to_integral_value 46607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerclass _WorkRep(object): 46627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __slots__ = ('sign','int','exp') 466317931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger # sign: 0 or 1 4664636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger # int: int or long 46657c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger # exp: None, int, or string 46667c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46677c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __init__(self, value=None): 46687c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger if value is None: 46697c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.sign = None 4670636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger self.int = 0 46717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.exp = None 467217931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger elif isinstance(value, Decimal): 467317931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger self.sign = value._sign 467472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista self.int = int(value._int) 46757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.exp = value._exp 467617931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger else: 467717931de110ac84e6b0eb88d77a95798f8670765aRaymond Hettinger # assert isinstance(value, tuple) 46787c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.sign = value[0] 46797c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.int = value[1] 46807c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger self.exp = value[2] 46817c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger def __repr__(self): 46837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return "(%r, %r, %r)" % (self.sign, self.int, self.exp) 46847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger __str__ = __repr__ 46867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4689e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batistadef _normalize(op1, op2, prec = 0): 46907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """Normalizes op1, op2 to have the same exp and length of coefficient. 46917c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 46927c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger Done during addition. 46937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger """ 4694353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if op1.exp < op2.exp: 46957c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger tmp = op2 46967c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger other = op1 46977c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger else: 46987c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger tmp = op1 46997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger other = op2 47007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4701353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1). 4702353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Then adding 10**exp to tmp has the same effect (after rounding) 4703353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # as adding any positive quantity smaller than 10**exp; similarly 4704353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for subtraction. So if other is smaller than 10**exp we replace 4705353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # it with 10**exp. This avoids tmp.exp - other.exp getting too large. 4706e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista tmp_len = len(str(tmp.int)) 4707e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista other_len = len(str(other.int)) 4708e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista exp = tmp.exp + min(-1, tmp_len - prec - 2) 4709e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista if other_len + other.exp - 1 < exp: 4710e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista other.int = 1 4711e64acfad3dd13809943eecf50f11c40c510dedbeFacundo Batista other.exp = exp 4712636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 4713353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista tmp.int *= 10 ** (tmp.exp - other.exp) 4714353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista tmp.exp = other.exp 47157c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger return op1, op2 47167c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 4717353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista##### Integer arithmetic functions used by ln, log10, exp and __pow__ ##### 4718353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4719353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# This function from Tim Peters was taken from here: 4720353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# http://mail.python.org/pipermail/python-list/1999-July/007758.html 4721353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# The correction being in the function definition is for speed, and 4722353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# the whole function is not resolved with math.log because of avoiding 4723353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista# the use of floats. 4724353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _nbits(n, correction = { 4725353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '0': 4, '1': 3, '2': 2, '3': 2, 4726353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '4': 1, '5': 1, '6': 1, '7': 1, 4727353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '8': 0, '9': 0, 'a': 0, 'b': 0, 4728353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 'c': 0, 'd': 0, 'e': 0, 'f': 0}): 4729353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Number of bits in binary representation of the positive integer n, 4730353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista or 0 if n == 0. 4731353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4732353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n < 0: 4733353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise ValueError("The argument to _nbits should be nonnegative.") 4734353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista hex_n = "%x" % n 4735353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return 4*len(hex_n) - correction[hex_n[0]] 4736353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4737353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _sqrt_nearest(n, a): 4738353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Closest integer to the square root of the positive integer n. a is 4739353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista an initial approximation to the square root. Any positive integer 4740353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista will do for a, but the closer a is to the square root of n the 4741353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista faster convergence will be. 4742353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4743353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4744353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if n <= 0 or a <= 0: 4745353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise ValueError("Both arguments to _sqrt_nearest should be positive.") 4746353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4747353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b=0 4748353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while a != b: 4749353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b, a = a, a--n//a>>1 4750353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return a 4751353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4752353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _rshift_nearest(x, shift): 4753353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given an integer x and a nonnegative integer shift, return closest 4754353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista integer to x / 2**shift; use round-to-even in case of a tie. 4755353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4756353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4757353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b, q = 1L << shift, x >> shift 4758353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return q + (2*(x & (b-1)) + (q&1) > b) 4759353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4760353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _div_nearest(a, b): 4761353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Closest integer to a/b, a and b positive integers; rounds to even 4762353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in the case of a tie. 4763353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4764353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 4765353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q, r = divmod(a, b) 4766353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return q + (2*r + (q&1) > b) 4767353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4768353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _ilog(x, M, L = 8): 4769353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Integer approximation to M*log(x/M), with absolute error boundable 4770353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in terms only of x/M. 4771353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4772353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Given positive integers x and M, return an integer approximation to 4773353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista M * log(x/M). For L = 8 and 0.1 <= x/M <= 10 the difference 4774353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista between the approximation and the exact result is at most 22. For 4775353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15. In 4776353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista both cases these are upper bounds on the error; it will usually be 4777353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista much smaller.""" 4778353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4779353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # The basic algorithm is the following: let log1p be the function 4780353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log1p(x) = log(1+x). Then log(x/M) = log1p((x-M)/M). We use 4781353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # the reduction 4782353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4783353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log1p(y) = 2*log1p(y/(1+sqrt(1+y))) 4784353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4785353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # repeatedly until the argument to log1p is small (< 2**-L in 4786353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # absolute value). For small y we can use the Taylor series 4787353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expansion 4788353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4789353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log1p(y) ~ y - y**2/2 + y**3/3 - ... - (-y)**T/T 4790353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4791353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # truncating at T such that y**T is small enough. The whole 4792353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # computation is carried out in a form of fixed-point arithmetic, 4793353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # with a real number z being represented by an integer 4794353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # approximation to z*M. To avoid loss of precision, the y below 4795353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # is actually an integer approximation to 2**R*y*M, where R is the 4796353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # number of reductions performed so far. 4797353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4798353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = x-M 4799353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # argument reduction; R = number of reductions performed 4800353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R = 0 4801353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista while (R <= L and long(abs(y)) << L-R >= M or 4802353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R > L and abs(y) >> R-L >= M): 4803353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(long(M*y) << 1, 4804353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista M + _sqrt_nearest(M*(M+_rshift_nearest(y, R)), M)) 4805353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R += 1 4806353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4807353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Taylor series with T terms 4808353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista T = -int(-10*len(str(M))//(3*L)) 4809353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista yshift = _rshift_nearest(y, R) 4810353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista w = _div_nearest(M, T) 4811353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for k in xrange(T-1, 0, -1): 4812353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista w = _div_nearest(M, k) - _div_nearest(yshift*w, M) 4813353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4814353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(w*y, M) 4815353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4816353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dlog10(c, e, p): 4817353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers c, e and p with c > 0, p >= 0, compute an integer 4818353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista approximation to 10**p * log10(c*10**e), with an absolute error of 4819353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista at most 1. Assumes that c*10**e is not exactly 1.""" 4820353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4821353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # increase precision by 2; compensate for this by dividing 4822353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # final result by 100 4823353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p += 2 4824353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4825353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # write c*10**e as d*10**f with either: 4826353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # f >= 0 and 1 <= d <= 10, or 4827353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # f <= 0 and 0.1 <= d <= 1. 4828353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Thus for c*10**e close to 1, f = 0 4829353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = len(str(c)) 4830353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f = e+l - (e+l >= 1) 4831353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4832353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if p > 0: 4833353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista M = 10**p 4834353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista k = e+p-f 4835353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if k >= 0: 4836353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c *= 10**k 4837353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 4838353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = _div_nearest(c, 10**-k) 4839353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4840353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = _ilog(c, M) # error < 5 + 22 = 27 4841be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista log_10 = _log10_digits(p) # error < 1 4842353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = _div_nearest(log_d*M, log_10) 4843353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_tenpower = f*M # exact 4844353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 4845353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = 0 # error < 2.31 4846353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_tenpower = div_nearest(f, 10**-p) # error < 0.5 4847353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4848353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(log_tenpower+log_d, 100) 4849353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4850353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dlog(c, e, p): 4851353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers c, e and p with c > 0, compute an integer 4852353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista approximation to 10**p * log(c*10**e), with an absolute error of 4853353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista at most 1. Assumes that c*10**e is not exactly 1.""" 4854353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4855353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Increase precision by 2. The precision increase is compensated 4856353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # for at the end with a division by 100. 4857353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p += 2 4858353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4859353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rewrite c*10**e as d*10**f with either f >= 0 and 1 <= d <= 10, 4860353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # or f <= 0 and 0.1 <= d <= 1. Then we can compute 10**p * log(c*10**e) 4861353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # as 10**p * log(d) + 10**p*f * log(10). 4862353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista l = len(str(c)) 4863353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f = e+l - (e+l >= 1) 4864353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4865353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute approximation to 10**p*log(d), with error < 27 4866353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if p > 0: 4867353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista k = e+p-f 4868353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if k >= 0: 4869353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c *= 10**k 4870353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 4871353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista c = _div_nearest(c, 10**-k) # error of <= 0.5 in c 4872353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4873353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # _ilog magnifies existing error in c by a factor of at most 10 4874353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = _ilog(c, 10**p) # error < 5 + 22 = 27 4875353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 4876353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # p <= 0: just approximate the whole thing by 0; error < 2.31 4877353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista log_d = 0 4878353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4879be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute approximation to f*10**p*log(10), with error < 11. 4880353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if f: 4881be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista extra = len(str(abs(f)))-1 4882be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if p + extra >= 0: 4883be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # error in f * _log10_digits(p+extra) < |f| * 1 = |f| 4884be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # after division, error < |f|/10**extra + 0.5 < 10 + 0.5 < 11 4885be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista f_log_ten = _div_nearest(f*_log10_digits(p+extra), 10**extra) 4886353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 4887353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f_log_ten = 0 4888353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 4889353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista f_log_ten = 0 4890353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4891be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # error in sum < 11+27 = 38; error after division < 0.38 + 0.5 < 1 4892353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(f_log_ten + log_d, 100) 4893353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4894be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batistaclass _Log10Memoize(object): 4895be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista """Class to compute, store, and allow retrieval of, digits of the 4896be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista constant log(10) = 2.302585.... This constant is needed by 4897be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__.""" 4898be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista def __init__(self): 4899be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista self.digits = "23025850929940456840179914546843642076011014886" 4900be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 4901be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista def getdigits(self, p): 4902be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista """Given an integer p >= 0, return floor(10**p)*log(10). 4903be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 4904be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista For example, self.getdigits(3) returns 2302. 4905be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista """ 4906be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # digits are stored as a string, for quick conversion to 4907be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # integer in the case that we've already computed enough 4908be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # digits; the stored digits should always be correct 4909be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # (truncated, not rounded to nearest). 4910be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if p < 0: 4911be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista raise ValueError("p should be nonnegative") 4912be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 4913be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if p >= len(self.digits): 4914be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute p+3, p+6, p+9, ... digits; continue until at 4915be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # least one of the extra digits is nonzero 4916be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista extra = 3 4917be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista while True: 4918be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute p+extra digits, correct to within 1ulp 4919be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista M = 10**(p+extra+2) 4920be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista digits = str(_div_nearest(_ilog(10*M, M), 100)) 4921be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista if digits[-extra:] != '0'*extra: 4922be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista break 4923be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista extra += 3 4924be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # keep all reliable digits so far; remove trailing zeros 4925be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # and next nonzero digit 4926be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista self.digits = digits.rstrip('0')[:-1] 4927be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista return int(self.digits[:p+1]) 4928be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 4929be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista_log10_digits = _Log10Memoize().getdigits 4930be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista 4931353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _iexp(x, M, L=8): 4932353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers x and M, M > 0, such that x/M is small in absolute 4933353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista value, compute an integer approximation to M*exp(x/M). For 0 <= 4934353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista x/M <= 2.4, the absolute error in the result is bounded by 60 (and 4935353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista is usually much smaller).""" 4936353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4937353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Algorithm: to compute exp(z) for a real number z, first divide z 4938353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # by a suitable power R of 2 so that |z/2**R| < 2**-L. Then 4939353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor 4940353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # series 4941353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4942353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expm1(x) = x + x**2/2! + x**3/3! + ... 4943353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4944353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Now use the identity 4945353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4946353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expm1(2x) = expm1(x)*(expm1(x)+2) 4947353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # 4948353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # R times to compute the sequence expm1(z/2**R), 4949353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # expm1(z/2**(R-1)), ... , exp(z/2), exp(z). 4950353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4951353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Find R such that x/2**R/M <= 2**-L 4952353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista R = _nbits((long(x)<<L)//M) 4953353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4954353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Taylor series. (2**L)**T > M 4955353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista T = -int(-10*len(str(M))//(3*L)) 4956353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(x, T) 4957353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Mshift = long(M)<<R 4958353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for i in xrange(T-1, 0, -1): 4959353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(x*(Mshift + y), Mshift * i) 4960353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4961353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Expansion 4962353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista for k in xrange(R-1, -1, -1): 4963353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista Mshift = long(M)<<(k+2) 4964353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = _div_nearest(y*(y+Mshift), Mshift) 4965353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4966353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return M+y 4967353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4968353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dexp(c, e, p): 4969353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute an approximation to exp(c*10**e), with p decimal places of 4970353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista precision. 4971353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4972be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista Returns integers d, f such that: 4973353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4974353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 10**(p-1) <= d <= 10**p, and 4975353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (d-1)*10**f < exp(c*10**e) < (d+1)*10**f 4976353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4977353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista In other words, d*10**f is an approximation to exp(c*10**e) with p 4978353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista digits of precision, and with an error in d of at most 1. This is 4979353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista almost, but not quite, the same as the error being < 1ulp: when d 4980353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista = 10**(p-1) the error could be up to 10 ulp.""" 4981353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4982353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we'll call iexp with M = 10**(p+2), giving p+3 digits of precision 4983353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista p += 2 4984353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4985be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute log(10) with extra precision = adjusted exponent of c*10**e 4986353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista extra = max(0, e + len(str(c)) - 1) 4987353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista q = p + extra 4988353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4989be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista # compute quotient c*10**e/(log(10)) = c*10**(e+q)/(log(10)*10**q), 4990353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # rounding down 4991353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista shift = e+q 4992353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 4993353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista cshift = c*10**shift 4994353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 4995353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista cshift = c//10**-shift 4996be6c7ba72ad2bc3087a7df20871387b11756e140Facundo Batista quot, rem = divmod(cshift, _log10_digits(q)) 4997353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 4998353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # reduce remainder back to original precision 4999353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista rem = _div_nearest(rem, 10**extra) 5000353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5001353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # error in result of _iexp < 120; error after division < 0.62 5002353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3 5003353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5004353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _dpower(xc, xe, yc, ye, p): 5005353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and 5006353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that: 5007353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5008353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 10**(p-1) <= c <= 10**p, and 5009353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista (c-1)*10**e < x**y < (c+1)*10**e 5010353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5011353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista in other words, c*10**e is an approximation to x**y with p digits 5012353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista of precision, and with an error in c of at most 1. (This is 5013353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista almost, but not quite, the same as the error being < 1ulp: when c 5014353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista == 10**(p-1) we can only guarantee error < 10ulp.) 5015353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5016353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista We assume that: x is positive and not equal to 1, and y is nonzero. 5017353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """ 5018353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5019353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # Find b such that 10**(b-1) <= |y| <= 10**b 5020353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista b = len(str(abs(yc))) + ye 5021353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5022353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point 5023353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista lxc = _dlog(xc, xe, p+b+1) 5024353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5025353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1) 5026353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista shift = ye-b 5027353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if shift >= 0: 5028353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista pc = lxc*yc*10**shift 5029353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5030353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista pc = _div_nearest(lxc*yc, 10**-shift) 5031353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5032353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if pc == 0: 5033353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # we prefer a result that isn't exactly 1; this makes it 5034353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista # easier to compute a correctly rounded result in __pow__ 5035353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1: 5036353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = 10**(p-1)+1, 1-p 5037353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5038353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = 10**p-1, -p 5039353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista else: 5040353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff, exp = _dexp(pc, -(p+1), p+1) 5041353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista coeff = _div_nearest(coeff, 10) 5042353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista exp += 1 5043353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5044353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return coeff, exp 5045353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 5046353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _log10_lb(c, correction = { 5047353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '1': 100, '2': 70, '3': 53, '4': 40, '5': 31, 5048353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista '6': 23, '7': 16, '8': 10, '9': 5}): 5049353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista """Compute a lower bound for 100*log10(c) for a positive integer c.""" 5050353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if c <= 0: 5051353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise ValueError("The argument to _log10_lb should be nonnegative.") 5052353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista str_c = str(c) 5053353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista return 100*len(str_c) - correction[str_c[0]] 5054353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista 505559c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Helper Functions #################################################### 50567c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 5057353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batistadef _convert_other(other, raiseit=False): 5058636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger """Convert other to Decimal. 5059636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 5060636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger Verifies that it's ok to use in an implicit construction. 5061636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger """ 5062636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(other, Decimal): 5063636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return other 5064636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger if isinstance(other, (int, long)): 5065636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger return Decimal(other) 5066353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista if raiseit: 5067353750c405c9099d0be69c6af1d17037b38c4ddfFacundo Batista raise TypeError("Unable to convert %s to Decimal" % other) 5068267b868f23a85c6a63a06452c85487355cf9ab8aRaymond Hettinger return NotImplemented 5069636a6b100fe6083388bc5315758326078abe65b4Raymond Hettinger 507059c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### Setup Specific Contexts ############################################ 50717c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50727c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# The default context prototype used by Context() 5073fed52963fcf97154e0b7d1d82514767d95eb27e5Raymond Hettinger# Is mutable, so that new contexts can have different default values 50747c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50757c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond HettingerDefaultContext = Context( 50766ea484582238a65d44a76e3a831e3ba7c77a1a25Raymond Hettinger prec=28, rounding=ROUND_HALF_EVEN, 5077bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps=[DivisionByZero, Overflow, InvalidOperation], 5078bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger flags=[], 507999148e7eaad7741fbfb0822009b7c9b216ecc227Raymond Hettinger Emax=999999999, 508099148e7eaad7741fbfb0822009b7c9b216ecc227Raymond Hettinger Emin=-999999999, 5081e0f1581babe682552d7eadc4e54636b1c2775f0bRaymond Hettinger capitals=1 50827c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger) 50837c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50847c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# Pre-made alternate contexts offered by the specification 50857c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# Don't change these; the user should be able to select these 50867c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# contexts and be able to reproduce results from other implementations 50877c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# of the spec. 50887c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50899ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond HettingerBasicContext = Context( 50907c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger prec=9, rounding=ROUND_HALF_UP, 5091bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow], 5092bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger flags=[], 50937c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger) 50947c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 50959ec3e3b6eb4b36870bd28682e6c7a6c36911e5e0Raymond HettingerExtendedContext = Context( 50966ea484582238a65d44a76e3a831e3ba7c77a1a25Raymond Hettinger prec=9, rounding=ROUND_HALF_EVEN, 5097bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger traps=[], 5098bf4406971ce34434ed914d17fab70370948d2aabRaymond Hettinger flags=[], 50997c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger) 51007c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51017c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 510259c5884b4c37b6288911ff2aed218cfbb41f06abFacundo Batista##### crud for parsing strings ############################################# 51037c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerimport re 51047c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 510572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# Regular expression used for parsing numeric strings. Additional 510672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# comments: 510772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 510872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 1. Uncomment the two '\s*' lines to allow leading and/or trailing 510972bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# whitespace. But note that the specification disallows whitespace in 511072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# a numeric string. 511172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 511272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 2. For finite numbers (not infinities and NaNs) the body of the 511372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# number between the optional sign and the optional exponent must have 511472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# at least one decimal digit, possibly after the decimal point. The 511572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# lookahead expression '(?=\d|\.\d)' checks this. 511672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# 511772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# As the flag UNICODE is not enabled here, we're explicitly avoiding any 511872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# other meaning for \d than the numbers [0-9]. 51197c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 512072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batistaimport re 512172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista_parser = re.compile(r""" # A numeric string consists of: 51227c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# \s* 512372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista (?P<sign>[-+])? # an optional sign, followed by either... 51247c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ( 512572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista (?=\d|\.\d) # ...a number (with at least one digit) 512672bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista (?P<int>\d*) # consisting of a (possibly empty) integer part 512772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista (\.(?P<frac>\d*))? # followed by an optional fractional part 512872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista (E(?P<exp>[-+]?\d+))? # followed by an optional exponent, or... 51297c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger | 513072bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista Inf(inity)? # ...an infinity, or... 513172bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista | 513272bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista (?P<signal>s)? # ...an (optionally signaling) 513372bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista NaN # NaN 513472bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista (?P<diag>\d*) # with (possibly empty) diagnostic information. 51357c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger ) 51367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger# \s* 51377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger $ 513872bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista""", re.VERBOSE | re.IGNORECASE).match 51397c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51402ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista_all_zeros = re.compile('0*$').match 51412ec7415db5ad63c4e4b27ee793214071454f6fe5Facundo Batista_exact_half = re.compile('50*$').match 51427c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerdel re 51437c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51440d4c06e06e5ee1f3bb1fa8068114bd700d74864aNeal Norwitz 514572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista##### Useful Constants (internal use only) ################################ 51467c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 514772bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# Reusable defaults 514872bc54faed8f12dc89ecc989fc7921002db3427fFacundo BatistaInf = Decimal('Inf') 514972bc54faed8f12dc89ecc989fc7921002db3427fFacundo BatistanegInf = Decimal('-Inf') 515072bc54faed8f12dc89ecc989fc7921002db3427fFacundo BatistaNaN = Decimal('NaN') 515172bc54faed8f12dc89ecc989fc7921002db3427fFacundo BatistaDec_0 = Decimal(0) 515272bc54faed8f12dc89ecc989fc7921002db3427fFacundo BatistaDec_p1 = Decimal(1) 515372bc54faed8f12dc89ecc989fc7921002db3427fFacundo BatistaDec_n1 = Decimal(-1) 51547c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 515572bc54faed8f12dc89ecc989fc7921002db3427fFacundo Batista# Infsign[sign] is infinity w/ that sign 515672bc54faed8f12dc89ecc989fc7921002db3427fFacundo BatistaInfsign = (Inf, negInf) 51577c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51587c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51597c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger 51607c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettingerif __name__ == '__main__': 51617c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger import doctest, sys 51627c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger doctest.testmod(sys.modules[__name__]) 5163