10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Copyright (c) 2004 Python Software Foundation. 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# All rights reserved. 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Written by Eric Price <eprice at tjhsst.edu> 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# and Facundo Batista <facundo at taniquetil.com.ar> 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# and Raymond Hettinger <python at rcn.com> 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# and Aahz <aahz at pobox.com> 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# and Tim Peters 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This module is currently Py2.3 compatible and should be kept that way 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# unless a major compelling advantage arises. IOW, 2.3 compatibility is 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# strongly preferred, but not guaranteed. 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Also, this module should be kept in sync with the latest updates of 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# the IBM specification as it evolves. Those updates will be treated 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# as bug fixes (deviation from the spec is a compatibility, usability 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# bug) and will be backported. At this point the spec is stabilizing 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# and the updates are becoming fewer, smaller, and less significant. 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 210a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThis is a Py2.3 implementation of decimal floating point arithmetic based on 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaothe General Decimal Arithmetic Specification: 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao http://speleotrove.com/decimal/decarith.html 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoand IEEE standard 854-1987: 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 300a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal floating point has finite precision with arbitrarily large bounds. 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 320a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThe purpose of this module is to support arithmetic using familiar 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"schoolhouse" rules and to avoid some of the tricky representation 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoissues associated with binary floating point. The package is especially 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gaouseful for financial applications or for contexts where users have 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexpectations that are at odds with binary floating point (for instance, 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoin binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoof the expected Decimal('0.00') returned by decimal floating point). 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 400a8c90248264a8b26970b4473770bcc3df8515fJosh GaoHere are some examples of using the decimal module: 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> from decimal import * 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> setcontext(ExtendedContext) 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> Decimal(0) 450a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('0') 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> Decimal('1') 470a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('1') 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> Decimal('-.0123') 490a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('-0.0123') 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> Decimal(123456) 510a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('123456') 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> Decimal('123.45e12345678901234567890') 530a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('1.2345E+12345678901234567892') 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> Decimal('1.33') + Decimal('1.27') 550a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('2.60') 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41') 570a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('-2.20') 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> dig = Decimal(1) 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print dig / Decimal(3) 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao0.333333333 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> getcontext().prec = 18 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print dig / Decimal(3) 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao0.333333333333333333 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print dig.sqrt() 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao1 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print Decimal(3).sqrt() 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao1.73205080756887729 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print Decimal(3) ** 123 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao4.85192780976896427E+58 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> inf = Decimal(1) / Decimal(0) 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print inf 720a8c90248264a8b26970b4473770bcc3df8515fJosh GaoInfinity 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> neginf = Decimal(-1) / Decimal(0) 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print neginf 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao-Infinity 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print neginf + inf 770a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNaN 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print neginf * inf 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao-Infinity 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print dig / 0 810a8c90248264a8b26970b4473770bcc3df8515fJosh GaoInfinity 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> getcontext().traps[DivisionByZero] = 1 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print dig / 0 840a8c90248264a8b26970b4473770bcc3df8515fJosh GaoTraceback (most recent call last): 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 880a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDivisionByZero: x / 0 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> c = Context() 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> c.traps[InvalidOperation] = 0 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print c.flags[InvalidOperation] 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao0 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> c.divide(Decimal(0), Decimal(0)) 940a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDecimal('NaN') 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> c.traps[InvalidOperation] = 1 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print c.flags[InvalidOperation] 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao1 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> c.flags[InvalidOperation] = 0 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print c.flags[InvalidOperation] 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao0 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print c.divide(Decimal(0), Decimal(0)) 1020a8c90248264a8b26970b4473770bcc3df8515fJosh GaoTraceback (most recent call last): 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 1060a8c90248264a8b26970b4473770bcc3df8515fJosh GaoInvalidOperation: 0 / 0 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print c.flags[InvalidOperation] 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao1 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> c.flags[InvalidOperation] = 0 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> c.traps[InvalidOperation] = 0 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print c.divide(Decimal(0), Decimal(0)) 1120a8c90248264a8b26970b4473770bcc3df8515fJosh GaoNaN 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> print c.flags[InvalidOperation] 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao1 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao>>> 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__all__ = [ 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Two major classes 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Decimal', 'Context', 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Contexts 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'DefaultContext', 'BasicContext', 'ExtendedContext', 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Exceptions 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero', 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow', 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Constants for use in setting up contexts 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING', 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', 'ROUND_05UP', 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Functions for manipulating contexts 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'setcontext', 'getcontext', 'localcontext' 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao] 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__version__ = '1.70' # Highest version of the spec this complies with 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport copy as _copy 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport math as _math 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport numbers as _numbers 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry: 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from collections import namedtuple as _namedtuple 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent') 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ImportError: 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao DecimalTuple = lambda *args: args 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Rounding 1500a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_DOWN = 'ROUND_DOWN' 1510a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_HALF_UP = 'ROUND_HALF_UP' 1520a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_HALF_EVEN = 'ROUND_HALF_EVEN' 1530a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_CEILING = 'ROUND_CEILING' 1540a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_FLOOR = 'ROUND_FLOOR' 1550a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_UP = 'ROUND_UP' 1560a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_HALF_DOWN = 'ROUND_HALF_DOWN' 1570a8c90248264a8b26970b4473770bcc3df8515fJosh GaoROUND_05UP = 'ROUND_05UP' 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Errors 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass DecimalException(ArithmeticError): 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Base exception class. 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Used exceptions derive from this. 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If an exception derives from another exception besides this (such as 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Underflow (Inexact, Rounded, Subnormal) that indicates that it is only 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao called if the others are present. This isn't actually used for 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao anything, though. 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao handle -- Called when context._raise_error is called and the 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao trap_enabler is not set. First argument is self, second is the 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context. More arguments can be given, those being after 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the explanation in _raise_error (For example, 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(NewError, '(-x)!', self._sign) would 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao call NewError().handle(context, self._sign).) 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao To define a new exception, it should be sufficient to have it derive 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from DecimalException. 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, *args): 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Clamped(DecimalException): 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Exponent of a 0 changed to fit bounds. 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals clamped if the exponent of a result has been 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao altered in order to fit the constraints of a specific concrete 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao representation. This may occur when the exponent of a zero result would 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be outside the bounds of a representation, or when a large normal 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao number would have an encoded exponent that cannot be represented. In 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao this latter case, the exponent is reduced to fit and the corresponding 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao number of zero digits are appended to the coefficient ("fold-down"). 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass InvalidOperation(DecimalException): 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """An invalid operation was performed. 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Various bad things cause this: 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Something creates a signaling NaN 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -INF + INF 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0 * (+-)INF 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (+-)INF / (+-)INF 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x % 0 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (+-)INF % x 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x._rescale( non-integer ) 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sqrt(-x) , x > 0 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0 ** 0 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x ** (non-integer) 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x ** (+-)INF 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao An operand is invalid 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result of the operation after these is a quiet positive NaN, 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except when the cause is a signaling NaN, in which case the result is 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao also a quiet NaN, but with the original sign, and an optional 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao diagnostic information. 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, *args): 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if args: 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True) 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix_nan(context) 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NaN 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ConversionSyntax(InvalidOperation): 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Trying to convert badly formed string. 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals invalid-operation if an string is being 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao converted to a number and it does not conform to the numeric string 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao syntax. The result is [0,qNaN]. 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, *args): 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NaN 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass DivisionByZero(DecimalException, ZeroDivisionError): 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Division by 0. 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals division-by-zero if division of a finite number 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao by zero was attempted (during a divide-integer or divide operation, or a 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao power operation with negative right-hand operand), and the dividend was 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao not zero. 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result of the operation is [sign,inf], where sign is the exclusive 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao or of the signs of the operands for divide, or is 1 for an odd power of 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -0, for power. 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, sign, *args): 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[sign] 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass DivisionImpossible(InvalidOperation): 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Cannot perform the division adequately. 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals invalid-operation if the integer result of a 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao divide-integer or remainder operation had too many digits (would be 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao longer than precision). The result is [0,qNaN]. 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, *args): 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NaN 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass DivisionUndefined(InvalidOperation, ZeroDivisionError): 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Undefined result of division. 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals invalid-operation if division by zero was 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao attempted (during a divide-integer, divide, or remainder operation), and 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the dividend is also zero. The result is [0,qNaN]. 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, *args): 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NaN 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Inexact(DecimalException): 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Had to round, losing information. 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals inexact whenever the result of an operation is 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao not exact (that is, it needed to be rounded and any discarded digits 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao were non-zero), or if an overflow or underflow condition occurs. The 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result in all cases is unchanged. 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The inexact signal may be tested (or trapped) to determine if a given 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation (or sequence of operations) was inexact. 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass InvalidContext(InvalidOperation): 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Invalid context. Unknown rounding, for example. 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals invalid-operation if an invalid context was 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao detected during an operation. This can occur if contexts are not checked 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao on creation and either the precision exceeds the capability of the 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao underlying concrete representation or an unknown or unsupported rounding 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao was specified. These aspects of the context need only be checked when 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the values are required to be used. The result is [0,qNaN]. 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, *args): 2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NaN 2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Rounded(DecimalException): 3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Number got rounded (not necessarily changed during rounding). 3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals rounded whenever the result of an operation is 3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounded (that is, some zero or non-zero digits were discarded from the 3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coefficient), or if an overflow or underflow condition occurs. The 3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result in all cases is unchanged. 3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The rounded signal may be tested (or trapped) to determine if a given 3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation (or sequence of operations) caused a loss of precision. 3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Subnormal(DecimalException): 3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Exponent < Emin before rounding. 3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals subnormal whenever the result of a conversion or 3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation is subnormal (that is, its adjusted exponent is less than 3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Emin, before any rounding). The result in all cases is unchanged. 3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The subnormal signal may be tested (or trapped) to determine if a given 3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao or operation (or sequence of operations) yielded a subnormal result. 3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Overflow(Inexact, Rounded): 3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Numerical overflow. 3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals overflow if the adjusted exponent of a result 3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (from a conversion or from an operation that is not an attempt to divide 3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao by zero), after rounding, would be greater than the largest value that 3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao can be handled by the implementation (the value Emax). 3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result depends on the rounding mode: 3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao For round-half-up and round-half-even (and for round-half-down and 3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao round-up, if implemented), the result of the operation is [sign,inf], 3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao where sign is the sign of the intermediate result. For round-down, the 3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result is the largest finite number that can be represented in the 3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao current precision, with the sign of the intermediate result. For 3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao round-ceiling, the result is the same as for round-down if the sign of 3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the intermediate result is 1, or is [0,inf] otherwise. For round-floor, 3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the result is the same as for round-down if the sign of the intermediate 3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded 3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao will also be raised. 3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def handle(self, context, sign, *args): 3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN, 3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_HALF_DOWN, ROUND_UP): 3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[sign] 3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sign == 0: 3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context.rounding == ROUND_CEILING: 3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[sign] 3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(sign, '9'*context.prec, 3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.Emax-context.prec+1) 3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sign == 1: 3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context.rounding == ROUND_FLOOR: 3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[sign] 3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(sign, '9'*context.prec, 3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.Emax-context.prec+1) 3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Underflow(Inexact, Rounded, Subnormal): 3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Numerical underflow with result rounded to 0. 3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This occurs and signals underflow if a result is inexact and the 3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao adjusted exponent of the result would be smaller (more negative) than 3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the smallest value that can be handled by the implementation (the value 3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Emin). That is, the result is both inexact and subnormal. 3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result after an underflow will be a subnormal number rounded, if 3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao necessary, so that its exponent is not less than Etiny. This may result 3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in 0 with the sign of the intermediate result and an exponent of Etiny. 3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao In all cases, Inexact, Rounded, and Subnormal will also be raised. 3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# List of public traps and flags 3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded, 3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Underflow, InvalidOperation, Subnormal] 3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Map conditions (per the spec) to signals 3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_condition_map = {ConversionSyntax:InvalidOperation, 3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao DivisionImpossible:InvalidOperation, 3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao DivisionUndefined:InvalidOperation, 3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao InvalidContext:InvalidOperation} 3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### Context Functions ################################################## 3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The getcontext() and setcontext() function manage access to a thread-local 3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# current context. Py2.4 offers direct support for thread locals. If that 3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# is not available, use threading.currentThread() which is slower but will 3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# work for older Pythons. If threads are not part of the build, create a 3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# mock threading object with threading.local() returning the module namespace. 3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry: 3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import threading 3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ImportError: 3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Python was compiled without threads; create a mock object instead 3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import sys 3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class MockThreading(object): 3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def local(self, sys=sys): 4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sys.modules[__name__] 4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao threading = MockThreading() 4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del sys, MockThreading 4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry: 4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao threading.local 4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept AttributeError: 4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # To fix reloading, force it to create a new context 4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Old contexts have different exceptions in their dicts, making problems. 4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(threading.currentThread(), '__decimal_context__'): 4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del threading.currentThread().__decimal_context__ 4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setcontext(context): 4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Set this thread's context to context.""" 4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context in (DefaultContext, BasicContext, ExtendedContext): 4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context.copy() 4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.clear_flags() 4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao threading.currentThread().__decimal_context__ = context 4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getcontext(): 4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns this thread's context. 4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If this thread does not yet have a context, returns 4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a new context and sets this thread's context. 4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao New contexts are copies of DefaultContext. 4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return threading.currentThread().__decimal_context__ 4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except AttributeError: 4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = Context() 4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao threading.currentThread().__decimal_context__ = context 4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context 4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoelse: 4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao local = threading.local() 4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(local, '__decimal_context__'): 4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del local.__decimal_context__ 4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getcontext(_local=local): 4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns this thread's context. 4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If this thread does not yet have a context, returns 4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a new context and sets this thread's context. 4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao New contexts are copies of DefaultContext. 4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _local.__decimal_context__ 4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except AttributeError: 4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = Context() 4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _local.__decimal_context__ = context 4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context 4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setcontext(context, _local=local): 4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Set this thread's context to context.""" 4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context in (DefaultContext, BasicContext, ExtendedContext): 4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context.copy() 4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.clear_flags() 4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _local.__decimal_context__ = context 4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del threading, local # Don't contaminate the namespace 4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef localcontext(ctx=None): 4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return a context manager for a copy of the supplied context 4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Uses a copy of the current context if no context is specified 4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The returned context manager creates a local decimal context 4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in a with statement: 4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def sin(x): 4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with localcontext() as ctx: 4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ctx.prec += 2 4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Rest of sin calculation algorithm 4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # uses a precision 2 greater than normal 4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return +s # Convert result to normal precision 4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def sin(x): 4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with localcontext(ExtendedContext): 4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Rest of sin calculation algorithm 4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # uses the Extended Context from the 4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # General Decimal Arithmetic Specification 4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return +s # Convert result to normal context 4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> setcontext(DefaultContext) 4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> print getcontext().prec 4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28 4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> with localcontext(): 4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... ctx = getcontext() 4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... ctx.prec += 2 4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... print ctx.prec 4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30 4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> with localcontext(ExtendedContext): 4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... print getcontext().prec 4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9 4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> print getcontext().prec 4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28 4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ctx is None: ctx = getcontext() 5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _ContextManager(ctx) 5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### Decimal class ####################################################### 5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Decimal(object): 5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Floating point class for decimal arithmetic.""" 5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __slots__ = ('_exp','_int','_sign', '_is_special') 5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Generally, the value of the Decimal instance is given by 5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (-1)**_sign * _int * 10**_exp 5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Special values are signified by _is_special == True 5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We're immutable, so use __new__ not __init__ 5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __new__(cls, value="0", context=None): 5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Create a decimal point instance. 5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal('3.14') # string input 5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3.14') 5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) 5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3.14') 5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal(314) # int or long 5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('314') 5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal(Decimal(314)) # another decimal instance 5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('314') 5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay 5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3.14') 5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Note that the coefficient, self._int, is actually stored as 5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # a string rather than as a tuple of digits. This speeds up 5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the "digits to integer" and "integer to digits" conversions 5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # that are used in almost every arithmetic operation on 5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Decimals. This is an internal detail: the as_tuple function 5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # and the Decimal constructor still deal with tuples of 5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # digits. 5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = object.__new__(cls) 5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # From a string 5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # REs insist on real strings, so we can too. 5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(value, basestring): 5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m = _parser(value.strip()) 5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if m is None: 5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(ConversionSyntax, 5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Invalid literal for Decimal: %r" % value) 5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if m.group('sign') == "-": 5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = 1 5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = 0 5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = m.group('int') 5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if intpart is not None: 5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # finite number 5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = m.group('frac') or '' 5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = int(m.group('exp') or '0') 5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = str(int(intpart+fracpart)) 5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = exp - len(fracpart) 5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = False 5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao diag = m.group('diag') 5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if diag is not None: 5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # NaN 5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = str(int(diag or '0')).lstrip('0') 5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if m.group('signal'): 5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = 'N' 5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = 'n' 5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # infinity 5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = '0' 5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = 'F' 5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = True 5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # From an integer 5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(value, (int,long)): 5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if value >= 0: 5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = 0 5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = 1 5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = 0 5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = str(abs(value)) 5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = False 5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # From another decimal 5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(value, Decimal): 5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = value._exp 5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = value._sign 5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = value._int 5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = value._is_special 5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # From an internal working value 5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(value, _WorkRep): 5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = value.sign 6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = str(value.int) 6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = int(value.exp) 6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = False 6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # tuple/list conversion (possibly from as_tuple()) 6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(value, (list,tuple)): 6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(value) != 3: 6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('Invalid tuple size in creation of Decimal ' 6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'from list or tuple. The list or tuple ' 6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'should have exactly three elements.') 6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # process sign. The isinstance test rejects floats 6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not (isinstance(value[0], (int, long)) and value[0] in (0,1)): 6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Invalid sign. The first value in the tuple " 6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "should be an integer; either 0 for a " 6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "positive number or 1 for a negative number.") 6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = value[0] 6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if value[2] == 'F': 6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # infinity: value[1] is ignored 6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = '0' 6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = value[2] 6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = True 6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # process and validate the digits in value[1] 6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits = [] 6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for digit in value[1]: 6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(digit, (int, long)) and 0 <= digit <= 9: 6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # skip leading zeros 6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if digits or digit != 0: 6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits.append(digit) 6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("The second value in the tuple must " 6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "be composed of integers in the range " 6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "0 through 9.") 6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if value[2] in ('n', 'N'): 6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # NaN: digits form the diagnostic 6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = ''.join(map(str, digits)) 6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = value[2] 6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = True 6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif isinstance(value[2], (int, long)): 6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # finite number: digits give the coefficient 6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = ''.join(map(str, digits or [0])) 6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = value[2] 6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = False 6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("The third value in the tuple must " 6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "be an integer, or one of the " 6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "strings 'F', 'n', 'N'.") 6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(value, float): 6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value = Decimal.from_float(value) 6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = value._exp 6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = value._sign 6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = value._int 6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = value._is_special 6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Cannot convert %r to Decimal" % value) 6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # @classmethod, but @decorator is not valid Python 2.3 syntax, so 6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # don't use it (see notes on Py2.3 compatibility at top of file) 6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def from_float(cls, f): 6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Converts a float to a decimal number, exactly. 6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Note that Decimal.from_float(0.1) is not the same as Decimal('0.1'). 6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Since 0.1 is not exactly representable in binary floating point, the 6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value is stored as the nearest representable value which is 6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0x1.999999999999ap-4. The exact equivalent of the value in decimal 6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is 0.1000000000000000055511151231257827021181583404541015625. 6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal.from_float(0.1) 6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.1000000000000000055511151231257827021181583404541015625') 6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal.from_float(float('nan')) 6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('NaN') 6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal.from_float(float('inf')) 6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('Infinity') 6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal.from_float(-float('inf')) 6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> Decimal.from_float(-0.0) 6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0') 6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(f, (int, long)): # handle integer inputs 6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cls(f) 6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _math.isinf(f) or _math.isnan(f): # raises TypeError if not a float 6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cls(repr(f)) 6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _math.copysign(1.0, f) == 1.0: 6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = 0 6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = 1 6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n, d = abs(f).as_integer_ratio() 6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao k = d.bit_length() - 1 6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = _dec_from_triple(sign, str(n*5**k), -k) 6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if cls is Decimal: 6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return result 6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cls(result) 6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from_float = classmethod(from_float) 6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _isnan(self): 7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns whether the number is not actually one. 7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0 if a number 7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1 if NaN 7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2 if sNaN 7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = self._exp 7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exp == 'n': 7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 1 7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif exp == 'N': 7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 2 7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _isinfinity(self): 7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns whether the number is infinite 7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0 if finite or not a number 7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1 if +INF 7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -1 if -INF 7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp == 'F': 7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -1 7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 1 7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _check_nans(self, other=None, context=None): 7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns whether the number is not actually one. 7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self, other are sNaN, signal 7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self, other are NaN return nan 7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Done before operations. 7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_is_nan = self._isnan() 7390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is None: 7400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_is_nan = False 7410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 7420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_is_nan = other._isnan() 7430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_nan or other_is_nan: 7450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 7460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 7470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_nan == 2: 7490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sNaN', 7500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self) 7510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_is_nan == 2: 7520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sNaN', 7530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other) 7540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_nan: 7550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._fix_nan(context) 7560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other._fix_nan(context) 7580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 7590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _compare_check_nans(self, other, context): 7610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Version of _check_nans used for the signaling comparisons 7620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao compare_signal, __le__, __lt__, __ge__, __gt__. 7630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Signal InvalidOperation if either self or other is a (quiet 7650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao or signaling) NaN. Signaling NaNs take precedence over quiet 7660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaNs. 7670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return 0 if neither operand is a NaN. 7690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 7710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 7720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 7730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 7750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.is_snan(): 7760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 7770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'comparison involving sNaN', 7780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self) 7790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif other.is_snan(): 7800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 7810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'comparison involving sNaN', 7820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other) 7830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self.is_qnan(): 7840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 7850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'comparison involving NaN', 7860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self) 7870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif other.is_qnan(): 7880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 7890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'comparison involving NaN', 7900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other) 7910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 7920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __nonzero__(self): 7940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is nonzero; otherwise return False. 7950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaNs and infinities are considered nonzero. 7970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 7980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._is_special or self._int != '0' 7990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _cmp(self, other): 8010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compare the two non-NaN decimal instances self and other. 8020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Returns -1 if self < other, 0 if self == other and 1 8040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self > other. This routine is for internal use only.""" 8050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 8070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_inf = self._isinfinity() 8080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_inf = other._isinfinity() 8090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_inf == other_inf: 8100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 8110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self_inf < other_inf: 8120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -1 8130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 8140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 1 8150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # check for zeros; Decimal('0') == Decimal('-0') 8170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 8180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 8190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 8200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 8210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -((-1)**other._sign) 8220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 8230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (-1)**self._sign 8240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If different signs, neg one is less 8260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._sign < self._sign: 8270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -1 8280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign < other._sign: 8290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 1 8300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_adjusted = self.adjusted() 8320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_adjusted = other.adjusted() 8330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_adjusted == other_adjusted: 8340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_padded = self._int + '0'*(self._exp - other._exp) 8350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_padded = other._int + '0'*(other._exp - self._exp) 8360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_padded == other_padded: 8370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 8380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self_padded < other_padded: 8390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -(-1)**self._sign 8400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 8410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (-1)**self._sign 8420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self_adjusted > other_adjusted: 8430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (-1)**self._sign 8440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: # self_adjusted < other_adjusted 8450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -((-1)**self._sign) 8460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Note: The Decimal standard doesn't cover rich comparisons for 8480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Decimals. In particular, the specification is silent on the 8490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # subject of what should happen for a comparison involving a NaN. 8500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We take the following approach: 8510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 8520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # == comparisons involving a quiet NaN always return False 8530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # != comparisons involving a quiet NaN always return True 8540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # == or != comparisons involving a signaling NaN signal 8550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # InvalidOperation, and return False or True as above if the 8560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # InvalidOperation is not trapped. 8570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # <, >, <= and >= comparisons involving a (quiet or signaling) 8580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # NaN signal InvalidOperation, and return False if the 8590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # InvalidOperation is not trapped. 8600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 8610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This behavior is designed to conform as closely as possible to 8620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # that specified by IEEE 754. 8630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __eq__(self, other, context=None): 8650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, allow_float=True) 8660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 8670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 8680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._check_nans(other, context): 8690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 8700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._cmp(other) == 0 8710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __ne__(self, other, context=None): 8730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, allow_float=True) 8740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 8750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 8760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._check_nans(other, context): 8770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return True 8780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._cmp(other) != 0 8790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __lt__(self, other, context=None): 8810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, allow_float=True) 8820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 8830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 8840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._compare_check_nans(other, context) 8850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 8860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 8870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._cmp(other) < 0 8880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __le__(self, other, context=None): 8900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, allow_float=True) 8910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 8920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 8930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._compare_check_nans(other, context) 8940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 8950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 8960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._cmp(other) <= 0 8970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __gt__(self, other, context=None): 8990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, allow_float=True) 9000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 9010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 9020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._compare_check_nans(other, context) 9030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 9040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 9050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._cmp(other) > 0 9060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __ge__(self, other, context=None): 9080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, allow_float=True) 9090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 9100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 9110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._compare_check_nans(other, context) 9120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 9130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 9140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._cmp(other) >= 0 9150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare(self, other, context=None): 9170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares one to another. 9180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -1 => a < b 9200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0 => a = b 9210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1 => a > b 9220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaN => one is NaN 9230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Like __cmp__, but returns Decimal instances. 9240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 9250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 9260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Compare(NaN, NaN) = NaN 9280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if (self._is_special or other and other._is_special): 9290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 9300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 9310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 9320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self._cmp(other)) 9340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 9360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """x.__hash__() <==> hash(x)""" 9370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Decimal integers must hash the same as the ints 9380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 9390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The hash of a nonspecial noninteger Decimal must depend only 9400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # on the value of that Decimal, and not on its representation. 9410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # For example: hash(Decimal('100E-1')) == hash(Decimal('10')). 9420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Equality comparisons involving signaling nans can raise an 9440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exception; since equality checks are implicitly and 9450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # unpredictably used when checking set and dict membership, we 9460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # prevent signaling nans from being used as set elements or 9470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # dict keys by making __hash__ raise an exception. 9480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 9490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.is_snan(): 9500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError('Cannot hash a signaling NaN value.') 9510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self.is_nan(): 9520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 0 to match hash(float('nan')) 9530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 9540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 9550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # values chosen to match hash(float('inf')) and 9560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # hash(float('-inf')). 9570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 9580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -271828 9590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 9600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 314159 9610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # In Python 2.7, we're allowing comparisons (but not 9630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # arithmetic operations) between floats and Decimals; so if 9640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # a Decimal instance is exactly representable as a float then 9650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # its hash should match that of the float. 9660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_as_float = float(self) 9670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if Decimal.from_float(self_as_float) == self: 9680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return hash(self_as_float) 9690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinteger(): 9710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op = _WorkRep(self.to_integral_value()) 9720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # to make computation feasible for Decimals with large 9730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exponent, we use the fact that hash(n) == hash(m) for 9740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # any two nonzero integers n and m such that (i) n and m 9750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # have the same sign, and (ii) n is congruent to m modulo 9760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 2**64-1. So we can replace hash((-1)**s*c*10**e) with 9770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # hash((-1)**s*c*pow(10, e, 2**64-1). 9780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return hash((-1)**op.sign*op.int*pow(10, op.exp, 2**64-1)) 9790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The value of a nonzero nonspecial Decimal instance is 9800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # faithfully represented by the triple consisting of its sign, 9810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # its adjusted exponent, and its coefficient with trailing 9820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # zeros removed. 9830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return hash((self._sign, 9840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp+len(self._int), 9850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int.rstrip('0'))) 9860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def as_tuple(self): 9880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Represents the number as a triple tuple. 9890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao To show the internals exactly as they are. 9910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 9920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp) 9930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 9950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Represents the number as an instance of Decimal.""" 9960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Invariant: eval(repr(d)) == d 9970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "Decimal('%s')" % str(self) 9980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __str__(self, eng=False, context=None): 10000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return string representation of the number in scientific notation. 10010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Captures all of the information in the underlying representation. 10030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 10040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = ['', '-'][self._sign] 10060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 10070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp == 'F': 10080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sign + 'Infinity' 10090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self._exp == 'n': 10100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sign + 'NaN' + self._int 10110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: # self._exp == 'N' 10120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sign + 'sNaN' + self._int 10130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # number of digits of self._int to left of decimal point 10150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao leftdigits = self._exp + len(self._int) 10160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # dotplace is number of digits of self._int to the left of the 10180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # decimal point in the mantissa of the output string (that is, 10190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # after adjusting the exponent) 10200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp <= 0 and leftdigits > -6: 10210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # no exponent required 10220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = leftdigits 10230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif not eng: 10240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # usual scientific notation: 1 digit on left of the point 10250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = 1 10260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self._int == '0': 10270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # engineering notation, zero 10280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = (leftdigits + 1) % 3 - 1 10290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 10300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # engineering notation, nonzero 10310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = (leftdigits - 1) % 3 + 1 10320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if dotplace <= 0: 10340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = '0' 10350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = '.' + '0'*(-dotplace) + self._int 10360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif dotplace >= len(self._int): 10370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = self._int+'0'*(dotplace-len(self._int)) 10380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = '' 10390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 10400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = self._int[:dotplace] 10410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = '.' + self._int[dotplace:] 10420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if leftdigits == dotplace: 10430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = '' 10440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 10450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 10460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 10470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = ['e', 'E'][context.capitals] + "%+d" % (leftdigits-dotplace) 10480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sign + intpart + fracpart + exp 10500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def to_eng_string(self, context=None): 10520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Convert to engineering-type string. 10530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Engineering notation has an exponent which is a multiple of 3, so there 10550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao are up to 3 digits left of the decimal place. 10560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Same rules for when in exponential and when as a value as in __str__. 10580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 10590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.__str__(eng=True, context=context) 10600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __neg__(self, context=None): 10620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a copy with the sign switched. 10630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Rounds, if it has reason. 10650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 10660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 10670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 10680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 10690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 10700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 10720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 10730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self and context.rounding != ROUND_FLOOR: 10750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # -Decimal('0') is Decimal('0'), not Decimal('-0'), except 10760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # in ROUND_FLOOR rounding mode. 10770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self.copy_abs() 10780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 10790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self.copy_negate() 10800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 10820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __pos__(self, context=None): 10840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a copy, unless it is a sNaN. 10850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Rounds the number (if more then precision digits) 10870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 10880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 10890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 10900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 10910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 10920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 10940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 10950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self and context.rounding != ROUND_FLOOR: 10970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # + (-0) = 0, except in ROUND_FLOOR rounding mode. 10980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self.copy_abs() 10990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 11000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = Decimal(self) 11010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 11030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __abs__(self, round=True, context=None): 11050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the absolute value of self. 11060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If the keyword argument 'round' is false, do not round. The 11080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expression self.__abs__(round=False) is equivalent to 11090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.copy_abs(). 11100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 11110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not round: 11120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.copy_abs() 11130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 11150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 11160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 11170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 11180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 11200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self.__neg__(context=context) 11210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 11220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self.__pos__(context=context) 11230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 11250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __add__(self, other, context=None): 11270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns self + other. 11280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -INF + INF (or the reverse) cause InvalidOperation errors. 11300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 11310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 11320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 11330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 11340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 11360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 11370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 11390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 11400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 11410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 11420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 11440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If both INF, same sign => same as both, opposite => error. 11450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign != other._sign and other._isinfinity(): 11460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, '-INF + INF') 11470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 11480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 11490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(other) # Can't both be infinity here 11500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = min(self._exp, other._exp) 11520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao negativezero = 0 11530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context.rounding == ROUND_FLOOR and self._sign != other._sign: 11540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If the answer is 0, the sign should be negative, in this case. 11550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao negativezero = 1 11560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self and not other: 11580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = min(self._sign, other._sign) 11590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if negativezero: 11600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = 1 11610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(sign, '0', exp) 11620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 11630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 11640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 11650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = max(exp, other._exp - context.prec-1) 11660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = other._rescale(exp, context.rounding) 11670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 11680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 11690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 11700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = max(exp, self._exp - context.prec-1) 11710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._rescale(exp, context.rounding) 11720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 11730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 11740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1 = _WorkRep(self) 11760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op2 = _WorkRep(other) 11770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1, op2 = _normalize(op1, op2, context.prec) 11780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = _WorkRep() 11800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op1.sign != op2.sign: 11810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Equal and opposite 11820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op1.int == op2.int: 11830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(negativezero, '0', exp) 11840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 11850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 11860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op1.int < op2.int: 11870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1, op2 = op2, op1 11880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # OK, now abs(op1) > abs(op2) 11890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op1.sign == 1: 11900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.sign = 1 11910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1.sign, op2.sign = op2.sign, op1.sign 11920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 11930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.sign = 0 11940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # So we know the sign, and op1 > 0. 11950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif op1.sign == 1: 11960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.sign = 1 11970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1.sign, op2.sign = (0, 0) 11980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 11990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.sign = 0 12000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Now, op1 > abs(op2) > 0 12010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op2.sign == 0: 12030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.int = op1.int + op2.int 12040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 12050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.int = op1.int - op2.int 12060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.exp = op1.exp 12080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = Decimal(result) 12090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 12100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 12110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __radd__ = __add__ 12130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __sub__(self, other, context=None): 12150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return self - other""" 12160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 12170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 12180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 12190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 12210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context=context) 12220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 12230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 12240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self - other is computed as self + other.copy_negate() 12260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.__add__(other.copy_negate(), context=context) 12270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __rsub__(self, other, context=None): 12290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return other - self""" 12300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 12310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 12320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 12330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other.__sub__(self, context=context) 12350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __mul__(self, other, context=None): 12370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return self * other. 12380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (+-) INF * 0 (or its reverse) raise InvalidOperation. 12400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 12410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 12420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 12430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 12440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 12460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 12470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao resultsign = self._sign ^ other._sign 12490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 12510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 12520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 12530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 12540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 12560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 12570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, '(+-)INF * 0') 12580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[resultsign] 12590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 12610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 12620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, '0 * (+-)INF') 12630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[resultsign] 12640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao resultexp = self._exp + other._exp 12660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Special case for multiplying by zero 12680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self or not other: 12690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(resultsign, '0', resultexp) 12700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Fixing in case the exponent is out of bounds 12710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 12720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 12730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Special case for multiplying by power of 10 12750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._int == '1': 12760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(resultsign, other._int, resultexp) 12770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 12780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 12790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._int == '1': 12800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(resultsign, self._int, resultexp) 12810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 12820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 12830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1 = _WorkRep(self) 12850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op2 = _WorkRep(other) 12860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(resultsign, str(op1.int * op2.int), resultexp) 12880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 12890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 12910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __rmul__ = __mul__ 12920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __truediv__(self, other, context=None): 12940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return self / other.""" 12950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 12960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 12970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return NotImplemented 12980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 13000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 13010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = self._sign ^ other._sign 13030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 13050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 13060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 13070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 13080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() and other._isinfinity(): 13100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF') 13110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 13130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[sign] 13140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 13160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Clamped, 'Division by infinity') 13170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(sign, '0', context.Etiny()) 13180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Special cases for zeroes 13200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 13210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 13220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionUndefined, '0 / 0') 13230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionByZero, 'x / 0', sign) 13240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 13260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = self._exp - other._exp 13270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = 0 13280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # OK, so neither = 0, INF or NaN 13300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shift = len(other._int) - len(self._int) + context.prec + 1 13310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = self._exp - other._exp - shift 13320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1 = _WorkRep(self) 13330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op2 = _WorkRep(other) 13340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if shift >= 0: 13350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff, remainder = divmod(op1.int * 10**shift, op2.int) 13360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff, remainder = divmod(op1.int, op2.int * 10**-shift) 13380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if remainder: 13390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result is not exact; adjust to ensure correct rounding 13400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if coeff % 5 == 0: 13410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff += 1 13420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result is exact; get as close to ideal exponent as possible 13440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ideal_exp = self._exp - other._exp 13450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while exp < ideal_exp and coeff % 10 == 0: 13460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff //= 10 13470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp += 1 13480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(sign, str(coeff), exp) 13500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 13510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _divide(self, other, context): 13530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return (self // other, self % other), to context.prec precision. 13540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Assumes that neither self nor other is a NaN, that self is not 13560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao infinite and that other is nonzero. 13570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 13580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = self._sign ^ other._sign 13590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 13600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ideal_exp = self._exp 13610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ideal_exp = min(self._exp, other._exp) 13630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expdiff = self.adjusted() - other.adjusted() 13650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self or other._isinfinity() or expdiff <= -2: 13660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (_dec_from_triple(sign, '0', 0), 13670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._rescale(ideal_exp, context.rounding)) 13680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if expdiff <= context.prec: 13690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1 = _WorkRep(self) 13700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op2 = _WorkRep(other) 13710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op1.exp >= op2.exp: 13720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1.int *= 10**(op1.exp - op2.exp) 13730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op2.int *= 10**(op2.exp - op1.exp) 13750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q, r = divmod(op1.int, op2.int) 13760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if q < 10**context.prec: 13770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (_dec_from_triple(sign, str(q), 0), 13780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _dec_from_triple(self._sign, str(r), ideal_exp)) 13790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Here the quotient is too large to be representable 13810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = context._raise_error(DivisionImpossible, 13820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'quotient too large in //, % or divmod') 13830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans, ans 13840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __rtruediv__(self, other, context=None): 13860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Swaps self/other and returns __truediv__.""" 13870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 13880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 13890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 13900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other.__truediv__(self, context=context) 13910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __div__ = __truediv__ 13930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __rdiv__ = __rtruediv__ 13940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __divmod__(self, other, context=None): 13960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 13970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Return (self // other, self % other) 13980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 13990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 14000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 14010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 14020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 14040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 14050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 14070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 14080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (ans, ans) 14090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = self._sign ^ other._sign 14110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 14120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 14130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = context._raise_error(InvalidOperation, 'divmod(INF, INF)') 14140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans, ans 14150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 14160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (_SignedInfinity[sign], 14170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(InvalidOperation, 'INF % x')) 14180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 14200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 14210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = context._raise_error(DivisionUndefined, 'divmod(0, 0)') 14220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans, ans 14230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 14240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (context._raise_error(DivisionByZero, 'x // 0', sign), 14250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(InvalidOperation, 'x % 0')) 14260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao quotient, remainder = self._divide(other, context) 14280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao remainder = remainder._fix(context) 14290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return quotient, remainder 14300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __rdivmod__(self, other, context=None): 14320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Swaps self/other and returns __divmod__.""" 14330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 14340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 14350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 14360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other.__divmod__(self, context=context) 14370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __mod__(self, other, context=None): 14390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 14400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self % other 14410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 14420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 14430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 14440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 14450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 14470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 14480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 14500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 14510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 14520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 14540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'INF % x') 14550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif not other: 14560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self: 14570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'x % 0') 14580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 14590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionUndefined, '0 % 0') 14600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao remainder = self._divide(other, context)[1] 14620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao remainder = remainder._fix(context) 14630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return remainder 14640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __rmod__(self, other, context=None): 14660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Swaps self/other and returns __mod__.""" 14670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 14680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 14690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 14700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other.__mod__(self, context=context) 14710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def remainder_near(self, other, context=None): 14730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 14740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Remainder nearest to 0- abs(remainder-near) <= other/2 14750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 14760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 14770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 14780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 14800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 14820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 14830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 14840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self == +/-infinity -> InvalidOperation 14860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 14870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 14880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'remainder_near(infinity, x)') 14890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # other == 0 -> either InvalidOperation or DivisionUndefined 14910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 14920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self: 14930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 14940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'remainder_near(x, 0)') 14950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 14960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionUndefined, 14970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'remainder_near(0, 0)') 14980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # other = +/-infinity -> remainder = self 15000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 15010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = Decimal(self) 15020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 15030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self = 0 -> remainder = self, with ideal exponent 15050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ideal_exponent = min(self._exp, other._exp) 15060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 15070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(self._sign, '0', ideal_exponent) 15080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 15090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # catch most cases of large or small quotient 15110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expdiff = self.adjusted() - other.adjusted() 15120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if expdiff >= context.prec + 1: 15130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # expdiff >= prec+1 => abs(self/other) > 10**prec 15140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionImpossible) 15150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if expdiff <= -2: 15160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # expdiff <= -2 => abs(self/other) < 0.1 15170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._rescale(ideal_exponent, context.rounding) 15180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 15190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # adjust both arguments to have the same exponent, then divide 15210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1 = _WorkRep(self) 15220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op2 = _WorkRep(other) 15230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op1.exp >= op2.exp: 15240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op1.int *= 10**(op1.exp - op2.exp) 15250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 15260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op2.int *= 10**(op2.exp - op1.exp) 15270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q, r = divmod(op1.int, op2.int) 15280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # remainder is r*10**ideal_exponent; other is +/-op2.int * 15290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 10**ideal_exponent. Apply correction to ensure that 15300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # abs(remainder) <= abs(other)/2 15310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if 2*r + (q&1) > op2.int: 15320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r -= op2.int 15330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q += 1 15340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if q >= 10**context.prec: 15360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionImpossible) 15370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result has same sign as self unless r is negative 15390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = self._sign 15400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r < 0: 15410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = 1-sign 15420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = -r 15430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(sign, str(r), ideal_exponent) 15450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 15460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __floordiv__(self, other, context=None): 15480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """self // other""" 15490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 15500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 15510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 15520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 15540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 15550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 15570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 15580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 15590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 15610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 15620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'INF // INF') 15630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 15640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[self._sign ^ other._sign] 15650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 15670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self: 15680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionByZero, 'x // 0', 15690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign ^ other._sign) 15700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 15710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionUndefined, '0 // 0') 15720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._divide(other, context)[0] 15740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __rfloordiv__(self, other, context=None): 15760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Swaps self/other and returns __floordiv__.""" 15770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 15780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 15790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 15800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other.__floordiv__(self, context=context) 15810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __float__(self): 15830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Float representation.""" 15840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isnan(): 15850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.is_snan(): 15860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Cannot convert signaling NaN to float") 15870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = "-nan" if self._sign else "nan" 15880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 15890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = str(self) 15900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return float(s) 15910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __int__(self): 15930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Converts self to an int, truncating if necessary.""" 15940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 15950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isnan(): 15960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Cannot convert NaN to integer") 15970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self._isinfinity(): 15980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise OverflowError("Cannot convert infinity to integer") 15990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = (-1)**self._sign 16000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp >= 0: 16010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s*int(self._int)*10**self._exp 16020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 16030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s*int(self._int[:self._exp] or '0') 16040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __trunc__ = __int__ 16060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def real(self): 16080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 16090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao real = property(real) 16100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def imag(self): 16120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(0) 16130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao imag = property(imag) 16140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def conjugate(self): 16160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 16170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __complex__(self): 16190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return complex(float(self)) 16200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __long__(self): 16220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Converts to a long. 16230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Equivalent to long(int(self)) 16250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 16260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return long(self.__int__()) 16270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _fix_nan(self, context): 16290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Decapitate the payload of a NaN to fit the context""" 16300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao payload = self._int 16310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # maximum length of payload is precision if _clamp=0, 16330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # precision-1 if _clamp=1. 16340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao max_payload_len = context.prec - context._clamp 16350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(payload) > max_payload_len: 16360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao payload = payload[len(payload)-max_payload_len:].lstrip('0') 16370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, payload, self._exp, True) 16380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 16390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _fix(self, context): 16410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Round if it is necessary to keep self within prec precision. 16420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Rounds and fixes the exponent. Does not raise on a sNaN. 16440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Arguments: 16460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self - Decimal instance 16470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context - context used. 16480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 16490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 16510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isnan(): 16520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # decapitate payload if necessary 16530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._fix_nan(context) 16540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 16550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self is +/-Infinity; return unaltered 16560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 16570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if self is zero then exponent should be between Etiny and 16590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Emax if _clamp==0, and between Etiny and Etop if _clamp==1. 16600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Etiny = context.Etiny() 16610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Etop = context.Etop() 16620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 16630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp_max = [context.Emax, Etop][context._clamp] 16640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao new_exp = min(max(self._exp, Etiny), exp_max) 16650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if new_exp != self._exp: 16660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Clamped) 16670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, '0', new_exp) 16680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 16690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 16700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp_min is the smallest allowable exponent of the result, 16720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # equal to max(self.adjusted()-context.prec+1, Etiny) 16730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp_min = len(self._int) + self._exp - context.prec 16740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exp_min > Etop: 16750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # overflow: exp_min > Etop iff self.adjusted() > Emax 16760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = context._raise_error(Overflow, 'above Emax', self._sign) 16770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 16780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 16790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 16800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_is_subnormal = exp_min < Etiny 16820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_subnormal: 16830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp_min = Etiny 16840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # round if self has too many digits 16860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp < exp_min: 16870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits = len(self._int) + self._exp - exp_min 16880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if digits < 0: 16890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = _dec_from_triple(self._sign, '1', exp_min-1) 16900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits = 0 16910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding_method = self._pick_rounding_function[context.rounding] 16920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao changed = rounding_method(self, digits) 16930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = self._int[:digits] or '0' 16940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if changed > 0: 16950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = str(int(coeff)+1) 16960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(coeff) > context.prec: 16970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = coeff[:-1] 16980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp_min += 1 16990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # check whether the rounding pushed the exponent out of range 17010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exp_min > Etop: 17020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = context._raise_error(Overflow, 'above Emax', self._sign) 17030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(self._sign, coeff, exp_min) 17050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # raise the appropriate signals, taking care to respect 17070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the precedence described in the specification 17080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if changed and self_is_subnormal: 17090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Underflow) 17100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_subnormal: 17110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Subnormal) 17120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if changed: 17130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 17140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 17150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not ans: 17160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # raise Clamped on underflow to 0 17170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Clamped) 17180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 17190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_subnormal: 17210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Subnormal) 17220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # fold down if _clamp == 1 and self has too few digits 17240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context._clamp == 1 and self._exp > Etop: 17250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Clamped) 17260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_padded = self._int + '0'*(self._exp - Etop) 17270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, self_padded, Etop) 17280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # here self was representable to begin with; return unchanged 17300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 17310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for each of the rounding functions below: 17330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self is a finite, nonzero Decimal 17340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # prec is an integer satisfying 0 <= prec < len(self._int) 17350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 17360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # each function returns either -1, 0, or 1, as follows: 17370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1 indicates that self should be rounded up (away from zero) 17380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 0 indicates that self should be truncated, and that all the 17390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # digits to be truncated are zeros (so the value is unchanged) 17400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # -1 indicates that there are nonzero digits to be truncated 17410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_down(self, prec): 17430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Also known as round-towards-0, truncate.""" 17440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _all_zeros(self._int, prec): 17450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 17460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -1 17480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_up(self, prec): 17500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds away from 0.""" 17510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -self._round_down(prec) 17520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_half_up(self, prec): 17540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds 5 up (away from 0)""" 17550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._int[prec] in '56789': 17560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 1 17570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif _all_zeros(self._int, prec): 17580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 17590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -1 17610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_half_down(self, prec): 17630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Round 5 down""" 17640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _exact_half(self._int, prec): 17650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -1 17660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._round_half_up(prec) 17680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_half_even(self, prec): 17700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Round 5 to even, rest to nearest.""" 17710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _exact_half(self._int, prec) and \ 17720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (prec == 0 or self._int[prec-1] in '02468'): 17730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -1 17740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._round_half_up(prec) 17760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_ceiling(self, prec): 17780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds up (not away from 0 if negative.)""" 17790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 17800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._round_down(prec) 17810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -self._round_down(prec) 17830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_floor(self, prec): 17850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds down (not towards 0 if negative)""" 17860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self._sign: 17870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._round_down(prec) 17880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -self._round_down(prec) 17900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round_05up(self, prec): 17920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Round down unless digit prec-1 is 0 or 5.""" 17930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if prec and self._int[prec-1] not in '05': 17940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._round_down(prec) 17950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return -self._round_down(prec) 17970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _pick_rounding_function = dict( 17990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_DOWN = _round_down, 18000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_UP = _round_up, 18010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_HALF_UP = _round_half_up, 18020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_HALF_DOWN = _round_half_down, 18030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_HALF_EVEN = _round_half_even, 18040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_CEILING = _round_ceiling, 18050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_FLOOR = _round_floor, 18060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ROUND_05UP = _round_05up, 18070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ) 18080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def fma(self, other, third, context=None): 18100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Fused multiply-add. 18110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Returns self*other+third with no rounding of the intermediate 18130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao product self*other. 18140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self and other are multiplied together, with no rounding of 18160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the result. The third operand is then added to the result, 18170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao and a single final rounding is performed. 18180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 18190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 18210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute product; raise InvalidOperation if either operand is 18230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # a signaling NaN or if the product is zero times infinity. 18240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 18250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 18260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 18270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp == 'N': 18280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sNaN', self) 18290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._exp == 'N': 18300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sNaN', other) 18310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp == 'n': 18320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao product = self 18330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif other._exp == 'n': 18340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao product = other 18350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self._exp == 'F': 18360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 18370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 18380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'INF * 0 in fma') 18390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao product = _SignedInfinity[self._sign ^ other._sign] 18400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif other._exp == 'F': 18410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 18420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 18430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '0 * INF in fma') 18440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao product = _SignedInfinity[self._sign ^ other._sign] 18450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 18460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao product = _dec_from_triple(self._sign ^ other._sign, 18470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao str(int(self._int) * int(other._int)), 18480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp + other._exp) 18490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao third = _convert_other(third, raiseit=True) 18510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return product.__add__(third, context) 18520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _power_modulo(self, other, modulo, context=None): 18540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Three argument version of __pow__""" 18550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if can't convert other and modulo to Decimal, raise 18570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # TypeError; there's no point returning NotImplemented (no 18580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # equivalent of __rpow__ for three argument pow) 18590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 18600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao modulo = _convert_other(modulo, raiseit=True) 18610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 18630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 18640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # deal with NaNs: if there are any sNaNs then first one wins, 18660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (i.e. behaviour for NaNs is identical to that of fma) 18670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_is_nan = self._isnan() 18680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_is_nan = other._isnan() 18690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao modulo_is_nan = modulo._isnan() 18700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_nan or other_is_nan or modulo_is_nan: 18710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_nan == 2: 18720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sNaN', 18730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self) 18740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_is_nan == 2: 18750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sNaN', 18760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other) 18770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if modulo_is_nan == 2: 18780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sNaN', 18790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao modulo) 18800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_is_nan: 18810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._fix_nan(context) 18820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_is_nan: 18830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other._fix_nan(context) 18840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return modulo._fix_nan(context) 18850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # check inputs: we apply same restrictions as Python's pow() 18870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not (self._isinteger() and 18880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other._isinteger() and 18890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao modulo._isinteger()): 18900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 18910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'pow() 3rd argument not allowed ' 18920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'unless all arguments are integers') 18930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other < 0: 18940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 18950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'pow() 2nd argument cannot be ' 18960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'negative when 3rd argument specified') 18970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not modulo: 18980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 18990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'pow() 3rd argument cannot be 0') 19000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # additional restriction for decimal: the modulus must be less 19020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # than 10**prec in absolute value 19030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if modulo.adjusted() >= context.prec: 19040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 19050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'insufficient precision: pow() 3rd ' 19060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'argument must not have more than ' 19070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'precision digits') 19080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # define 0**0 == NaN, for consistency with two-argument pow 19100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (even though it hurts!) 19110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other and not self: 19120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 19130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'at least one of pow() 1st argument ' 19140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'and 2nd argument must be nonzero ;' 19150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '0**0 is not defined') 19160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute sign of result 19180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._iseven(): 19190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = 0 19200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 19210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = self._sign 19220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # convert modulo to a Python integer, and self and other to 19240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Decimal integers (i.e. force their exponents to be >= 0) 19250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao modulo = abs(int(modulo)) 19260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao base = _WorkRep(self.to_integral_value()) 19270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exponent = _WorkRep(other.to_integral_value()) 19280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute result using integer pow() 19300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo 19310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in xrange(exponent.exp): 19320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao base = pow(base, 10, modulo) 19330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao base = pow(base, exponent.int, modulo) 19340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(sign, str(base), 0) 19360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _power_exact(self, other, p): 19380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Attempt to compute self**other exactly. 19390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Given Decimals self and other and an integer p, attempt to 19410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao compute an exact result for the power self**other, with p 19420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits of precision. Return None if self**other is not 19430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exactly representable in p digits. 19440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Assumes that elimination of special cases has already been 19460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao performed: self and other must both be nonspecial; self must 19470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be positive and not numerically equal to 1; other must be 19480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nonzero. For efficiency, other._exp should not be too large, 19490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao so that 10**abs(other._exp) is a feasible calculation.""" 19500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # In the comments below, we write x for the value of self and y for the 19520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # value of other. Write x = xc*10**xe and abs(y) = yc*10**ye, with xc 19530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # and yc positive integers not divisible by 10. 19540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The main purpose of this method is to identify the *failure* 19560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # of x**y to be exactly representable with as little effort as 19570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # possible. So we look for cheap and easy tests that 19580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # eliminate the possibility of x**y being exact. Only if all 19590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # these tests are passed do we go on to actually compute x**y. 19600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Here's the main idea. Express y as a rational number m/n, with m and 19620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # n relatively prime and n>0. Then for x**y to be exactly 19630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # representable (at *any* precision), xc must be the nth power of a 19640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # positive integer and xe must be divisible by n. If y is negative 19650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # then additionally xc must be a power of either 2 or 5, hence a power 19660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # of 2**n or 5**n. 19670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 19680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # There's a limit to how small |y| can be: if y=m/n as above 19690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # then: 19700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 19710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (1) if xc != 1 then for the result to be representable we 19720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # need xc**(1/n) >= 2, and hence also xc**|y| >= 2. So 19730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if |y| <= 1/nbits(xc) then xc < 2**nbits(xc) <= 19740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 2**(1/|y|), hence xc**|y| < 2 and the result is not 19750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # representable. 19760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 19770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (2) if xe != 0, |xe|*(1/n) >= 1, so |xe|*|y| >= 1. Hence if 19780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # |y| < 1/|xe| then the result is not representable. 19790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 19800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Note that since x is not equal to 1, at least one of (1) and 19810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (2) must apply. Now |y| < 1/nbits(xc) iff |yc|*nbits(xc) < 19820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 10**-ye iff len(str(|yc|*nbits(xc)) <= -ye. 19830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 19840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # There's also a limit to how large y can be, at least if it's 19850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # positive: the normalized result will have coefficient xc**y, 19860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # so if it's representable then xc**y < 10**p, and y < 19870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # p/log10(xc). Hence if y*log10(xc) >= p then the result is 19880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # not exactly representable. 19890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if len(str(abs(yc*xe)) <= -ye then abs(yc*xe) < 10**-ye, 19910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # so |y| < 1/xe and the result is not representable. 19920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Similarly, len(str(abs(yc)*xc_bits)) <= -ye implies |y| 19930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # < 1/nbits(xc). 19940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 19950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = _WorkRep(self) 19960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc, xe = x.int, x.exp 19970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while xc % 10 == 0: 19980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc //= 10 19990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe += 1 20000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _WorkRep(other) 20020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yc, ye = y.int, y.exp 20030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while yc % 10 == 0: 20040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yc //= 10 20050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ye += 1 20060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # case where xc == 1: result is 10**(xe*y), with xe*y 20080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # required to be an integer 20090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xc == 1: 20100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe *= yc 20110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result is now 10**(xe * 10**ye); xe * 10**ye must be integral 20120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while xe % 10 == 0: 20130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe //= 10 20140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ye += 1 20150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ye < 0: 20160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 20170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exponent = xe * 10**ye 20180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if y.sign == 1: 20190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exponent = -exponent 20200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if other is a nonnegative integer, use ideal exponent 20210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinteger() and other._sign == 0: 20220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ideal_exponent = self._exp*int(other) 20230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao zeros = min(exponent-ideal_exponent, p-1) 20240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 20250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao zeros = 0 20260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, '1' + '0'*zeros, exponent-zeros) 20270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # case where y is negative: xc must be either a power 20290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # of 2 or a power of 5. 20300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if y.sign == 1: 20310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao last_digit = xc % 10 20320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if last_digit in (2,4,6,8): 20330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # quick test for power of 2 20340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xc & -xc != xc: 20350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 20360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # now xc is a power of 2; e is its exponent 20370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e = _nbits(xc)-1 20380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We now have: 20400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # x = 2**e * 10**xe, e > 0, and y < 0. 20420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The exact result is: 20440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # x**y = 5**(-e*y) * 10**(e*y + xe*y) 20460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # provided that both e*y and xe*y are integers. Note that if 20480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 5**(-e*y) >= 10**p, then the result can't be expressed 20490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exactly with p digits of precision. 20500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Using the above, we can guard against large values of ye. 20520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 93/65 is an upper bound for log(10)/log(5), so if 20530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ye >= len(str(93*p//65)) 20550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # then 20570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # -e*y >= -y >= 10**ye > 93*p/65 > p*log(10)/log(5), 20590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 20600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # so 5**(-e*y) >= 10**p, and the coefficient of the result 20610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # can't be expressed in p digits. 20620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # emax >= largest e such that 5**e < 10**p. 20640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao emax = p*93//65 20650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ye >= len(str(emax)): 20660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 20670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Find -e*y and -xe*y; both must be integers 20690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e = _decimal_lshift_exact(e * yc, ye) 20700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe = _decimal_lshift_exact(xe * yc, ye) 20710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if e is None or xe is None: 20720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 20730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if e > emax: 20750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 20760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc = 5**e 20770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif last_digit == 5: 20790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # e >= log_5(xc) if xc is a power of 5; we have 20800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # equality all the way up to xc=5**2658 20810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e = _nbits(xc)*28//65 20820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc, remainder = divmod(5**e, xc) 20830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if remainder: 20840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 20850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while xc % 5 == 0: 20860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc //= 5 20870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e -= 1 20880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Guard against large values of ye, using the same logic as in 20900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the 'xc is a power of 2' branch. 10/3 is an upper bound for 20910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log(10)/log(2). 20920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao emax = p*10//3 20930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ye >= len(str(emax)): 20940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 20950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e = _decimal_lshift_exact(e * yc, ye) 20970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe = _decimal_lshift_exact(xe * yc, ye) 20980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if e is None or xe is None: 20990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if e > emax: 21020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc = 2**e 21040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 21050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xc >= 10**p: 21080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe = -e-xe 21100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, str(xc), xe) 21110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # now y is positive; find m and n such that y = m/n 21130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ye >= 0: 21140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m, n = yc*10**ye, 1 21150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 21160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xe != 0 and len(str(abs(yc*xe))) <= -ye: 21170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc_bits = _nbits(xc) 21190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xc != 1 and len(str(abs(yc)*xc_bits)) <= -ye: 21200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m, n = yc, 10**(-ye) 21220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while m % 2 == n % 2 == 0: 21230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m //= 2 21240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n //= 2 21250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while m % 5 == n % 5 == 0: 21260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m //= 5 21270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n //= 5 21280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute nth root of xc*10**xe 21300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if n > 1: 21310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if 1 < xc < 2**n then xc isn't an nth power 21320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xc != 1 and xc_bits <= n: 21330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe, rem = divmod(xe, n) 21360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if rem != 0: 21370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute nth root of xc using Newton's method 21400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = 1L << -(-_nbits(xc)//n) # initial estimate 21410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while True: 21420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q, r = divmod(xc, a**(n-1)) 21430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if a <= q: 21440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 21450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 21460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = (a*(n-1) + q)//n 21470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not (a == q and r == 0): 21480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc = a 21500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # now xc*10**xe is the nth root of the original xc*10**xe 21520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute mth power of xc*10**xe 21530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if m > p*100//_log10_lb(xc) then m > p/log10(xc), hence xc**m > 21550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 10**p and the result is not representable. 21560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xc > 1 and m > p*100//_log10_lb(xc): 21570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc = xc**m 21590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xe *= m 21600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if xc > 10**p: 21610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None 21620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # by this point the result *is* exactly representable 21640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # adjust the exponent to get as close as possible to the ideal 21650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exponent, if necessary 21660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao str_xc = str(xc) 21670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinteger() and other._sign == 0: 21680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ideal_exponent = self._exp*int(other) 21690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao zeros = min(xe-ideal_exponent, p-len(str_xc)) 21700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 21710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao zeros = 0 21720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, str_xc+'0'*zeros, xe-zeros) 21730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __pow__(self, other, modulo=None, context=None): 21750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return self ** other [ % modulo]. 21760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao With two arguments, compute self**other. 21780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao With three arguments, compute (self**other) % modulo. For the 21800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao three argument form, the following restrictions on the 21810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao arguments hold: 21820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - all three arguments must be integral 21840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - other must be nonnegative 21850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - either self or other (or both) must be nonzero 21860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - modulo must be nonzero and must have at most p digits, 21870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao where p is the context precision. 21880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If any of these restrictions is violated the InvalidOperation 21900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao flag is raised. 21910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result of pow(self, other, modulo) is identical to the 21930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result that would be obtained by computing (self**other) % 21940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao modulo with unbounded precision, but is computed more 21950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao efficiently. It is always exact. 21960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 21970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 21980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if modulo is not None: 21990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._power_modulo(other, modulo, context) 22000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 22020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 22030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 22040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 22060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 22070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # either argument is a NaN => result is NaN 22090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 22100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 22110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 22120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity) 22140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other: 22150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 22160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, '0 ** 0') 22170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 22180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 22190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result has sign 1 iff self._sign is 1 and other is an odd integer 22210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result_sign = 0 22220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign == 1: 22230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinteger(): 22240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not other._iseven(): 22250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result_sign = 1 22260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 22270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # -ve**noninteger = NaN 22280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (-0)**noninteger = 0**noninteger 22290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self: 22300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 22310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'x ** y with x negative and y not an integer') 22320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # negate self, without doing any unwanted rounding 22330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = self.copy_negate() 22340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 0**(+ve or Inf)= 0; 0**(-ve or -Inf) = Infinity 22360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 22370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._sign == 0: 22380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(result_sign, '0', 0) 22390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 22400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[result_sign] 22410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Inf**(+ve or Inf) = Inf; Inf**(-ve or -Inf) = 0 22430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 22440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._sign == 0: 22450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[result_sign] 22460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 22470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(result_sign, '0', 0) 22480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1**other = 1, but the choice of exponent and the flags 22500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # depend on the exponent of self, and on whether other is a 22510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # positive integer, a negative integer, or neither 22520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self == _One: 22530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinteger(): 22540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp = max(self._exp*max(int(other), 0), 22550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1-context.prec) but evaluating int(other) directly 22560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # is dangerous until we know other is small (other 22570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # could be 1e999999999) 22580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._sign == 1: 22590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao multiplier = 0 22600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif other > context.prec: 22610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao multiplier = context.prec 22620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 22630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao multiplier = int(other) 22640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = self._exp * multiplier 22660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exp < 1-context.prec: 22670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = 1-context.prec 22680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 22690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 22700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 22710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 22720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = 1-context.prec 22730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(result_sign, '1'+'0'*-exp, exp) 22750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute adjusted exponent of self 22770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_adj = self.adjusted() 22780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self ** infinity is infinity if self > 1, 0 if self < 1 22800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self ** -infinity is infinity if self < 1, 0 if self > 1 22810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._isinfinity(): 22820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if (other._sign == 0) == (self_adj < 0): 22830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(result_sign, '0', 0) 22840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 22850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _SignedInfinity[result_sign] 22860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # from here on, the result always goes through the call 22880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # to _fix at the end of this function. 22890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = None 22900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exact = False 22910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 22920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # crude test to catch cases of extreme overflow/underflow. If 22930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log10(self)*other >= 10**bound and bound >= len(str(Emax)) 22940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # then 10**bound >= 10**len(str(Emax)) >= Emax+1 and hence 22950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self**other >= 10**(Emax+1), so overflow occurs. The test 22960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for underflow is similar. 22970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bound = self._log10_exp_bound() + other.adjusted() 22980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if (self_adj >= 0) == (other._sign == 0): 22990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self > 1 and other +ve, or self < 1 and other -ve 23000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # possibility of overflow 23010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if bound >= len(str(context.Emax)): 23020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(result_sign, '1', context.Emax+1) 23030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 23040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self > 1 and other -ve, or self < 1 and other +ve 23050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # possibility of underflow to 0 23060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Etiny = context.Etiny() 23070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if bound >= len(str(-Etiny)): 23080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(result_sign, '1', Etiny-1) 23090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # try for an exact result with precision +1 23110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans is None: 23120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._power_exact(other, context.prec + 1) 23130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans is not None: 23140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if result_sign == 1: 23150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(1, ans._int, ans._exp) 23160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exact = True 23170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # usual case: inexact result, x**y computed directly as exp(y*log(x)) 23190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans is None: 23200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = context.prec 23210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = _WorkRep(self) 23220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao xc, xe = x.int, x.exp 23230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _WorkRep(other) 23240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yc, ye = y.int, y.exp 23250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if y.sign == 1: 23260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yc = -yc 23270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute correctly rounded result: start with precision +3, 23290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # then increase precision until result is unambiguously roundable 23300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra = 3 23310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while True: 23320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff, exp = _dpower(xc, xe, yc, ye, p+extra) 23330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if coeff % (5*10**(len(str(coeff))-p-1)): 23340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 23350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra += 3 23360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(result_sign, str(coeff), exp) 23380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # unlike exp, ln and log10, the power function respects the 23400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # rounding mode; no need to switch to ROUND_HALF_EVEN here 23410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # There's a difficulty here when 'other' is not an integer and 23430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the result is exact. In this case, the specification 23440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # requires that the Inexact flag be raised (in spite of 23450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exactness), but since the result is exact _fix won't do this 23460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for us. (Correspondingly, the Underflow signal should also 23470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # be raised for subnormal results.) We can't directly raise 23480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # these signals either before or after calling _fix, since 23490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # that would violate the precedence for signals. So we wrap 23500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the ._fix call in a temporary context, and reraise 23510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # afterwards. 23520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exact and not other._isinteger(): 23530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # pad with zeros up to length context.prec+1 if necessary; this 23540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ensures that the Rounded signal will be raised. 23550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(ans._int) <= context.prec: 23560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expdiff = context.prec + 1 - len(ans._int) 23570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(ans._sign, ans._int+'0'*expdiff, 23580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans._exp-expdiff) 23590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # create a copy of the current context, with cleared flags/traps 23610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao newcontext = context.copy() 23620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao newcontext.clear_flags() 23630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for exception in _signals: 23640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao newcontext.traps[exception] = 0 23650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # round in the new context 23670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(newcontext) 23680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # raise Inexact, and if necessary, Underflow 23700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao newcontext._raise_error(Inexact) 23710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if newcontext.flags[Subnormal]: 23720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao newcontext._raise_error(Underflow) 23730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # propagate signals to the original context; _fix could 23750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # have raised any of Overflow, Underflow, Subnormal, 23760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Inexact, Rounded, Clamped. Overflow needs the correct 23770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # arguments. Note that the order of the exceptions is 23780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # important here. 23790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if newcontext.flags[Overflow]: 23800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Overflow, 'above Emax', ans._sign) 23810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for exception in Underflow, Subnormal, Inexact, Rounded, Clamped: 23820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if newcontext.flags[exception]: 23830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(exception) 23840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 23860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 23870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 23890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __rpow__(self, other, context=None): 23910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Swaps self/other and returns __pow__.""" 23920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other) 23930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other is NotImplemented: 23940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 23950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other.__pow__(self, context=context) 23960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 23970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def normalize(self, context=None): 23980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Normalize- strip trailing 0s, change anything equal to 0 to 0e0""" 23990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 24010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 24020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 24040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 24050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 24060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 24070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = self._fix(context) 24090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if dup._isinfinity(): 24100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return dup 24110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not dup: 24130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(dup._sign, '0', 0) 24140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp_max = [context.Emax, context.Etop()][context._clamp] 24150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao end = len(dup._int) 24160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = dup._exp 24170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while dup._int[end-1] == '0' and exp < exp_max: 24180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp += 1 24190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao end -= 1 24200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(dup._sign, dup._int[:end], exp) 24210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def quantize(self, exp, rounding=None, context=None, watchexp=True): 24230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Quantize self so its exponent is the same as that of exp. 24240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Similar to self._rescale(exp._exp) but with error checking. 24260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 24270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = _convert_other(exp, raiseit=True) 24280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 24300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 24310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if rounding is None: 24320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context.rounding 24330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or exp._is_special: 24350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(exp, context) 24360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 24370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 24380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exp._isinfinity() or self._isinfinity(): 24400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exp._isinfinity() and self._isinfinity(): 24410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) # if both are inf, it is OK 24420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 24430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'quantize with one INF') 24440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if we're not watching exponents, do a simple rescale 24460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not watchexp: 24470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._rescale(exp._exp, rounding) 24480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # raise Inexact and Rounded where appropriate 24490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans._exp > self._exp: 24500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 24510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans != self: 24520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 24530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 24540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp._exp should be between Etiny and Emax 24560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not (context.Etiny() <= exp._exp <= context.Emax): 24570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 24580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'target exponent out of bounds in quantize') 24590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 24610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(self._sign, '0', exp._exp) 24620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 24630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_adjusted = self.adjusted() 24650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_adjusted > context.Emax: 24660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 24670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'exponent of quantize result too large for current context') 24680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_adjusted - exp._exp + 1 > context.prec: 24690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 24700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'quantize result has too many digits for current context') 24710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._rescale(exp._exp, rounding) 24730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans.adjusted() > context.Emax: 24740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 24750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'exponent of quantize result too large for current context') 24760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if len(ans._int) > context.prec: 24770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 24780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'quantize result has too many digits for current context') 24790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # raise appropriate flags 24810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans and ans.adjusted() < context.Emin: 24820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Subnormal) 24830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans._exp > self._exp: 24840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans != self: 24850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 24860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 24870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # call to fix takes care of any necessary folddown, and 24890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # signals Clamped if necessary 24900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 24910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 24920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def same_quantum(self, other): 24940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self and other have the same exponent; otherwise 24950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False. 24960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 24970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If either operand is a special value, the following rules are used: 24980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao * return True if both operands are infinities 24990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao * return True if both operands are NaNs 25000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao * otherwise, return False. 25010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 25020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 25030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 25040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.is_nan() and other.is_nan() or 25050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.is_infinite() and other.is_infinite()) 25060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._exp == other._exp 25070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _rescale(self, exp, rounding): 25090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rescale self so that the exponent is exp, either by padding with zeros 25100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao or by truncating digits, using the given rounding mode. 25110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Specials are returned without change. This operation is 25130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao quiet: it raises no flags, and uses no information from the 25140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context. 25150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = exp to scale to (an integer) 25170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = rounding mode 25180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 25190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 25200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 25210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 25220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, '0', exp) 25230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp >= exp: 25250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # pad answer with zeros if necessary 25260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, 25270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int + '0'*(self._exp - exp), exp) 25280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # too many digits; round and lose data. If self.adjusted() < 25300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp-1, replace self by 10**(exp-1) before rounding 25310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits = len(self._int) + self._exp - exp 25320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if digits < 0: 25330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = _dec_from_triple(self._sign, '1', exp-1) 25340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits = 0 25350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao this_function = self._pick_rounding_function[rounding] 25360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao changed = this_function(self, digits) 25370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = self._int[:digits] or '0' 25380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if changed == 1: 25390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = str(int(coeff)+1) 25400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, coeff, exp) 25410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _round(self, places, rounding): 25430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Round a nonzero, nonspecial Decimal to a fixed number of 25440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao significant figures, using the given rounding mode. 25450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Infinities, NaNs and zeros are returned unaltered. 25470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This operation is quiet: it raises no flags, and uses no 25490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao information from the context. 25500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 25520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if places <= 0: 25530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("argument should be at least 1 in _round") 25540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or not self: 25550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 25560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._rescale(self.adjusted()+1-places, rounding) 25570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # it can happen that the rescale alters the adjusted exponent; 25580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for example when rounding 99.97 to 3 significant figures. 25590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # When this happens we end up with an extra 0 at the end of 25600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the number; a second rescale fixes this. 25610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans.adjusted() != self.adjusted(): 25620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._rescale(ans.adjusted()+1-places, rounding) 25630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 25640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def to_integral_exact(self, rounding=None, context=None): 25660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds to a nearby integer. 25670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If no rounding mode is specified, take the rounding mode from 25690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the context. This method raises the Rounded and Inexact flags 25700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao when appropriate. 25710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao See also: to_integral_value, which does exactly the same as 25730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao this method except that it doesn't raise Inexact or Rounded. 25740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 25750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 25760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 25770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 25780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 25790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 25800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp >= 0: 25810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 25820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 25830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, '0', 0) 25840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 25850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 25860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if rounding is None: 25870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context.rounding 25880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._rescale(0, rounding) 25890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans != self: 25900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 25910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 25920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 25930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 25940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def to_integral_value(self, rounding=None, context=None): 25950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds to the nearest integer, without raising inexact, rounded.""" 25960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 25970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 25980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if rounding is None: 25990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context.rounding 26000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 26010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 26020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 26030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 26040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 26050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp >= 0: 26060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 26070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 26080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._rescale(0, rounding) 26090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the method name changed, but we provide also the old one, for compatibility 26110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao to_integral = to_integral_value 26120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def sqrt(self, context=None): 26140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return the square root of self.""" 26150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 26160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 26170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 26190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 26200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 26210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 26220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() and self._sign == 0: 26240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 26250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 26270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exponent = self._exp // 2. sqrt(-0) = -0 26280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(self._sign, '0', self._exp // 2) 26290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 26300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign == 1: 26320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0') 26330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # At this point self represents a positive number. Let p be 26350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the desired precision and express self in the form c*100**e 26360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # with c a positive real number and e an integer, c and e 26370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # being chosen so that 100**(p-1) <= c < 100**p. Then the 26380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (exact) square root of self is sqrt(c)*10**e, and 10**(p-1) 26390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # <= sqrt(c) < 10**p, so the closest representable Decimal at 26400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # precision p is n*10**e where n = round_half_even(sqrt(c)), 26410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the closest integer to sqrt(c) with the even integer chosen 26420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # in the case of a tie. 26430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 26440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # To ensure correct rounding in all cases, we use the 26450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # following trick: we compute the square root to an extra 26460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # place (precision p+1 instead of precision p), rounding down. 26470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Then, if the result is inexact and its last digit is 0 or 5, 26480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # we increase the last digit to 1 or 6 respectively; if it's 26490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exact we leave the last digit alone. Now the final round to 26500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # p places (or fewer in the case of underflow) will round 26510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # correctly and raise the appropriate flags. 26520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # use an extra digit of precision 26540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao prec = context.prec+1 26550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # write argument in the form c*100**e where e = self._exp//2 26570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # is the 'ideal' exponent, to be used if the square root is 26580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exactly representable. l is the number of 'digits' of c in 26590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # base 100, so that 100**(l-1) <= c < 100**l. 26600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op = _WorkRep(self) 26610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e = op.exp >> 1 26620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op.exp & 1: 26630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = op.int * 10 26640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao l = (len(self._int) >> 1) + 1 26650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 26660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = op.int 26670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao l = len(self._int)+1 >> 1 26680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # rescale so that c has exactly prec base 100 'digits' 26700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shift = prec-l 26710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if shift >= 0: 26720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c *= 100**shift 26730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exact = True 26740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 26750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c, remainder = divmod(c, 100**-shift) 26760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exact = not remainder 26770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e -= shift 26780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # find n = floor(sqrt(c)) using Newton's method 26800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n = 10**prec 26810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while True: 26820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q = c//n 26830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if n <= q: 26840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 26850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 26860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n = n + q >> 1 26870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exact = exact and n*n == c 26880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 26890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exact: 26900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result is exact; rescale to use ideal exponent e 26910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if shift >= 0: 26920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # assert n % 10**shift == 0 26930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n //= 10**shift 26940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 26950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n *= 10**-shift 26960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e += shift 26970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 26980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result is not exact; fix last digit as described above 26990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if n % 5 == 0: 27000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n += 1 27010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(0, str(n), e) 27030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # round, and fit to current context 27050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context._shallow_copy() 27060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context._set_rounding(ROUND_HALF_EVEN) 27070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 27080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.rounding = rounding 27090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 27110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def max(self, other, context=None): 27130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the larger value. 27140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Like max(self, other) except if one is not a number, returns 27160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaN (and signals if one is sNaN). Also rounds. 27170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 27180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 27190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 27210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 27220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 27240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If one operand is a quiet NaN and the other is number, then the 27250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # number is always returned 27260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sn = self._isnan() 27270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao on = other._isnan() 27280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn or on: 27290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if on == 1 and sn == 0: 27300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._fix(context) 27310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn == 1 and on == 0: 27320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other._fix(context) 27330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._check_nans(other, context) 27340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self._cmp(other) 27360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == 0: 27370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If both operands are finite and equal in numerical value 27380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # then an ordering is applied: 27390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 27400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If the signs differ then max returns the operand with the 27410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # positive sign and min returns the operand with the negative sign 27420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 27430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If the signs are the same then the exponent is used to select 27440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the result. This is exactly the ordering used in compare_total. 27450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self.compare_total(other) 27460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == -1: 27480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = other 27490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 27500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self 27510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 27530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def min(self, other, context=None): 27550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the smaller value. 27560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Like min(self, other) except if one is not a number, returns 27580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaN (and signals if one is sNaN). Also rounds. 27590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 27600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 27610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 27630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 27640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 27660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If one operand is a quiet NaN and the other is number, then the 27670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # number is always returned 27680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sn = self._isnan() 27690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao on = other._isnan() 27700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn or on: 27710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if on == 1 and sn == 0: 27720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._fix(context) 27730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn == 1 and on == 0: 27740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other._fix(context) 27750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._check_nans(other, context) 27760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self._cmp(other) 27780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == 0: 27790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self.compare_total(other) 27800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == -1: 27820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self 27830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 27840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = other 27850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 27870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _isinteger(self): 27890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns whether self is an integer""" 27900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 27910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 27920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp >= 0: 27930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return True 27940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rest = self._int[self._exp:] 27950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return rest == '0'*len(rest) 27960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 27970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _iseven(self): 27980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns True if self is even. Assumes self is an integer.""" 27990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self or self._exp > 0: 28000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return True 28010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._int[-1+self._exp] in '02468' 28020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def adjusted(self): 28040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return the adjusted exponent of self""" 28050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 28060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._exp + len(self._int) - 1 28070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If NaN or Infinity, self._exp is string 28080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 28090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 28100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def canonical(self, context=None): 28120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the same Decimal object. 28130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao As we do not have different encodings for the same number, the 28150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao received object already is in its canonical form. 28160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 28170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 28180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare_signal(self, other, context=None): 28200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares self to the other operand numerically. 28210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao It's pretty much like compare(), but all NaNs signal, with signaling 28230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaNs taking precedence over quiet NaNs. 28240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 28250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit = True) 28260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._compare_check_nans(other, context) 28270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 28280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 28290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.compare(other, context=context) 28300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare_total(self, other): 28320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares self to other using the abstract representations. 28330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This is not like the standard compare, which use their numerical 28350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value. Note that a total ordering is defined for all possible abstract 28360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao representations. 28370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 28380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 28390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if one is negative and the other is positive, it's easy 28410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign and not other._sign: 28420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self._sign and other._sign: 28440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = self._sign 28460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # let's handle both NaN types 28480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_nan = self._isnan() 28490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_nan = other._isnan() 28500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_nan or other_nan: 28510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_nan == other_nan: 28520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compare payloads as though they're integers 28530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self_key = len(self._int), self._int 28540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_key = len(other._int), other._int 28550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_key < other_key: 28560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sign: 28570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 28590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_key > other_key: 28610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sign: 28620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 28640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Zero 28660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sign: 28680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_nan == 1: 28690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_nan == 1: 28710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_nan == 2: 28730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_nan == 2: 28750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 28770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_nan == 1: 28780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_nan == 1: 28800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self_nan == 2: 28820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_nan == 2: 28840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self < other: 28870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self > other: 28890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 28910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp < other._exp: 28920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sign: 28930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 28940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 28950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp > other._exp: 28970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sign: 28980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeOne 28990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 29000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 29010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Zero 29020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare_total_mag(self, other): 29050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares self to other using abstract repr., ignoring sign. 29060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Like compare_total, but with operand's sign ignored and assumed to be 0. 29080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 29090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 29100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.copy_abs() 29120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao o = other.copy_abs() 29130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return s.compare_total(o) 29140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy_abs(self): 29160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a copy with the sign set to 0. """ 29170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, self._int, self._exp, self._is_special) 29180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy_negate(self): 29200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a copy with the sign inverted.""" 29210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 29220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, self._int, self._exp, self._is_special) 29230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 29240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(1, self._int, self._exp, self._is_special) 29250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy_sign(self, other): 29270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns self with the sign of other.""" 29280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 29290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(other._sign, self._int, 29300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp, self._is_special) 29310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def exp(self, context=None): 29330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns e ** self.""" 29340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 29360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 29370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp(NaN) = NaN 29390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 29400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 29410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 29420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp(-Infinity) = 0 29440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == -1: 29450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Zero 29460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp(0) = 1 29480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 29490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _One 29500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp(Infinity) = Infinity 29520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == 1: 29530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 29540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the result is now guaranteed to be inexact (the true 29560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # mathematical result is transcendental). There's no need to 29570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # raise Rounded and Inexact here---they'll always be raised as 29580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # a result of the call to _fix. 29590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = context.prec 29600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao adj = self.adjusted() 29610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # we only need to do any computation for quite a small range 29630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # of adjusted exponents---for example, -29 <= adj <= 10 for 29640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the default context. For smaller exponent the result is 29650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # indistinguishable from 1 at the given precision, while for 29660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # larger exponent the result either overflows or underflows. 29670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign == 0 and adj > len(str((context.Emax+1)*3)): 29680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # overflow 29690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(0, '1', context.Emax+1) 29700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self._sign == 1 and adj > len(str((-context.Etiny()+1)*3)): 29710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # underflow to 0 29720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(0, '1', context.Etiny()-1) 29730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self._sign == 0 and adj < -p: 29740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # p+1 digits; final round will raise correct flags 29750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(0, '1' + '0'*(p-1) + '1', -p) 29760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif self._sign == 1 and adj < -p-1: 29770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # p+1 digits; final round will raise correct flags 29780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(0, '9'*(p+1), -p-1) 29790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # general case 29800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 29810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op = _WorkRep(self) 29820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c, e = op.int, op.exp 29830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op.sign == 1: 29840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = -c 29850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute correctly rounded result: increase precision by 29870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 3 digits at a time until we get an unambiguously 29880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # roundable result 29890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra = 3 29900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while True: 29910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff, exp = _dexp(c, e, p+extra) 29920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if coeff % (5*10**(len(str(coeff))-p-1)): 29930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 29940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra += 3 29950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(0, str(coeff), exp) 29970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 29980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # at this stage, ans should round correctly with *any* 29990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # rounding mode, not just with ROUND_HALF_EVEN 30000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context._shallow_copy() 30010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context._set_rounding(ROUND_HALF_EVEN) 30020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 30030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.rounding = rounding 30040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 30060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_canonical(self): 30080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is canonical; otherwise return False. 30090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Currently, the encoding of a Decimal instance is always 30110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao canonical, so this method returns True for any Decimal. 30120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 30130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return True 30140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_finite(self): 30160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is finite; otherwise return False. 30170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao A Decimal instance is considered finite if it is neither 30190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao infinite nor a NaN. 30200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 30210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return not self._is_special 30220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_infinite(self): 30240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is infinite; otherwise return False.""" 30250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._exp == 'F' 30260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_nan(self): 30280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is a qNaN or sNaN; otherwise return False.""" 30290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._exp in ('n', 'N') 30300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_normal(self, context=None): 30320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is a normal number; otherwise return False.""" 30330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or not self: 30340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 30350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 30360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 30370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context.Emin <= self.adjusted() 30380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_qnan(self): 30400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is a quiet NaN; otherwise return False.""" 30410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._exp == 'n' 30420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_signed(self): 30440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is negative; otherwise return False.""" 30450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._sign == 1 30460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_snan(self): 30480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is a signaling NaN; otherwise return False.""" 30490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._exp == 'N' 30500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_subnormal(self, context=None): 30520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is subnormal; otherwise return False.""" 30530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or not self: 30540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 30550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 30560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 30570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.adjusted() < context.Emin 30580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_zero(self): 30600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is a zero; otherwise return False.""" 30610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return not self._is_special and self._int == '0' 30620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _ln_exp_bound(self): 30640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compute a lower bound for the adjusted exponent of self.ln(). 30650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao In other words, compute r such that self.ln() >= 10**r. Assumes 30660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao that self is finite and positive and that self != 1. 30670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 30680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for 0.1 <= x <= 10 we use the inequalities 1-1/x <= ln(x) <= x-1 30700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao adj = self._exp + len(self._int) - 1 30710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if adj >= 1: 30720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # argument >= 10; we use 23/10 = 2.3 as a lower bound for ln(10) 30730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(str(adj*23//10)) - 1 30740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if adj <= -2: 30750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # argument <= 0.1 30760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(str((-1-adj)*23//10)) - 1 30770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op = _WorkRep(self) 30780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c, e = op.int, op.exp 30790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if adj == 0: 30800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1 < self < 10 30810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao num = str(c-10**-e) 30820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao den = str(c) 30830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(num) - len(den) - (num < den) 30840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # adj == -1, 0.1 <= self < 1 30850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return e + len(str(10**-e - c)) - 1 30860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def ln(self, context=None): 30890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the natural (base e) logarithm of self.""" 30900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 30920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 30930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ln(NaN) = NaN 30950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 30960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 30970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 30980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ln(0.0) == -Infinity 31000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 31010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeInfinity 31020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ln(Infinity) = Infinity 31040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == 1: 31050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Infinity 31060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ln(1.0) == 0.0 31080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self == _One: 31090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Zero 31100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ln(negative) raises InvalidOperation 31120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign == 1: 31130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 31140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'ln of a negative value') 31150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result is irrational, so necessarily inexact 31170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op = _WorkRep(self) 31180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c, e = op.int, op.exp 31190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = context.prec 31200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # correctly rounded result: repeatedly increase precision by 3 31220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # until we get an unambiguously roundable result 31230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao places = p - self._ln_exp_bound() + 2 # at least p+3 places 31240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while True: 31250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = _dlog(c, e, places) 31260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # assert len(str(abs(coeff)))-p >= 1 31270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if coeff % (5*10**(len(str(abs(coeff)))-p-1)): 31280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 31290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao places += 3 31300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places) 31310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context._shallow_copy() 31330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context._set_rounding(ROUND_HALF_EVEN) 31340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 31350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.rounding = rounding 31360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 31370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _log10_exp_bound(self): 31390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compute a lower bound for the adjusted exponent of self.log10(). 31400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao In other words, find r such that self.log10() >= 10**r. 31410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Assumes that self is finite and positive and that self != 1. 31420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 31430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # For x >= 10 or x < 0.1 we only need a bound on the integer 31450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # part of log10(self), and this comes directly from the 31460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exponent of x. For 0.1 <= x <= 10 we use the inequalities 31470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1-1/x <= log(x) <= x-1. If x > 1 we have |log10(x)| > 31480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (1-1/x)/2.31 > 0. If x < 1 then |log10(x)| > (1-x)/2.31 > 0 31490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao adj = self._exp + len(self._int) - 1 31510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if adj >= 1: 31520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self >= 10 31530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(str(adj))-1 31540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if adj <= -2: 31550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self < 0.1 31560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(str(-1-adj))-1 31570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op = _WorkRep(self) 31580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c, e = op.int, op.exp 31590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if adj == 0: 31600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1 < self < 10 31610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao num = str(c-10**-e) 31620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao den = str(231*c) 31630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(num) - len(den) - (num < den) + 2 31640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # adj == -1, 0.1 <= self < 1 31650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao num = str(10**-e-c) 31660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return len(num) + e - (num < "231") - 1 31670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def log10(self, context=None): 31690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the base 10 logarithm of self.""" 31700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 31720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 31730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log10(NaN) = NaN 31750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 31760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 31770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 31780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log10(0.0) == -Infinity 31800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 31810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeInfinity 31820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log10(Infinity) = Infinity 31840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == 1: 31850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Infinity 31860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log10(negative or -Infinity) raises InvalidOperation 31880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign == 1: 31890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation, 31900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'log10 of a negative value') 31910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 31920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log10(10**n) = n 31930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._int[0] == '1' and self._int[1:] == '0'*(len(self._int) - 1): 31940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # answer may need rounding 31950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = Decimal(self._exp + len(self._int) - 1) 31960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 31970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result is irrational, so necessarily inexact 31980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao op = _WorkRep(self) 31990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c, e = op.int, op.exp 32000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = context.prec 32010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # correctly rounded result: repeatedly increase precision 32030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # until result is unambiguously roundable 32040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao places = p-self._log10_exp_bound()+2 32050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while True: 32060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = _dlog10(c, e, places) 32070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # assert len(str(abs(coeff)))-p >= 1 32080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if coeff % (5*10**(len(str(abs(coeff)))-p-1)): 32090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 32100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao places += 3 32110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = _dec_from_triple(int(coeff<0), str(abs(coeff)), -places) 32120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context._shallow_copy() 32140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context._set_rounding(ROUND_HALF_EVEN) 32150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = ans._fix(context) 32160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context.rounding = rounding 32170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 32180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logb(self, context=None): 32200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ Returns the exponent of the magnitude of self's MSD. 32210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result is the integer which is the exponent of the magnitude 32230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of the most significant digit of self (as though it were truncated 32240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao to a single digit while maintaining the value of that digit and 32250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao without limiting the resulting exponent). 32260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 32270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # logb(NaN) = NaN 32280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 32290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 32300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 32310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 32330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 32340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # logb(+/-Inf) = +Inf 32360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 32370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Infinity 32380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # logb(0) = -Inf, DivisionByZero 32400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self: 32410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(DivisionByZero, 'logb(0)', 1) 32420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # otherwise, simply return the adjusted exponent of self, as a 32440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Decimal. Note that no attempt is made to fit the result 32450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # into the current context. 32460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = Decimal(self.adjusted()) 32470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 32480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _islogical(self): 32500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if self is a logical operand. 32510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao For being logical, it must be a finite number with a sign of 0, 32530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao an exponent of 0, and a coefficient whose digits must all be 32540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao either 0 or 1. 32550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 32560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign != 0 or self._exp != 0: 32570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 32580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for dig in self._int: 32590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if dig not in '01': 32600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return False 32610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return True 32620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _fill_logical(self, context, opa, opb): 32640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dif = context.prec - len(opa) 32650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if dif > 0: 32660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao opa = '0'*dif + opa 32670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif dif < 0: 32680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao opa = opa[-context.prec:] 32690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dif = context.prec - len(opb) 32700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if dif > 0: 32710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao opb = '0'*dif + opb 32720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif dif < 0: 32730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao opb = opb[-context.prec:] 32740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return opa, opb 32750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_and(self, other, context=None): 32770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Applies an 'and' operation between self and other's digits.""" 32780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 32790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 32800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 32820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self._islogical() or not other._islogical(): 32840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 32850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # fill to context.prec 32870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (opa, opb) = self._fill_logical(context, self._int, other._int) 32880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make the operation, and clean starting zeroes 32900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = "".join([str(int(a)&int(b)) for a,b in zip(opa,opb)]) 32910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, result.lstrip('0') or '0', 0) 32920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 32930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_invert(self, context=None): 32940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Invert all its digits.""" 32950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 32960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 32970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.logical_xor(_dec_from_triple(0,'1'*context.prec,0), 32980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context) 32990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_or(self, other, context=None): 33010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Applies an 'or' operation between self and other's digits.""" 33020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 33030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 33040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 33060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self._islogical() or not other._islogical(): 33080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 33090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # fill to context.prec 33110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (opa, opb) = self._fill_logical(context, self._int, other._int) 33120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make the operation, and clean starting zeroes 33140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = "".join([str(int(a)|int(b)) for a,b in zip(opa,opb)]) 33150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, result.lstrip('0') or '0', 0) 33160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_xor(self, other, context=None): 33180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Applies an 'xor' operation between self and other's digits.""" 33190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 33200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 33210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 33230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self._islogical() or not other._islogical(): 33250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 33260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # fill to context.prec 33280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (opa, opb) = self._fill_logical(context, self._int, other._int) 33290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make the operation, and clean starting zeroes 33310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = "".join([str(int(a)^int(b)) for a,b in zip(opa,opb)]) 33320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, result.lstrip('0') or '0', 0) 33330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def max_mag(self, other, context=None): 33350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares the values numerically with their sign ignored.""" 33360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 33370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 33390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 33400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 33420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If one operand is a quiet NaN and the other is number, then the 33430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # number is always returned 33440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sn = self._isnan() 33450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao on = other._isnan() 33460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn or on: 33470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if on == 1 and sn == 0: 33480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._fix(context) 33490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn == 1 and on == 0: 33500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other._fix(context) 33510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._check_nans(other, context) 33520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self.copy_abs()._cmp(other.copy_abs()) 33540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == 0: 33550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self.compare_total(other) 33560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == -1: 33580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = other 33590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 33600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self 33610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 33630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def min_mag(self, other, context=None): 33650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares the values numerically with their sign ignored.""" 33660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 33670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 33690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 33700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special or other._is_special: 33720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If one operand is a quiet NaN and the other is number, then the 33730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # number is always returned 33740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sn = self._isnan() 33750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao on = other._isnan() 33760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn or on: 33770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if on == 1 and sn == 0: 33780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._fix(context) 33790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sn == 1 and on == 0: 33800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other._fix(context) 33810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._check_nans(other, context) 33820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self.copy_abs()._cmp(other.copy_abs()) 33840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == 0: 33850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = self.compare_total(other) 33860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c == -1: 33880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self 33890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 33900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = other 33910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans._fix(context) 33930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next_minus(self, context=None): 33950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the largest representable number smaller than itself.""" 33960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 33970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 33980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 33990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 34000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 34010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 34020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == -1: 34040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _NegativeInfinity 34050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == 1: 34060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(0, '9'*context.prec, context.Etop()) 34070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context.copy() 34090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._set_rounding(ROUND_FLOOR) 34100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._ignore_all_flags() 34110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao new_self = self._fix(context) 34120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if new_self != self: 34130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return new_self 34140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.__sub__(_dec_from_triple(0, '1', context.Etiny()-1), 34150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context) 34160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next_plus(self, context=None): 34180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the smallest representable number larger than itself.""" 34190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 34200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 34210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(context=context) 34230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 34240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 34250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == 1: 34270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _Infinity 34280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity() == -1: 34290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(1, '9'*context.prec, context.Etop()) 34300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context.copy() 34320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._set_rounding(ROUND_CEILING) 34330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._ignore_all_flags() 34340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao new_self = self._fix(context) 34350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if new_self != self: 34360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return new_self 34370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.__add__(_dec_from_triple(0, '1', context.Etiny()-1), 34380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context) 34390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next_toward(self, other, context=None): 34410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the number closest to self, in the direction towards other. 34420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result is the closest representable number to self 34440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (excluding self) that is in the direction towards other, 34450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unless both have the same value. If the two operands are 34460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao numerically equal, then the result is a copy of self with the 34470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign set to be the same as the sign of other. 34480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 34490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 34500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 34520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 34530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 34550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 34560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 34570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao comparison = self._cmp(other) 34590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if comparison == 0: 34600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.copy_sign(other) 34610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if comparison == -1: 34630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self.next_plus(context) 34640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: # comparison == 1 34650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self.next_minus(context) 34660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # decide which flags to raise using value of ans 34680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans._isinfinity(): 34690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Overflow, 34700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Infinite result from next_toward', 34710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans._sign) 34720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 34730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 34740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif ans.adjusted() < context.Emin: 34750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Underflow) 34760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Subnormal) 34770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Inexact) 34780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Rounded) 34790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if precision == 1 then we don't raise Clamped for a 34800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # result 0E-Etiny. 34810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not ans: 34820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._raise_error(Clamped) 34830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 34850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def number_class(self, context=None): 34870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns an indication of the class of self. 34880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 34890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The class is one of the following strings: 34900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sNaN 34910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaN 34920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Infinity 34930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Normal 34940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Subnormal 34950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Zero 34960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Zero 34970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Subnormal 34980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Normal 34990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Infinity 35000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 35010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.is_snan(): 35020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "sNaN" 35030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.is_qnan(): 35040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "NaN" 35050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao inf = self._isinfinity() 35060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if inf == 1: 35070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "+Infinity" 35080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if inf == -1: 35090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "-Infinity" 35100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.is_zero(): 35110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 35120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "-Zero" 35130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 35140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "+Zero" 35150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 35160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 35170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.is_subnormal(context=context): 35180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 35190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "-Subnormal" 35200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 35210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "+Subnormal" 35220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # just a normal, regular, boring number, :) 35230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._sign: 35240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "-Normal" 35250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 35260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "+Normal" 35270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def radix(self): 35290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Just returns 10, as this is Decimal, :)""" 35300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(10) 35310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def rotate(self, other, context=None): 35330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a rotated copy of self, value-of-other times.""" 35340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 35350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 35360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 35380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 35400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 35410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 35420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._exp != 0: 35440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 35450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not (-context.prec <= int(other) <= context.prec): 35460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 35470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 35490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 35500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # get values, pad if necessary 35520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao torot = int(other) 35530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotdig = self._int 35540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao topad = context.prec - len(rotdig) 35550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if topad > 0: 35560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotdig = '0'*topad + rotdig 35570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif topad < 0: 35580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotdig = rotdig[-topad:] 35590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # let's rotate! 35610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotated = rotdig[torot:] + rotdig[:torot] 35620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, 35630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotated.lstrip('0') or '0', self._exp) 35640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def scaleb(self, other, context=None): 35660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns self operand after adding the second value to its exp.""" 35670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 35680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 35690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 35710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 35730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 35740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 35750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._exp != 0: 35770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 35780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao liminf = -2 * (context.Emax + context.prec) 35790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao limsup = 2 * (context.Emax + context.prec) 35800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not (liminf <= int(other) <= limsup): 35810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 35820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 35840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 35850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = _dec_from_triple(self._sign, self._int, self._exp + int(other)) 35870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = d._fix(context) 35880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return d 35890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def shift(self, other, context=None): 35910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a shifted copy of self, value-of-other times.""" 35920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 35930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 35940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = _convert_other(other, raiseit=True) 35960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 35970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ans = self._check_nans(other, context) 35980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ans: 35990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ans 36000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other._exp != 0: 36020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 36030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not (-context.prec <= int(other) <= context.prec): 36040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return context._raise_error(InvalidOperation) 36050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._isinfinity(): 36070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(self) 36080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # get values, pad if necessary 36100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao torot = int(other) 36110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotdig = self._int 36120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao topad = context.prec - len(rotdig) 36130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if topad > 0: 36140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotdig = '0'*topad + rotdig 36150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif topad < 0: 36160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotdig = rotdig[-topad:] 36170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # let's shift! 36190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if torot < 0: 36200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shifted = rotdig[:torot] 36210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 36220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shifted = rotdig + '0'*torot 36230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shifted = shifted[-context.prec:] 36240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _dec_from_triple(self._sign, 36260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shifted.lstrip('0') or '0', self._exp) 36270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Support for pickling, copy, and deepcopy 36290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 36300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.__class__, (str(self),)) 36310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __copy__(self): 36330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if type(self) is Decimal: 36340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self # I'm immutable; therefore I am my own clone 36350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.__class__(str(self)) 36360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __deepcopy__(self, memo): 36380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if type(self) is Decimal: 36390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self # My components are also immutable 36400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.__class__(str(self)) 36410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # PEP 3101 support. the _localeconv keyword argument should be 36430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # considered private: it's provided for ease of testing only. 36440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __format__(self, specifier, context=None, _localeconv=None): 36450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Format a Decimal instance according to the given specifier. 36460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The specifier should be a standard format specifier, with the 36480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao form described in PEP 3101. Formatting types 'e', 'E', 'f', 36490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'F', 'g', 'G', 'n' and '%' are supported. If the formatting 36500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type is omitted it defaults to 'g' or 'G', depending on the 36510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value of context.capitals. 36520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 36530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Note: PEP 3101 says that if the type is not present then 36550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # there should be at least one digit after the decimal point. 36560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We take the liberty of ignoring this requirement for 36570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Decimal---it's presumably there to make sure that 36580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # format(float, '') behaves similarly to str(float). 36590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if context is None: 36600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = getcontext() 36610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao spec = _parse_format_specifier(specifier, _localeconv=_localeconv) 36630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # special values don't care about the type or precision 36650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._is_special: 36660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = _format_sign(self._sign, spec) 36670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao body = str(self.copy_abs()) 36680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _format_align(sign, body, spec) 36690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # a type of None defaults to 'g' or 'G', depending on context 36710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if spec['type'] is None: 36720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao spec['type'] = ['g', 'G'][context.capitals] 36730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if type is '%', adjust exponent of self accordingly 36750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if spec['type'] == '%': 36760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = _dec_from_triple(self._sign, self._int, self._exp+2) 36770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # round if necessary, taking rounding mode from the context 36790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context.rounding 36800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao precision = spec['precision'] 36810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if precision is not None: 36820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if spec['type'] in 'eE': 36830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = self._round(precision+1, rounding) 36840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif spec['type'] in 'fF%': 36850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = self._rescale(-precision, rounding) 36860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif spec['type'] in 'gG' and len(self._int) > precision: 36870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = self._round(precision, rounding) 36880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # special case: zeros with a positive exponent can't be 36890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # represented in fixed point; rescale them to 0e0. 36900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self and self._exp > 0 and spec['type'] in 'fF%': 36910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = self._rescale(0, rounding) 36920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 36930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # figure out placement of the decimal point 36940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao leftdigits = self._exp + len(self._int) 36950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if spec['type'] in 'eE': 36960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self and precision is not None: 36970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = 1 - precision 36980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 36990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = 1 37000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif spec['type'] in 'fF%': 37010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = leftdigits 37020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif spec['type'] in 'gG': 37030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self._exp <= 0 and leftdigits > -6: 37040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = leftdigits 37050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 37060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dotplace = 1 37070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # find digits before and after decimal point, and get exponent 37090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if dotplace < 0: 37100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = '0' 37110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = '0'*(-dotplace) + self._int 37120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif dotplace > len(self._int): 37130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = self._int + '0'*(dotplace-len(self._int)) 37140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = '' 37150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 37160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = self._int[:dotplace] or '0' 37170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = self._int[dotplace:] 37180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = leftdigits-dotplace 37190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # done with the decimal-specific stuff; hand over the rest 37210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # of the formatting to the _format_number function 37220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _format_number(self._sign, intpart, fracpart, exp, spec) 37230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _dec_from_triple(sign, coefficient, exponent, special=False): 37250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Create a decimal instance directly, without any validation, 37260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao normalization (e.g. removal of leading zeros) or argument 37270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao conversion. 37280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This function is for *internal use only*. 37300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 37310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self = object.__new__(Decimal) 37330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._sign = sign 37340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._int = coefficient 37350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exp = exponent 37360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._is_special = special 37370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 37390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Register Decimal as a kind of Number (an abstract base class). 37410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# However, do not register it as Real (because Decimals are not 37420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# interoperable with floats). 37430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_numbers.Number.register(Decimal) 37440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### Context class ####################################################### 37470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _ContextManager(object): 37490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Context manager class to support localcontext(). 37500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Sets a copy of the supplied context in __enter__() and restores 37520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the previous decimal context in __exit__() 37530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 37540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, new_context): 37550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.new_context = new_context.copy() 37560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __enter__(self): 37570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.saved_context = getcontext() 37580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao setcontext(self.new_context) 37590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.new_context 37600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __exit__(self, t, v, tb): 37610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao setcontext(self.saved_context) 37620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37630a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Context(object): 37640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Contains the context for a Decimal instance. 37650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Contains: 37670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao prec - precision (for use in rounding, division, square roots..) 37680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding - rounding type (how you round) 37690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao traps - If traps[exception] = 1, then the exception is 37700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raised when it is caused. Otherwise, a value is 37710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao substituted in. 37720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao flags - When an exception is caused, flags[exception] is set. 37730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (Whether or not the trap_enabler is set) 37740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Should be reset by user of Decimal instance. 37750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Emin - Minimum exponent 37760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Emax - Maximum exponent 37770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao capitals - If 1, 1*10^1 is printed as 1E+1. 37780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If 0, printed as 1e1 37790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _clamp - If 1, change exponents if too high (Default 0) 37800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 37810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, prec=None, rounding=None, 37830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao traps=None, flags=None, 37840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Emin=None, Emax=None, 37850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao capitals=None, _clamp=0, 37860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _ignored_flags=None): 37870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Set defaults; for everything except flags and _ignored_flags, 37880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # inherit from DefaultContext. 37890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 37900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dc = DefaultContext 37910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except NameError: 37920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 37930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 37940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.prec = prec if prec is not None else dc.prec 37950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.rounding = rounding if rounding is not None else dc.rounding 37960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.Emin = Emin if Emin is not None else dc.Emin 37970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.Emax = Emax if Emax is not None else dc.Emax 37980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.capitals = capitals if capitals is not None else dc.capitals 37990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._clamp = _clamp if _clamp is not None else dc._clamp 38000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _ignored_flags is None: 38020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._ignored_flags = [] 38030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 38040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._ignored_flags = _ignored_flags 38050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if traps is None: 38070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.traps = dc.traps.copy() 38080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif not isinstance(traps, dict): 38090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.traps = dict((s, int(s in traps)) for s in _signals) 38100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 38110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.traps = traps 38120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if flags is None: 38140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.flags = dict.fromkeys(_signals, 0) 38150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif not isinstance(flags, dict): 38160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.flags = dict((s, int(s in flags)) for s in _signals) 38170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 38180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.flags = flags 38190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 38210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Show the current context.""" 38220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = [] 38230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.append('Context(prec=%(prec)d, rounding=%(rounding)s, ' 38240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d' 38250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao % vars(self)) 38260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao names = [f.__name__ for f, v in self.flags.items() if v] 38270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.append('flags=[' + ', '.join(names) + ']') 38280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao names = [t.__name__ for t, v in self.traps.items() if v] 38290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.append('traps=[' + ', '.join(names) + ']') 38300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ', '.join(s) + ')' 38310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def clear_flags(self): 38330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Reset all flags to zero""" 38340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for flag in self.flags: 38350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.flags[flag] = 0 38360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _shallow_copy(self): 38380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a shallow copy from self.""" 38390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nc = Context(self.prec, self.rounding, self.traps, 38400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.flags, self.Emin, self.Emax, 38410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.capitals, self._clamp, self._ignored_flags) 38420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return nc 38430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy(self): 38450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a deep copy from self.""" 38460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nc = Context(self.prec, self.rounding, self.traps.copy(), 38470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.flags.copy(), self.Emin, self.Emax, 38480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.capitals, self._clamp, self._ignored_flags) 38490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return nc 38500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __copy__ = copy 38510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _raise_error(self, condition, explanation = None, *args): 38530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Handles an error 38540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If the flag is in _ignored_flags, returns the default response. 38560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Otherwise, it sets the flag, then, if the corresponding 38570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao trap_enabler is set, it reraises the exception. Otherwise, it returns 38580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the default value after setting the flag. 38590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 38600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao error = _condition_map.get(condition, condition) 38610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if error in self._ignored_flags: 38620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Don't touch the flag 38630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return error().handle(self, *args) 38640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.flags[error] = 1 38660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not self.traps[error]: 38670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The errors define how to handle themselves. 38680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return condition().handle(self, *args) 38690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Errors should only be risked on copies of the context 38710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # self._ignored_flags = [] 38720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise error(explanation) 38730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _ignore_all_flags(self): 38750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Ignore all flags, if they are raised""" 38760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._ignore_flags(*_signals) 38770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _ignore_flags(self, *flags): 38790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Ignore the flags, if they are raised""" 38800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Do not mutate-- This way, copies of a context leave the original 38810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # alone. 38820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._ignored_flags = (self._ignored_flags + list(flags)) 38830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return list(flags) 38840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _regard_flags(self, *flags): 38860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Stop ignoring the flags, if they are raised""" 38870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if flags and isinstance(flags[0], (tuple,list)): 38880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao flags = flags[0] 38890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for flag in flags: 38900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._ignored_flags.remove(flag) 38910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We inherit object.__hash__, so we must deny this explicitly 38930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __hash__ = None 38940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def Etiny(self): 38960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns Etiny (= Emin - prec + 1)""" 38970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int(self.Emin - self.prec + 1) 38980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 38990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def Etop(self): 39000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns maximum exponent (= Emax - prec + 1)""" 39010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int(self.Emax - self.prec + 1) 39020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _set_rounding(self, type): 39040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Sets the rounding type. 39050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Sets the rounding type, and returns the current (previous) 39070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding type. Often used like: 39080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context = context.copy() 39100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # so you don't change the calling context 39110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if an error occurs in the middle. 39120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = context._set_rounding(ROUND_UP) 39130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao val = self.__sub__(other, context=context) 39140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context._set_rounding(rounding) 39150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This will make it round up for that operation. 39170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 39180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rounding = self.rounding 39190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.rounding= type 39200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return rounding 39210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def create_decimal(self, num='0'): 39230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Creates a new Decimal instance but using self as context. 39240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This method implements the to-number operation of the 39260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao IBM Decimal specification.""" 39270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(num, basestring) and num != num.strip(): 39290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._raise_error(ConversionSyntax, 39300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "no trailing or leading whitespace is " 39310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "permitted.") 39320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = Decimal(num, context=self) 39340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if d._isnan() and len(d._int) > self.prec - self._clamp: 39350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._raise_error(ConversionSyntax, 39360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "diagnostic info too long in NaN") 39370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return d._fix(self) 39380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def create_decimal_from_float(self, f): 39400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Creates a new Decimal instance from a float but rounding using self 39410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao as the context. 39420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> context = Context(prec=5, rounding=ROUND_DOWN) 39440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> context.create_decimal_from_float(3.1415926535897932) 39450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3.1415') 39460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> context = Context(prec=5, traps=[Inexact]) 39470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> context.create_decimal_from_float(3.1415926535897932) 39480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Traceback (most recent call last): 39490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ... 39500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Inexact: None 39510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 39530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = Decimal.from_float(f) # An exact conversion 39540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return d._fix(self) # Apply the context rounding 39550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Methods 39570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def abs(self, a): 39580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the absolute value of the operand. 39590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If the operand is negative, the result is the same as using the minus 39610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation on the operand. Otherwise, the result is the same as using 39620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the plus operation on the operand. 39630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.abs(Decimal('2.1')) 39650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.1') 39660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.abs(Decimal('-100')) 39670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 39680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.abs(Decimal('101.5')) 39690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('101.5') 39700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.abs(Decimal('-101.5')) 39710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('101.5') 39720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.abs(-1) 39730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 39740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 39750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 39760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.__abs__(context=self) 39770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def add(self, a, b): 39790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return the sum of the two operands. 39800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.add(Decimal('12'), Decimal('7.00')) 39820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('19.00') 39830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4')) 39840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.02E+4') 39850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.add(1, Decimal(2)) 39860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 39870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.add(Decimal(8), 5) 39880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('13') 39890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.add(5, 5) 39900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('10') 39910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 39920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 39930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__add__(b, context=self) 39940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 39950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 39960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 39970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 39980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 39990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _apply(self, a): 40000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return str(a._fix(self)) 40010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def canonical(self, a): 40030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the same Decimal object. 40040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao As we do not have different encodings for the same number, the 40060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao received object already is in its canonical form. 40070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.canonical(Decimal('2.50')) 40090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.50') 40100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 40110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.canonical(context=self) 40120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare(self, a, b): 40140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares values numerically. 40150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If the signs of the operands differ, a value representing each operand 40170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ('-1' if the operand is less than zero, '0' if the operand is zero or 40180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao negative zero, or '1' if the operand is greater than zero) is used in 40190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao place of that operand for the comparison instead of the actual 40200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operand. 40210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The comparison is then effected by subtracting the second operand from 40230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the first and then returning a value according to the result of the 40240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao subtraction: '-1' if the result is less than zero, '0' if the result is 40250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao zero or negative zero, or '1' if the result is greater than zero. 40260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3')) 40280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1')) 40300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 40310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10')) 40320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 40330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1')) 40340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 40350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3')) 40360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 40370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1')) 40380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(1, 2) 40400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(Decimal(1), 2) 40420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare(1, Decimal(2)) 40440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 40460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 40470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.compare(b, context=self) 40480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare_signal(self, a, b): 40500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares the values of the two operands numerically. 40510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao It's pretty much like compare(), but all NaNs signal, with signaling 40530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaNs taking precedence over quiet NaNs. 40540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext 40560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.compare_signal(Decimal('2.1'), Decimal('3')) 40570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.compare_signal(Decimal('2.1'), Decimal('2.1')) 40590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 40600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.flags[InvalidOperation] = 0 40610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> print c.flags[InvalidOperation] 40620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0 40630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.compare_signal(Decimal('NaN'), Decimal('2.1')) 40640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('NaN') 40650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> print c.flags[InvalidOperation] 40660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1 40670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.flags[InvalidOperation] = 0 40680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> print c.flags[InvalidOperation] 40690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 0 40700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1')) 40710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('NaN') 40720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> print c.flags[InvalidOperation] 40730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1 40740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.compare_signal(-1, 2) 40750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.compare_signal(Decimal(-1), 2) 40770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.compare_signal(-1, Decimal(2)) 40790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 40810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 40820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.compare_signal(b, context=self) 40830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare_total(self, a, b): 40850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares two operands using their abstract representation. 40860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This is not like the standard compare, which use their numerical 40880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value. Note that a total ordering is defined for all possible abstract 40890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao representations. 40900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 40910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9')) 40920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12')) 40940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3')) 40960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 40970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30')) 40980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 40990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300')) 41000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 41010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN')) 41020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(1, 2) 41040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(Decimal(1), 2) 41060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.compare_total(1, Decimal(2)) 41080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 41100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 41110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.compare_total(b) 41120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def compare_total_mag(self, a, b): 41140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares two operands using their abstract representation ignoring sign. 41150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Like compare_total, but with operand's sign ignored and assumed to be 0. 41170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 41180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 41190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.compare_total_mag(b) 41200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy_abs(self, a): 41220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a copy of the operand with the sign set to 0. 41230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_abs(Decimal('2.1')) 41250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.1') 41260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_abs(Decimal('-100')) 41270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 41280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_abs(-1) 41290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 41300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 41310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 41320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.copy_abs() 41330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy_decimal(self, a): 41350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a copy of the decimal object. 41360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_decimal(Decimal('2.1')) 41380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.1') 41390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_decimal(Decimal('-1.00')) 41400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.00') 41410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_decimal(1) 41420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 41430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 41440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 41450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(a) 41460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy_negate(self, a): 41480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a copy of the operand with the sign inverted. 41490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_negate(Decimal('101.5')) 41510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-101.5') 41520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_negate(Decimal('-101.5')) 41530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('101.5') 41540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_negate(1) 41550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 41570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 41580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.copy_negate() 41590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def copy_sign(self, a, b): 41610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Copies the second operand's sign to the first one. 41620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao In detail, it returns a copy of the first operand with the sign 41640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao equal to the sign of the second operand. 41650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33')) 41670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.50') 41680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33')) 41690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.50') 41700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33')) 41710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.50') 41720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33')) 41730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.50') 41740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_sign(1, -2) 41750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_sign(Decimal(1), -2) 41770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.copy_sign(1, Decimal(-2)) 41790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 41800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 41810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 41820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.copy_sign(b) 41830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def divide(self, a, b): 41850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Decimal division in a specified context. 41860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 41870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('1'), Decimal('3')) 41880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.333333333') 41890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('2'), Decimal('3')) 41900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.666666667') 41910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('5'), Decimal('2')) 41920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.5') 41930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('1'), Decimal('10')) 41940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.1') 41950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('12'), Decimal('12')) 41960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 41970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2')) 41980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('4.00') 41990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0')) 42000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.20') 42010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('1000'), Decimal('100')) 42020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('10') 42030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('1000'), Decimal('1')) 42040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1000') 42050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2')) 42060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.20E+6') 42070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(5, 5) 42080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 42090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(Decimal(5), 5) 42100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 42110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide(5, Decimal(5)) 42120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 42130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 42140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 42150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__div__(b, context=self) 42160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 42170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 42180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 42190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 42200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def divide_int(self, a, b): 42220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Divides two numbers and returns the integer part of the result. 42230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3')) 42250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 42260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3')) 42270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 42280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3')) 42290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 42300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide_int(10, 3) 42310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 42320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide_int(Decimal(10), 3) 42330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 42340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divide_int(10, Decimal(3)) 42350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 42360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 42370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 42380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__floordiv__(b, context=self) 42390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 42400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 42410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 42420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 42430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def divmod(self, a, b): 42450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return (a // b, a % b). 42460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divmod(Decimal(8), Decimal(3)) 42480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (Decimal('2'), Decimal('2')) 42490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divmod(Decimal(8), Decimal(4)) 42500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (Decimal('2'), Decimal('0')) 42510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divmod(8, 4) 42520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (Decimal('2'), Decimal('0')) 42530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divmod(Decimal(8), 4) 42540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (Decimal('2'), Decimal('0')) 42550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.divmod(8, Decimal(4)) 42560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (Decimal('2'), Decimal('0')) 42570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 42580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 42590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__divmod__(b, context=self) 42600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 42610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 42620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 42630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 42640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def exp(self, a): 42660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns e ** a. 42670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 42690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 42700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 42710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.exp(Decimal('-Infinity')) 42720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 42730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.exp(Decimal('-1')) 42740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.367879441') 42750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.exp(Decimal('0')) 42760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 42770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.exp(Decimal('1')) 42780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.71828183') 42790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.exp(Decimal('0.693147181')) 42800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.00000000') 42810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.exp(Decimal('+Infinity')) 42820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('Infinity') 42830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.exp(10) 42840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('22026.4658') 42850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 42860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a =_convert_other(a, raiseit=True) 42870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.exp(context=self) 42880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def fma(self, a, b, c): 42900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a multiplied by b, plus c. 42910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The first two operands are multiplied together, using multiply, 42930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the third operand is then added to the result of that 42940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao multiplication, using add, all with only one final rounding. 42950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 42960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7')) 42970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('22') 42980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7')) 42990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-8') 43000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578')) 43010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.38435736E+12') 43020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.fma(1, 3, 4) 43030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7') 43040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.fma(1, Decimal(3), 4) 43050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7') 43060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.fma(1, 3, Decimal(4)) 43070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7') 43080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 43090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 43100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.fma(b, c, context=self) 43110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_canonical(self, a): 43130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is canonical; otherwise return False. 43140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Currently, the encoding of a Decimal instance is always 43160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao canonical, so this method returns True for any Decimal. 43170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_canonical(Decimal('2.50')) 43190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 43210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_canonical() 43220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_finite(self, a): 43240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is finite; otherwise return False. 43250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao A Decimal instance is considered finite if it is neither 43270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao infinite nor a NaN. 43280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_finite(Decimal('2.50')) 43300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_finite(Decimal('-0.3')) 43320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_finite(Decimal('0')) 43340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_finite(Decimal('Inf')) 43360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_finite(Decimal('NaN')) 43380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_finite(1) 43400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 43420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 43430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_finite() 43440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_infinite(self, a): 43460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is infinite; otherwise return False. 43470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_infinite(Decimal('2.50')) 43490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_infinite(Decimal('-Inf')) 43510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_infinite(Decimal('NaN')) 43530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_infinite(1) 43550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 43570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 43580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_infinite() 43590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_nan(self, a): 43610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is a qNaN or sNaN; 43620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao otherwise return False. 43630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_nan(Decimal('2.50')) 43650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_nan(Decimal('NaN')) 43670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_nan(Decimal('-sNaN')) 43690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_nan(1) 43710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 43730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 43740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_nan() 43750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_normal(self, a): 43770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is a normal number; 43780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao otherwise return False. 43790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 43810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 43820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 43830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_normal(Decimal('2.50')) 43840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_normal(Decimal('0.1E-999')) 43860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_normal(Decimal('0.00')) 43880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_normal(Decimal('-Inf')) 43900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_normal(Decimal('NaN')) 43920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 43930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_normal(1) 43940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 43950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 43960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 43970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_normal(context=self) 43980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 43990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_qnan(self, a): 44000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is a quiet NaN; otherwise return False. 44010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_qnan(Decimal('2.50')) 44030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_qnan(Decimal('NaN')) 44050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_qnan(Decimal('sNaN')) 44070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_qnan(1) 44090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 44110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 44120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_qnan() 44130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_signed(self, a): 44150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is negative; otherwise return False. 44160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_signed(Decimal('2.50')) 44180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_signed(Decimal('-12')) 44200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_signed(Decimal('-0')) 44220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_signed(8) 44240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_signed(-8) 44260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 44280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 44290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_signed() 44300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_snan(self, a): 44320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is a signaling NaN; 44330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao otherwise return False. 44340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_snan(Decimal('2.50')) 44360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_snan(Decimal('NaN')) 44380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_snan(Decimal('sNaN')) 44400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_snan(1) 44420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 44440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 44450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_snan() 44460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_subnormal(self, a): 44480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is subnormal; otherwise return False. 44490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 44510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 44520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 44530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_subnormal(Decimal('2.50')) 44540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_subnormal(Decimal('0.1E-999')) 44560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_subnormal(Decimal('0.00')) 44580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_subnormal(Decimal('-Inf')) 44600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_subnormal(Decimal('NaN')) 44620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.is_subnormal(1) 44640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 44660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 44670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_subnormal(context=self) 44680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def is_zero(self, a): 44700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return True if the operand is a zero; otherwise return False. 44710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_zero(Decimal('0')) 44730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_zero(Decimal('2.50')) 44750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_zero(Decimal('-0E+2')) 44770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_zero(1) 44790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 44800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.is_zero(0) 44810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 44820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 44830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 44840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.is_zero() 44850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def ln(self, a): 44870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the natural (base e) logarithm of the operand. 44880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 44890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 44900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 44910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 44920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.ln(Decimal('0')) 44930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 44940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.ln(Decimal('1.000')) 44950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 44960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.ln(Decimal('2.71828183')) 44970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.00000000') 44980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.ln(Decimal('10')) 44990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.30258509') 45000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.ln(Decimal('+Infinity')) 45010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('Infinity') 45020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.ln(1) 45030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 45050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 45060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.ln(context=self) 45070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def log10(self, a): 45090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the base 10 logarithm of the operand. 45100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 45120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 45130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 45140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(Decimal('0')) 45150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 45160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(Decimal('0.001')) 45170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-3') 45180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(Decimal('1.000')) 45190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(Decimal('2')) 45210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.301029996') 45220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(Decimal('10')) 45230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 45240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(Decimal('70')) 45250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.84509804') 45260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(Decimal('+Infinity')) 45270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('Infinity') 45280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(0) 45290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 45300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.log10(1) 45310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 45330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 45340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.log10(context=self) 45350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logb(self, a): 45370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ Returns the exponent of the magnitude of the operand's MSD. 45380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result is the integer which is the exponent of the magnitude 45400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of the most significant digit of the operand (as though the 45410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operand were truncated to a single digit while maintaining the 45420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value of that digit and without limiting the resulting exponent). 45430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logb(Decimal('250')) 45450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 45460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logb(Decimal('2.50')) 45470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logb(Decimal('0.03')) 45490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-2') 45500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logb(Decimal('0')) 45510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 45520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logb(1) 45530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logb(10) 45550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 45560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logb(100) 45570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 45580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 45590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 45600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.logb(context=self) 45610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_and(self, a, b): 45630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Applies the logical operation 'and' between each operand's digits. 45640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operands must be both logical numbers. 45660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0')) 45680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1')) 45700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0')) 45720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 45730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1')) 45740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 45750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010')) 45760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1000') 45770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10')) 45780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('10') 45790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(110, 1101) 45800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 45810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(Decimal(110), 1101) 45820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 45830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_and(110, Decimal(1101)) 45840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 45850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 45860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 45870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.logical_and(b, context=self) 45880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_invert(self, a): 45900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Invert all the digits in the operand. 45910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operand must be a logical number. 45930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 45940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_invert(Decimal('0')) 45950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('111111111') 45960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_invert(Decimal('1')) 45970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('111111110') 45980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_invert(Decimal('111111111')) 45990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 46000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_invert(Decimal('101010101')) 46010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('10101010') 46020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_invert(1101) 46030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('111110010') 46040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 46050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 46060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.logical_invert(context=self) 46070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_or(self, a, b): 46090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Applies the logical operation 'or' between each operand's digits. 46100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operands must be both logical numbers. 46120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0')) 46140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 46150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1')) 46160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 46170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0')) 46180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 46190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1')) 46200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 46210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010')) 46220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1110') 46230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10')) 46240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1110') 46250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(110, 1101) 46260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1111') 46270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(Decimal(110), 1101) 46280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1111') 46290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_or(110, Decimal(1101)) 46300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1111') 46310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 46320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 46330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.logical_or(b, context=self) 46340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def logical_xor(self, a, b): 46360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Applies the logical operation 'xor' between each operand's digits. 46370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operands must be both logical numbers. 46390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0')) 46410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 46420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1')) 46430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 46440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0')) 46450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 46460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1')) 46470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 46480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010')) 46490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('110') 46500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10')) 46510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1101') 46520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(110, 1101) 46530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1011') 46540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(Decimal(110), 1101) 46550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1011') 46560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.logical_xor(110, Decimal(1101)) 46570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1011') 46580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 46590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 46600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.logical_xor(b, context=self) 46610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def max(self, a, b): 46630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """max compares two values numerically and returns the maximum. 46640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If either operand is a NaN then the general rules apply. 46660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Otherwise, the operands are compared as though by the compare 46670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation. If they are numerically equal then the left-hand operand 46680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is chosen as the result. Otherwise the maximum (closer to positive 46690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao infinity) of the two operands is chosen as the result. 46700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max(Decimal('3'), Decimal('2')) 46720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 46730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max(Decimal('-10'), Decimal('3')) 46740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 46750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max(Decimal('1.0'), Decimal('1')) 46760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 46770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max(Decimal('7'), Decimal('NaN')) 46780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7') 46790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max(1, 2) 46800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 46810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max(Decimal(1), 2) 46820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 46830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max(1, Decimal(2)) 46840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 46850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 46860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 46870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.max(b, context=self) 46880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def max_mag(self, a, b): 46900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares the values numerically with their sign ignored. 46910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 46920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN')) 46930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7') 46940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10')) 46950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-10') 46960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max_mag(1, -2) 46970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-2') 46980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max_mag(Decimal(1), -2) 46990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-2') 47000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.max_mag(1, Decimal(-2)) 47010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-2') 47020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 47030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 47040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.max_mag(b, context=self) 47050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def min(self, a, b): 47070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """min compares two values numerically and returns the minimum. 47080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If either operand is a NaN then the general rules apply. 47100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Otherwise, the operands are compared as though by the compare 47110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation. If they are numerically equal then the left-hand operand 47120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is chosen as the result. Otherwise the minimum (closer to negative 47130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao infinity) of the two operands is chosen as the result. 47140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min(Decimal('3'), Decimal('2')) 47160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 47170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min(Decimal('-10'), Decimal('3')) 47180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-10') 47190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min(Decimal('1.0'), Decimal('1')) 47200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.0') 47210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min(Decimal('7'), Decimal('NaN')) 47220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7') 47230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min(1, 2) 47240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 47250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min(Decimal(1), 2) 47260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 47270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min(1, Decimal(29)) 47280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 47290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 47300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 47310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.min(b, context=self) 47320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def min_mag(self, a, b): 47340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compares the values numerically with their sign ignored. 47350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2')) 47370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-2') 47380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN')) 47390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-3') 47400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min_mag(1, -2) 47410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 47420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min_mag(Decimal(1), -2) 47430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 47440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.min_mag(1, Decimal(-2)) 47450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 47460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 47470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 47480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.min_mag(b, context=self) 47490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def minus(self, a): 47510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Minus corresponds to unary prefix minus in Python. 47520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operation is evaluated using the same rules as subtract; the 47540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation minus(a) is calculated as subtract('0', a) where the '0' 47550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao has the same exponent as the operand. 47560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.minus(Decimal('1.3')) 47580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.3') 47590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.minus(Decimal('-1.3')) 47600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.3') 47610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.minus(1) 47620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 47630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 47640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 47650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.__neg__(context=self) 47660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def multiply(self, a, b): 47680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """multiply multiplies two operands. 47690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If either operand is a special value then the general rules apply. 47710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Otherwise, the operands are multiplied together 47720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ('long multiplication'), resulting in a number which may be as long as 47730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the sum of the lengths of the two operands. 47740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3')) 47760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3.60') 47770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(Decimal('7'), Decimal('3')) 47780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('21') 47790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8')) 47800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.72') 47810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0')) 47820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0.0') 47830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321')) 47840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('4.28135971E+11') 47850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(7, 7) 47860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('49') 47870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(Decimal(7), 7) 47880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('49') 47890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.multiply(7, Decimal(7)) 47900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('49') 47910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 47920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 47930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__mul__(b, context=self) 47940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 47950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 47960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 47970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 47980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 47990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next_minus(self, a): 48000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the largest representable number smaller than a. 48010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 48030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 48040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 48050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.next_minus(Decimal('1')) 48060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.999999999') 48070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_minus(Decimal('1E-1007')) 48080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0E-1007') 48090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.next_minus(Decimal('-1.00000003')) 48100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.00000004') 48110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_minus(Decimal('Infinity')) 48120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('9.99999999E+999') 48130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_minus(1) 48140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.999999999') 48150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 48160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 48170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.next_minus(context=self) 48180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next_plus(self, a): 48200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the smallest representable number larger than a. 48210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 48230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 48240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 48250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.next_plus(Decimal('1')) 48260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.00000001') 48270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_plus(Decimal('-1E-1007')) 48280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0E-1007') 48290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.next_plus(Decimal('-1.00000003')) 48300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.00000002') 48310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_plus(Decimal('-Infinity')) 48320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-9.99999999E+999') 48330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_plus(1) 48340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.00000001') 48350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 48360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 48370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.next_plus(context=self) 48380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next_toward(self, a, b): 48400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the number closest to a, in direction towards b. 48410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result is the closest representable number from the first 48430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operand (but not the first operand) that is in the direction 48440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao towards the second operand, unless the operands have the same 48450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value. 48460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 48480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 48490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 48500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal('1'), Decimal('2')) 48510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.00000001') 48520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal('-1E-1007'), Decimal('1')) 48530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0E-1007') 48540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal('-1.00000003'), Decimal('0')) 48550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.00000002') 48560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal('1'), Decimal('0')) 48570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.999999999') 48580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal('1E-1007'), Decimal('-100')) 48590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0E-1007') 48600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10')) 48610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.00000004') 48620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000')) 48630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0.00') 48640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(0, 1) 48650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1E-1007') 48660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(Decimal(0), 1) 48670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1E-1007') 48680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.next_toward(0, Decimal(1)) 48690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1E-1007') 48700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 48710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 48720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.next_toward(b, context=self) 48730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def normalize(self, a): 48750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """normalize reduces an operand to its simplest form. 48760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Essentially a plus operation with all trailing zeros removed from the 48780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result. 48790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.normalize(Decimal('2.1')) 48810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.1') 48820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.normalize(Decimal('-2.0')) 48830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-2') 48840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.normalize(Decimal('1.200')) 48850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.2') 48860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.normalize(Decimal('-120')) 48870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.2E+2') 48880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.normalize(Decimal('120.00')) 48890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.2E+2') 48900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.normalize(Decimal('0.00')) 48910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 48920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.normalize(6) 48930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('6') 48940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 48950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 48960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.normalize(context=self) 48970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 48980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def number_class(self, a): 48990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns an indication of the class of the operand. 49000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The class is one of the following strings: 49020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -sNaN 49030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -NaN 49040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Infinity 49050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Normal 49060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Subnormal 49070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao -Zero 49080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Zero 49090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Subnormal 49100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Normal 49110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao +Infinity 49120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = Context(ExtendedContext) 49140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 49150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 49160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('Infinity')) 49170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '+Infinity' 49180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('1E-10')) 49190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '+Normal' 49200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('2.50')) 49210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '+Normal' 49220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('0.1E-999')) 49230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '+Subnormal' 49240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('0')) 49250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '+Zero' 49260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('-0')) 49270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '-Zero' 49280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('-0.1E-999')) 49290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '-Subnormal' 49300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('-1E-10')) 49310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '-Normal' 49320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('-2.50')) 49330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '-Normal' 49340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('-Infinity')) 49350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '-Infinity' 49360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('NaN')) 49370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'NaN' 49380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('-NaN')) 49390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'NaN' 49400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(Decimal('sNaN')) 49410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'sNaN' 49420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.number_class(123) 49430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '+Normal' 49440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 49450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 49460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.number_class(context=self) 49470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def plus(self, a): 49490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Plus corresponds to unary prefix plus in Python. 49500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operation is evaluated using the same rules as add; the 49520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation plus(a) is calculated as add('0', a) where the '0' 49530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao has the same exponent as the operand. 49540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.plus(Decimal('1.3')) 49560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.3') 49570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.plus(Decimal('-1.3')) 49580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1.3') 49590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.plus(-1) 49600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 49610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 49620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 49630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.__pos__(context=self) 49640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def power(self, a, b, modulo=None): 49660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Raises a to the power of b, to modulo if given. 49670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao With two arguments, compute a**b. If a is negative then b 49690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao must be integral. The result will be inexact unless b is 49700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao integral and the result is finite and can be expressed exactly 49710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in 'precision' digits. 49720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao With three arguments, compute (a**b) % modulo. For the 49740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao three argument form, the following restrictions on the 49750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao arguments hold: 49760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - all three arguments must be integral 49780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - b must be nonnegative 49790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - at least one of a or b must be nonzero 49800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao - modulo must be nonzero and have at most 'precision' digits 49810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result of pow(a, b, modulo) is identical to the result 49830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao that would be obtained by computing (a**b) % modulo with 49840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unbounded precision, but is computed more efficiently. It is 49850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao always exact. 49860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 49870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c = ExtendedContext.copy() 49880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emin = -999 49890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.Emax = 999 49900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('2'), Decimal('3')) 49910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('8') 49920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-2'), Decimal('3')) 49930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-8') 49940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('2'), Decimal('-3')) 49950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.125') 49960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('1.7'), Decimal('8')) 49970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('69.7575744') 49980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('10'), Decimal('0.301029996')) 49990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.00000000') 50000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('Infinity'), Decimal('-1')) 50010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 50020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('Infinity'), Decimal('0')) 50030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('Infinity'), Decimal('1')) 50050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('Infinity') 50060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-Infinity'), Decimal('-1')) 50070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0') 50080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-Infinity'), Decimal('0')) 50090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-Infinity'), Decimal('1')) 50110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 50120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-Infinity'), Decimal('2')) 50130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('Infinity') 50140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('0'), Decimal('0')) 50150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('NaN') 50160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('3'), Decimal('7'), Decimal('16')) 50180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('11') 50190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16')) 50200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-11') 50210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16')) 50220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16')) 50240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('11') 50250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789')) 50260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('11729830') 50270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729')) 50280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0') 50290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537')) 50300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.power(7, 7) 50320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('823543') 50330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.power(Decimal(7), 7) 50340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('823543') 50350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.power(7, Decimal(7), 2) 50360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 50380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 50390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__pow__(b, modulo, context=self) 50400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 50410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 50420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 50430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 50440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def quantize(self, a, b): 50460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a value equal to 'a' (rounded), having the exponent of 'b'. 50470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The coefficient of the result is derived from that of the left-hand 50490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operand. It may be rounded using the current rounding setting (if the 50500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exponent is being increased), multiplied by a positive power of ten (if 50510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the exponent is being decreased), or is unchanged (if the exponent is 50520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao already equal to that of the right-hand operand). 50530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Unlike other operations, if the length of the coefficient after the 50550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao quantize operation would be greater than precision then an Invalid 50560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao operation condition is raised. This guarantees that, unless there is 50570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao an error condition, the exponent of the result of a quantize is always 50580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao equal to that of the right-hand operand. 50590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Also unlike other operations, quantize will never raise Underflow, even 50610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if the result is subnormal and inexact. 50620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 50630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001')) 50640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.170') 50650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01')) 50660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.17') 50670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1')) 50680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.2') 50690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0')) 50700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 50710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1')) 50720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0E+1') 50730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity')) 50740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 50750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity')) 50760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('NaN') 50770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1')) 50780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0') 50790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5')) 50800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0E+5') 50810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2')) 50820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('NaN') 50830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2')) 50840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('NaN') 50850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1')) 50860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('217.0') 50870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0')) 50880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('217') 50890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1')) 50900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.2E+2') 50910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2')) 50920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2E+2') 50930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(1, 2) 50940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(Decimal(1), 2) 50960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.quantize(1, Decimal(2)) 50980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 50990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 51000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 51010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.quantize(b, context=self) 51020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def radix(self): 51040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Just returns 10, as this is Decimal, :) 51050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.radix() 51070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('10') 51080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 51090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(10) 51100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def remainder(self, a, b): 51120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the remainder from integer division. 51130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result is the residue of the dividend after the operation of 51150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao calculating integer division as described for divide-integer, rounded 51160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao to precision digits if necessary. The sign of the result, if 51170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao non-zero, is the same as that of the original dividend. 51180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This operation will fail under the same conditions as integer division 51200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (that is, if integer division on the same two operands would fail, the 51210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao remainder cannot be calculated). 51220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3')) 51240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.1') 51250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(Decimal('10'), Decimal('3')) 51260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 51270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3')) 51280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 51290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1')) 51300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.2') 51310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3')) 51320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.1') 51330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3')) 51340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.0') 51350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(22, 6) 51360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('4') 51370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(Decimal(22), 6) 51380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('4') 51390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder(22, Decimal(6)) 51400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('4') 51410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 51420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 51430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__mod__(b, context=self) 51440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 51450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 51460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 51470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 51480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def remainder_near(self, a, b): 51500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns to be "a - b * n", where n is the integer nearest the exact 51510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value of "x / b" (if two integers are equally near then the even one 51520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is chosen). If the result is equal to 0 then its sign will be the 51530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign of a. 51540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This operation will fail under the same conditions as integer division 51560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (that is, if integer division on the same two operands would fail, the 51570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao remainder cannot be calculated). 51580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3')) 51600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0.9') 51610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6')) 51620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-2') 51630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3')) 51640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 51650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3')) 51660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-1') 51670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1')) 51680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.2') 51690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3')) 51700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.1') 51710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3')) 51720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0.3') 51730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(3, 11) 51740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 51750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(Decimal(3), 11) 51760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 51770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.remainder_near(3, Decimal(11)) 51780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 51790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 51800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 51810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.remainder_near(b, context=self) 51820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def rotate(self, a, b): 51840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a rotated copy of a, b times. 51850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The coefficient of the result is a rotated copy of the digits in 51870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the coefficient of the first operand. The number of places of 51880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rotation is taken from the absolute value of the second operand, 51890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with the rotation being to the left if the second operand is 51900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao positive or to the right otherwise. 51910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 51920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(Decimal('34'), Decimal('8')) 51930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('400000003') 51940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(Decimal('12'), Decimal('9')) 51950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('12') 51960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2')) 51970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('891234567') 51980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0')) 51990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('123456789') 52000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2')) 52010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('345678912') 52020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(1333333, 1) 52030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('13333330') 52040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(Decimal(1333333), 1) 52050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('13333330') 52060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.rotate(1333333, Decimal(1)) 52070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('13333330') 52080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 52090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 52100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.rotate(b, context=self) 52110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def same_quantum(self, a, b): 52130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns True if the two operands have the same exponent. 52140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The result is never affected by either the sign or the coefficient of 52160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao either operand. 52170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001')) 52190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 52200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01')) 52210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 52220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1')) 52230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao False 52240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf')) 52250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 52260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.same_quantum(10000, -1) 52270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 52280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.same_quantum(Decimal(10000), -1) 52290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 52300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.same_quantum(10000, Decimal(-1)) 52310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao True 52320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 52330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 52340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.same_quantum(b) 52350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def scaleb (self, a, b): 52370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns the first operand after adding the second value its exp. 52380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2')) 52400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.0750') 52410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0')) 52420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7.50') 52430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3')) 52440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7.50E+3') 52450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.scaleb(1, 4) 52460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1E+4') 52470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.scaleb(Decimal(1), 4) 52480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1E+4') 52490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.scaleb(1, Decimal(4)) 52500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1E+4') 52510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 52520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 52530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.scaleb(b, context=self) 52540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def shift(self, a, b): 52560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Returns a shifted copy of a, b times. 52570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The coefficient of the result is a shifted copy of the digits 52590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in the coefficient of the first operand. The number of places 52600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao to shift is taken from the absolute value of the second operand, 52610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with the shift being to the left if the second operand is 52620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao positive or to the right otherwise. Digits shifted into the 52630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coefficient are zeros. 52640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(Decimal('34'), Decimal('8')) 52660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('400000000') 52670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(Decimal('12'), Decimal('9')) 52680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 52690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2')) 52700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1234567') 52710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0')) 52720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('123456789') 52730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2')) 52740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('345678900') 52750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(88888888, 2) 52760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('888888800') 52770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(Decimal(88888888), 2) 52780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('888888800') 52790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.shift(88888888, Decimal(2)) 52800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('888888800') 52810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 52820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 52830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.shift(b, context=self) 52840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def sqrt(self, a): 52860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Square root of a non-negative number to context precision. 52870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If the result must be inexact, it is rounded using the round-half-even 52890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao algorithm. 52900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 52910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('0')) 52920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0') 52930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('-0')) 52940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0') 52950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('0.39')) 52960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.624499800') 52970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('100')) 52980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('10') 52990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('1')) 53000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1') 53010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('1.0')) 53020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.0') 53030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('1.00')) 53040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.0') 53050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('7')) 53060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2.64575131') 53070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(Decimal('10')) 53080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3.16227766') 53090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.sqrt(2) 53100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.41421356') 53110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.prec 53120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9 53130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 53140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 53150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.sqrt(context=self) 53160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def subtract(self, a, b): 53180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Return the difference between the two operands. 53190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07')) 53210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.23') 53220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30')) 53230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('0.00') 53240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07')) 53250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-0.77') 53260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.subtract(8, 5) 53270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 53280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.subtract(Decimal(8), 5) 53290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 53300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.subtract(8, Decimal(5)) 53310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('3') 53320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 53330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 53340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r = a.__sub__(b, context=self) 53350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if r is NotImplemented: 53360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % b) 53370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 53380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return r 53390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def to_eng_string(self, a): 53410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Converts a number to a string, using scientific notation. 53420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operation is not affected by the context. 53440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 53450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 53460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.to_eng_string(context=self) 53470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def to_sci_string(self, a): 53490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Converts a number to a string, using scientific notation. 53500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The operation is not affected by the context. 53520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 53530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 53540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.__str__(context=self) 53550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def to_integral_exact(self, a): 53570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds to an integer. 53580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao When the operand has a negative exponent, the result is the same 53600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao as using the quantize() operation using the given operand as the 53610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao left-hand-operand, 1E+0 as the right-hand-operand, and the precision 53620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of the operand as the precision setting; Inexact and Rounded flags 53630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao are allowed in this operation. The rounding mode is taken from the 53640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao context. 53650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('2.1')) 53670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 53680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('100')) 53690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 53700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('100.0')) 53710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 53720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('101.5')) 53730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('102') 53740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('-101.5')) 53750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-102') 53760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('10E+5')) 53770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.0E+6') 53780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77')) 53790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7.89E+77') 53800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_exact(Decimal('-Inf')) 53810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 53820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 53830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 53840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.to_integral_exact(context=self) 53850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def to_integral_value(self, a): 53870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Rounds to an integer. 53880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao When the operand has a negative exponent, the result is the same 53900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao as using the quantize() operation using the given operand as the 53910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao left-hand-operand, 1E+0 as the right-hand-operand, and the precision 53920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of the operand as the precision setting, except that no flags will 53930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be set. The rounding mode is taken from the context. 53940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 53950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('2.1')) 53960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('2') 53970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('100')) 53980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 53990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('100.0')) 54000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('100') 54010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('101.5')) 54020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('102') 54030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('-101.5')) 54040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-102') 54050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('10E+5')) 54060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('1.0E+6') 54070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('7.89E+77')) 54080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('7.89E+77') 54090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> ExtendedContext.to_integral_value(Decimal('-Inf')) 54100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal('-Infinity') 54110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 54120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = _convert_other(a, raiseit=True) 54130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a.to_integral_value(context=self) 54140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the method name changed, but we provide also the old one, for compatibility 54160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao to_integral = to_integral_value 54170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _WorkRep(object): 54190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __slots__ = ('sign','int','exp') 54200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # sign: 0 or 1 54210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # int: int or long 54220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # exp: None, int, or string 54230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, value=None): 54250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if value is None: 54260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.sign = None 54270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.int = 0 54280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.exp = None 54290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif isinstance(value, Decimal): 54300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.sign = value._sign 54310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.int = int(value._int) 54320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.exp = value._exp 54330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 54340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # assert isinstance(value, tuple) 54350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.sign = value[0] 54360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.int = value[1] 54370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.exp = value[2] 54380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 54400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "(%r, %r, %r)" % (self.sign, self.int, self.exp) 54410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __str__ = __repr__ 54430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _normalize(op1, op2, prec = 0): 54470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Normalizes op1, op2 to have the same exp and length of coefficient. 54480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Done during addition. 54500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 54510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if op1.exp < op2.exp: 54520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tmp = op2 54530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = op1 54540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 54550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tmp = op1 54560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other = op2 54570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Let exp = min(tmp.exp - 1, tmp.adjusted() - precision - 1). 54590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Then adding 10**exp to tmp has the same effect (after rounding) 54600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # as adding any positive quantity smaller than 10**exp; similarly 54610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for subtraction. So if other is smaller than 10**exp we replace 54620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # it with 10**exp. This avoids tmp.exp - other.exp getting too large. 54630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tmp_len = len(str(tmp.int)) 54640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other_len = len(str(other.int)) 54650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp = tmp.exp + min(-1, tmp_len - prec - 2) 54660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if other_len + other.exp - 1 < exp: 54670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other.int = 1 54680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao other.exp = exp 54690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tmp.int *= 10 ** (tmp.exp - other.exp) 54710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tmp.exp = other.exp 54720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return op1, op2 54730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### Integer arithmetic functions used by ln, log10, exp and __pow__ ##### 54750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This function from Tim Peters was taken from here: 54770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# http://mail.python.org/pipermail/python-list/1999-July/007758.html 54780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The correction being in the function definition is for speed, and 54790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# the whole function is not resolved with math.log because of avoiding 54800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# the use of floats. 54810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _nbits(n, correction = { 54820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '0': 4, '1': 3, '2': 2, '3': 2, 54830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '4': 1, '5': 1, '6': 1, '7': 1, 54840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '8': 0, '9': 0, 'a': 0, 'b': 0, 54850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'c': 0, 'd': 0, 'e': 0, 'f': 0}): 54860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Number of bits in binary representation of the positive integer n, 54870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao or 0 if n == 0. 54880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 54890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if n < 0: 54900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("The argument to _nbits should be nonnegative.") 54910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hex_n = "%x" % n 54920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 4*len(hex_n) - correction[hex_n[0]] 54930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54940a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _decimal_lshift_exact(n, e): 54950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ Given integers n and e, return n * 10**e if it's an integer, else None. 54960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 54970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The computation is designed to avoid computing large powers of 10 54980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unnecessarily. 54990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> _decimal_lshift_exact(3, 4) 55010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30000 55020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao >>> _decimal_lshift_exact(300, -999999999) # returns None 55030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 55050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if n == 0: 55060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 55070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif e >= 0: 55080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return n * 10**e 55090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 55100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # val_n = largest power of 10 dividing n. 55110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao str_n = str(abs(n)) 55120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao val_n = len(str_n) - len(str_n.rstrip('0')) 55130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return None if val_n < -e else n // 10**-e 55140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _sqrt_nearest(n, a): 55160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Closest integer to the square root of the positive integer n. a is 55170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao an initial approximation to the square root. Any positive integer 55180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao will do for a, but the closer a is to the square root of n the 55190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao faster convergence will be. 55200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 55220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if n <= 0 or a <= 0: 55230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Both arguments to _sqrt_nearest should be positive.") 55240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao b=0 55260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while a != b: 55270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao b, a = a, a--n//a>>1 55280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return a 55290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _rshift_nearest(x, shift): 55310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Given an integer x and a nonnegative integer shift, return closest 55320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao integer to x / 2**shift; use round-to-even in case of a tie. 55330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 55350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao b, q = 1L << shift, x >> shift 55360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return q + (2*(x & (b-1)) + (q&1) > b) 55370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55380a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _div_nearest(a, b): 55390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Closest integer to a/b, a and b positive integers; rounds to even 55400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in the case of a tie. 55410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 55430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q, r = divmod(a, b) 55440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return q + (2*r + (q&1) > b) 55450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _ilog(x, M, L = 8): 55470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Integer approximation to M*log(x/M), with absolute error boundable 55480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in terms only of x/M. 55490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Given positive integers x and M, return an integer approximation to 55510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao M * log(x/M). For L = 8 and 0.1 <= x/M <= 10 the difference 55520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao between the approximation and the exact result is at most 22. For 55530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15. In 55540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao both cases these are upper bounds on the error; it will usually be 55550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao much smaller.""" 55560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The basic algorithm is the following: let log1p be the function 55580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log1p(x) = log(1+x). Then log(x/M) = log1p((x-M)/M). We use 55590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the reduction 55600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 55610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log1p(y) = 2*log1p(y/(1+sqrt(1+y))) 55620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 55630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # repeatedly until the argument to log1p is small (< 2**-L in 55640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # absolute value). For small y we can use the Taylor series 55650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # expansion 55660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 55670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log1p(y) ~ y - y**2/2 + y**3/3 - ... - (-y)**T/T 55680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 55690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # truncating at T such that y**T is small enough. The whole 55700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # computation is carried out in a form of fixed-point arithmetic, 55710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # with a real number z being represented by an integer 55720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # approximation to z*M. To avoid loss of precision, the y below 55730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # is actually an integer approximation to 2**R*y*M, where R is the 55740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # number of reductions performed so far. 55750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = x-M 55770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # argument reduction; R = number of reductions performed 55780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao R = 0 55790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while (R <= L and long(abs(y)) << L-R >= M or 55800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao R > L and abs(y) >> R-L >= M): 55810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _div_nearest(long(M*y) << 1, 55820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao M + _sqrt_nearest(M*(M+_rshift_nearest(y, R)), M)) 55830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao R += 1 55840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Taylor series with T terms 55860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao T = -int(-10*len(str(M))//(3*L)) 55870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yshift = _rshift_nearest(y, R) 55880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao w = _div_nearest(M, T) 55890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for k in xrange(T-1, 0, -1): 55900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao w = _div_nearest(M, k) - _div_nearest(yshift*w, M) 55910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _div_nearest(w*y, M) 55930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55940a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _dlog10(c, e, p): 55950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Given integers c, e and p with c > 0, p >= 0, compute an integer 55960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao approximation to 10**p * log10(c*10**e), with an absolute error of 55970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao at most 1. Assumes that c*10**e is not exactly 1.""" 55980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 55990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # increase precision by 2; compensate for this by dividing 56000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # final result by 100 56010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p += 2 56020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # write c*10**e as d*10**f with either: 56040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # f >= 0 and 1 <= d <= 10, or 56050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # f <= 0 and 0.1 <= d <= 1. 56060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Thus for c*10**e close to 1, f = 0 56070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao l = len(str(c)) 56080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = e+l - (e+l >= 1) 56090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if p > 0: 56110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao M = 10**p 56120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao k = e+p-f 56130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if k >= 0: 56140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c *= 10**k 56150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 56160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = _div_nearest(c, 10**-k) 56170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_d = _ilog(c, M) # error < 5 + 22 = 27 56190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_10 = _log10_digits(p) # error < 1 56200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_d = _div_nearest(log_d*M, log_10) 56210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_tenpower = f*M # exact 56220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 56230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_d = 0 # error < 2.31 56240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_tenpower = _div_nearest(f, 10**-p) # error < 0.5 56250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _div_nearest(log_tenpower+log_d, 100) 56270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _dlog(c, e, p): 56290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Given integers c, e and p with c > 0, compute an integer 56300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao approximation to 10**p * log(c*10**e), with an absolute error of 56310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao at most 1. Assumes that c*10**e is not exactly 1.""" 56320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Increase precision by 2. The precision increase is compensated 56340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for at the end with a division by 100. 56350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p += 2 56360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # rewrite c*10**e as d*10**f with either f >= 0 and 1 <= d <= 10, 56380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # or f <= 0 and 0.1 <= d <= 1. Then we can compute 10**p * log(c*10**e) 56390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # as 10**p * log(d) + 10**p*f * log(10). 56400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao l = len(str(c)) 56410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = e+l - (e+l >= 1) 56420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute approximation to 10**p*log(d), with error < 27 56440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if p > 0: 56450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao k = e+p-f 56460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if k >= 0: 56470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c *= 10**k 56480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 56490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = _div_nearest(c, 10**-k) # error of <= 0.5 in c 56500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # _ilog magnifies existing error in c by a factor of at most 10 56520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_d = _ilog(c, 10**p) # error < 5 + 22 = 27 56530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 56540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # p <= 0: just approximate the whole thing by 0; error < 2.31 56550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao log_d = 0 56560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute approximation to f*10**p*log(10), with error < 11. 56580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if f: 56590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra = len(str(abs(f)))-1 56600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if p + extra >= 0: 56610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # error in f * _log10_digits(p+extra) < |f| * 1 = |f| 56620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # after division, error < |f|/10**extra + 0.5 < 10 + 0.5 < 11 56630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f_log_ten = _div_nearest(f*_log10_digits(p+extra), 10**extra) 56640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 56650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f_log_ten = 0 56660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 56670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f_log_ten = 0 56680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # error in sum < 11+27 = 38; error after division < 0.38 + 0.5 < 1 56700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _div_nearest(f_log_ten + log_d, 100) 56710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56720a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _Log10Memoize(object): 56730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Class to compute, store, and allow retrieval of, digits of the 56740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao constant log(10) = 2.302585.... This constant is needed by 56750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__.""" 56760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self): 56770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.digits = "23025850929940456840179914546843642076011014886" 56780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getdigits(self, p): 56800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Given an integer p >= 0, return floor(10**p)*log(10). 56810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao For example, self.getdigits(3) returns 2302. 56830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 56840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # digits are stored as a string, for quick conversion to 56850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # integer in the case that we've already computed enough 56860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # digits; the stored digits should always be correct 56870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (truncated, not rounded to nearest). 56880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if p < 0: 56890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("p should be nonnegative") 56900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 56910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if p >= len(self.digits): 56920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute p+3, p+6, p+9, ... digits; continue until at 56930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # least one of the extra digits is nonzero 56940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra = 3 56950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while True: 56960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute p+extra digits, correct to within 1ulp 56970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao M = 10**(p+extra+2) 56980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits = str(_div_nearest(_ilog(10*M, M), 100)) 56990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if digits[-extra:] != '0'*extra: 57000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 57010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra += 3 57020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # keep all reliable digits so far; remove trailing zeros 57030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # and next nonzero digit 57040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.digits = digits.rstrip('0')[:-1] 57050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int(self.digits[:p+1]) 57060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_log10_digits = _Log10Memoize().getdigits 57080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57090a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _iexp(x, M, L=8): 57100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Given integers x and M, M > 0, such that x/M is small in absolute 57110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao value, compute an integer approximation to M*exp(x/M). For 0 <= 57120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x/M <= 2.4, the absolute error in the result is bounded by 60 (and 57130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is usually much smaller).""" 57140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Algorithm: to compute exp(z) for a real number z, first divide z 57160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # by a suitable power R of 2 so that |z/2**R| < 2**-L. Then 57170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor 57180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # series 57190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 57200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # expm1(x) = x + x**2/2! + x**3/3! + ... 57210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 57220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Now use the identity 57230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 57240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # expm1(2x) = expm1(x)*(expm1(x)+2) 57250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 57260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # R times to compute the sequence expm1(z/2**R), 57270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # expm1(z/2**(R-1)), ... , exp(z/2), exp(z). 57280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Find R such that x/2**R/M <= 2**-L 57300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao R = _nbits((long(x)<<L)//M) 57310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Taylor series. (2**L)**T > M 57330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao T = -int(-10*len(str(M))//(3*L)) 57340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _div_nearest(x, T) 57350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Mshift = long(M)<<R 57360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in xrange(T-1, 0, -1): 57370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _div_nearest(x*(Mshift + y), Mshift * i) 57380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Expansion 57400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for k in xrange(R-1, -1, -1): 57410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Mshift = long(M)<<(k+2) 57420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _div_nearest(y*(y+Mshift), Mshift) 57430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return M+y 57450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _dexp(c, e, p): 57470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compute an approximation to exp(c*10**e), with p decimal places of 57480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao precision. 57490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Returns integers d, f such that: 57510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10**(p-1) <= d <= 10**p, and 57530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (d-1)*10**f < exp(c*10**e) < (d+1)*10**f 57540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao In other words, d*10**f is an approximation to exp(c*10**e) with p 57560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits of precision, and with an error in d of at most 1. This is 57570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao almost, but not quite, the same as the error being < 1ulp: when d 57580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao = 10**(p-1) the error could be up to 10 ulp.""" 57590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # we'll call iexp with M = 10**(p+2), giving p+3 digits of precision 57610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p += 2 57620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute log(10) with extra precision = adjusted exponent of c*10**e 57640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao extra = max(0, e + len(str(c)) - 1) 57650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q = p + extra 57660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute quotient c*10**e/(log(10)) = c*10**(e+q)/(log(10)*10**q), 57680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # rounding down 57690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shift = e+q 57700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if shift >= 0: 57710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cshift = c*10**shift 57720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 57730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cshift = c//10**-shift 57740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao quot, rem = divmod(cshift, _log10_digits(q)) 57750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # reduce remainder back to original precision 57770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rem = _div_nearest(rem, 10**extra) 57780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # error in result of _iexp < 120; error after division < 0.62 57800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _div_nearest(_iexp(rem, 10**p), 1000), quot - p + 3 57810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _dpower(xc, xe, yc, ye, p): 57830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and 57840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that: 57850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10**(p-1) <= c <= 10**p, and 57870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (c-1)*10**e < x**y < (c+1)*10**e 57880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in other words, c*10**e is an approximation to x**y with p digits 57900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao of precision, and with an error in c of at most 1. (This is 57910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao almost, but not quite, the same as the error being < 1ulp: when c 57920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao == 10**(p-1) we can only guarantee error < 10ulp.) 57930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao We assume that: x is positive and not equal to 1, and y is nonzero. 57950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 57960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 57970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Find b such that 10**(b-1) <= |y| <= 10**b 57980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao b = len(str(abs(yc))) + ye 57990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # log(x) = lxc*10**(-p-b-1), to p+b+1 places after the decimal point 58010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lxc = _dlog(xc, xe, p+b+1) 58020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # compute product y*log(x) = yc*lxc*10**(-p-b-1+ye) = pc*10**(-p-1) 58040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shift = ye-b 58050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if shift >= 0: 58060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pc = lxc*yc*10**shift 58070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 58080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pc = _div_nearest(lxc*yc, 10**-shift) 58090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if pc == 0: 58110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # we prefer a result that isn't exactly 1; this makes it 58120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # easier to compute a correctly rounded result in __pow__ 58130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if ((len(str(xc)) + xe >= 1) == (yc > 0)): # if x**y > 1: 58140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff, exp = 10**(p-1)+1, 1-p 58150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 58160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff, exp = 10**p-1, -p 58170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 58180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff, exp = _dexp(pc, -(p+1), p+1) 58190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao coeff = _div_nearest(coeff, 10) 58200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp += 1 58210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return coeff, exp 58230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _log10_lb(c, correction = { 58250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '1': 100, '2': 70, '3': 53, '4': 40, '5': 31, 58260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '6': 23, '7': 16, '8': 10, '9': 5}): 58270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Compute a lower bound for 100*log10(c) for a positive integer c.""" 58280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c <= 0: 58290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("The argument to _log10_lb should be nonnegative.") 58300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao str_c = str(c) 58310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 100*len(str_c) - correction[str_c[0]] 58320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### Helper Functions #################################################### 58340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58350a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _convert_other(other, raiseit=False, allow_float=False): 58360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Convert other to Decimal. 58370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Verifies that it's ok to use in an implicit construction. 58390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If allow_float is true, allow conversion from float; this 58400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is used in the comparison methods (__eq__ and friends). 58410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 58430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(other, Decimal): 58440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return other 58450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(other, (int, long)): 58460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal(other) 58470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if allow_float and isinstance(other, float): 58480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Decimal.from_float(other) 58490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if raiseit: 58510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError("Unable to convert %s to Decimal" % other) 58520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return NotImplemented 58530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### Setup Specific Contexts ############################################ 58550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The default context prototype used by Context() 58570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Is mutable, so that new contexts can have different default values 58580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58590a8c90248264a8b26970b4473770bcc3df8515fJosh GaoDefaultContext = Context( 58600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao prec=28, rounding=ROUND_HALF_EVEN, 58610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao traps=[DivisionByZero, Overflow, InvalidOperation], 58620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao flags=[], 58630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Emax=999999999, 58640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Emin=-999999999, 58650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao capitals=1 58660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao) 58670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Pre-made alternate contexts offered by the specification 58690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Don't change these; the user should be able to select these 58700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# contexts and be able to reproduce results from other implementations 58710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# of the spec. 58720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58730a8c90248264a8b26970b4473770bcc3df8515fJosh GaoBasicContext = Context( 58740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao prec=9, rounding=ROUND_HALF_UP, 58750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow], 58760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao flags=[], 58770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao) 58780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58790a8c90248264a8b26970b4473770bcc3df8515fJosh GaoExtendedContext = Context( 58800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao prec=9, rounding=ROUND_HALF_EVEN, 58810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao traps=[], 58820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao flags=[], 58830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao) 58840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 58860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### crud for parsing strings ############################################# 58870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 58880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Regular expression used for parsing numeric strings. Additional 58890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# comments: 58900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 58910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1. Uncomment the two '\s*' lines to allow leading and/or trailing 58920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# whitespace. But note that the specification disallows whitespace in 58930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# a numeric string. 58940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 58950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 2. For finite numbers (not infinities and NaNs) the body of the 58960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# number between the optional sign and the optional exponent must have 58970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# at least one decimal digit, possibly after the decimal point. The 58980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# lookahead expression '(?=\d|\.\d)' checks this. 58990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59000a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport re 59010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_parser = re.compile(r""" # A numeric string consists of: 59020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# \s* 59030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<sign>[-+])? # an optional sign, followed by either... 59040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ( 59050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?=\d|\.\d) # ...a number (with at least one digit) 59060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<int>\d*) # having a (possibly empty) integer part 59070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (\.(?P<frac>\d*))? # followed by an optional fractional part 59080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (E(?P<exp>[-+]?\d+))? # followed by an optional exponent, or... 59090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao | 59100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Inf(inity)? # ...an infinity, or... 59110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao | 59120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<signal>s)? # ...an (optionally signaling) 59130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NaN # NaN 59140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<diag>\d*) # with (possibly empty) diagnostic info. 59150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ) 59160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# \s* 59170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao \Z 59180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""", re.VERBOSE | re.IGNORECASE | re.UNICODE).match 59190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_all_zeros = re.compile('0*$').match 59210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_exact_half = re.compile('50*$').match 59220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### PEP3101 support functions ############################################## 59240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The functions in this section have little to do with the Decimal 59250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# class, and could potentially be reused or adapted for other pure 59260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Python numeric classes that want to implement __format__ 59270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 59280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# A format specifier for Decimal looks like: 59290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 59300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# [[fill]align][sign][0][minimumwidth][,][.precision][type] 59310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_parse_format_specifier_regex = re.compile(r"""\A 59330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao(?: 59340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<fill>.)? 59350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (?P<align>[<>=^]) 59360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao)? 59370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao(?P<sign>[-+ ])? 59380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao(?P<zeropad>0)? 59390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao(?P<minimumwidth>(?!0)\d+)? 59400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao(?P<thousands_sep>,)? 59410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao(?:\.(?P<precision>0|(?!0)\d+))? 59420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao(?P<type>[eEfFgGn%])? 59430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao\Z 59440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""", re.VERBOSE) 59450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel re 59470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# The locale module is only needed for the 'n' format specifier. The 59490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# rest of the PEP 3101 code functions quite happily without it, so we 59500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# don't care too much if locale isn't present. 59510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry: 59520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import locale as _locale 59530a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ImportError: 59540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 59550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59560a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _parse_format_specifier(format_spec, _localeconv=None): 59570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Parse and validate a format specifier. 59580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Turns a standard numeric format specifier into a dict, with the 59600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao following entries: 59610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fill: fill character to pad field to minimum width 59630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao align: alignment type, either '<', '>', '=' or '^' 59640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign: either '+', '-' or ' ' 59650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao minimumwidth: nonnegative integer giving minimum width 59660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao zeropad: boolean, indicating whether to pad with zeros 59670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thousands_sep: string to use as thousands separator, or '' 59680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao grouping: grouping for thousands separators, in format 59690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao used by localeconv 59700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao decimal_point: string to use for decimal point 59710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao precision: nonnegative integer giving precision, or None 59720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type: one of the characters 'eEfFgG%', or None 59730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unicode: boolean (always True for Python 3.x) 59740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 59760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao m = _parse_format_specifier_regex.match(format_spec) 59770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if m is None: 59780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Invalid format specifier: " + format_spec) 59790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # get the dictionary 59810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict = m.groupdict() 59820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 59830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # zeropad; defaults for fill and alignment. If zero padding 59840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # is requested, the fill and align fields should be absent. 59850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fill = format_dict['fill'] 59860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao align = format_dict['align'] 59870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['zeropad'] = (format_dict['zeropad'] is not None) 59880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['zeropad']: 59890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if fill is not None: 59900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Fill character conflicts with '0'" 59910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao " in format specifier: " + format_spec) 59920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if align is not None: 59930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Alignment conflicts with '0' in " 59940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "format specifier: " + format_spec) 59950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['fill'] = fill or ' ' 59960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # PEP 3101 originally specified that the default alignment should 59970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # be left; it was later agreed that right-aligned makes more sense 59980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # for numeric types. See http://bugs.python.org/issue6857. 59990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['align'] = align or '>' 60000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # default sign handling: '-' for negative, '' for positive 60020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['sign'] is None: 60030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['sign'] = '-' 60040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # minimumwidth defaults to 0; precision remains None if not given 60060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['minimumwidth'] = int(format_dict['minimumwidth'] or '0') 60070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['precision'] is not None: 60080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['precision'] = int(format_dict['precision']) 60090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if format type is 'g' or 'G' then a precision of 0 makes little 60110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # sense; convert it to 1. Same if format type is unspecified. 60120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['precision'] == 0: 60130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['type'] is None or format_dict['type'] in 'gG': 60140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['precision'] = 1 60150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # determine thousands separator, grouping, and decimal separator, and 60170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # add appropriate entries to format_dict 60180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['type'] == 'n': 60190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # apart from separators, 'n' behaves just like 'g' 60200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['type'] = 'g' 60210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if _localeconv is None: 60220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao _localeconv = _locale.localeconv() 60230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['thousands_sep'] is not None: 60240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("Explicit thousands separator conflicts with " 60250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "'n' type in format specifier: " + format_spec) 60260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['thousands_sep'] = _localeconv['thousands_sep'] 60270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['grouping'] = _localeconv['grouping'] 60280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['decimal_point'] = _localeconv['decimal_point'] 60290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 60300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if format_dict['thousands_sep'] is None: 60310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['thousands_sep'] = '' 60320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['grouping'] = [3, 0] 60330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['decimal_point'] = '.' 60340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # record whether return type should be str or unicode 60360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format_dict['unicode'] = isinstance(format_spec, unicode) 60370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return format_dict 60390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _format_align(sign, body, spec): 60410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Given an unpadded, non-aligned numeric string 'body' and sign 60420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao string 'sign', add padding and alignment conforming to the given 60430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format specifier dictionary 'spec' (as produced by 60440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao parse_format_specifier). 60450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Also converts result to unicode if necessary. 60470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 60490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # how much extra space do we have to play with? 60500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao minimumwidth = spec['minimumwidth'] 60510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fill = spec['fill'] 60520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao padding = fill*(minimumwidth - len(sign) - len(body)) 60530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao align = spec['align'] 60550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if align == '<': 60560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = sign + body + padding 60570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif align == '>': 60580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = padding + sign + body 60590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif align == '=': 60600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = sign + padding + body 60610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif align == '^': 60620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao half = len(padding)//2 60630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = padding[:half] + sign + body + padding[half:] 60640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 60650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('Unrecognised alignment field') 60660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make sure that result is unicode if necessary 60680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if spec['unicode']: 60690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = unicode(result) 60700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return result 60720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60730a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _group_lengths(grouping): 60740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Convert a localeconv-style grouping into a (possibly infinite) 60750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao iterable of integers representing group lengths. 60760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 60780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The result from localeconv()['grouping'], and the input to this 60790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # function, should be a list of integers in one of the 60800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # following three forms: 60810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 60820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (1) an empty list, or 60830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (2) nonempty list of positive integers + [0] 60840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # (3) list of positive integers + [locale.CHAR_MAX], or 60850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from itertools import chain, repeat 60870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not grouping: 60880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return [] 60890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif grouping[-1] == 0 and len(grouping) >= 2: 60900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return chain(grouping[:-1], repeat(grouping[-2])) 60910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif grouping[-1] == _locale.CHAR_MAX: 60920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return grouping[:-1] 60930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 60940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError('unrecognised format for grouping') 60950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _insert_thousands_sep(digits, spec, min_width=1): 60970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Insert thousands separators into a digit string. 60980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 60990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao spec is a dictionary whose keys should include 'thousands_sep' and 61000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'grouping'; typically it's the result of parsing the format 61010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao specifier using _parse_format_specifier. 61020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao The min_width keyword argument gives the minimum length of the 61040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result, which will be padded on the left with zeros if necessary. 61050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao If necessary, the zero padding adds an extra '0' on the left to 61070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao avoid a leading thousands separator. For example, inserting 61080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao commas every three digits in '123456', with min_width=8, gives 61090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao '0,123,456', even though that has length 9. 61100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 61120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sep = spec['thousands_sep'] 61140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao grouping = spec['grouping'] 61150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao groups = [] 61170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for l in _group_lengths(grouping): 61180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if l <= 0: 61190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError("group length should be positive") 61200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # max(..., 1) forces at least 1 digit to the left of a separator 61210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao l = min(max(len(digits), min_width, 1), l) 61220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao groups.append('0'*(l - len(digits)) + digits[-l:]) 61230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao digits = digits[:-l] 61240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao min_width -= l 61250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not digits and min_width <= 0: 61260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao break 61270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao min_width -= len(sep) 61280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 61290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao l = max(len(digits), min_width, 1) 61300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao groups.append('0'*(l - len(digits)) + digits[-l:]) 61310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return sep.join(reversed(groups)) 61320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _format_sign(is_negative, spec): 61340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Determine sign character.""" 61350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if is_negative: 61370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '-' 61380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elif spec['sign'] in ' +': 61390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return spec['sign'] 61400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 61410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return '' 61420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _format_number(is_negative, intpart, fracpart, exp, spec): 61440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Format a number, given the following data: 61450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is_negative: true if the number is negative, else false 61470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart: string of digits that must appear before the decimal point 61480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart: string of digits that must come after the point 61490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao exp: exponent, as an integer 61500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao spec: dictionary resulting from parsing the format specifier 61510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This function uses the information in spec to: 61530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao insert separators (decimal separator and thousands separators) 61540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format the sign 61550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao format the exponent 61560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao add trailing '%' for the '%' type 61570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao zero-pad if necessary 61580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fill and align if necessary 61590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 61600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sign = _format_sign(is_negative, spec) 61620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if fracpart: 61640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart = spec['decimal_point'] + fracpart 61650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if exp != 0 or spec['type'] in 'eE': 61670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao echar = {'E': 'E', 'e': 'e', 'G': 'E', 'g': 'e'}[spec['type']] 61680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart += "{0}{1:+}".format(echar, exp) 61690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if spec['type'] == '%': 61700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fracpart += '%' 61710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if spec['zeropad']: 61730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao min_width = spec['minimumwidth'] - len(fracpart) - len(sign) 61740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 61750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao min_width = 0 61760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao intpart = _insert_thousands_sep(intpart, spec, min_width) 61770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return _format_align(sign, intpart+fracpart, spec) 61790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##### Useful Constants (internal use only) ################################ 61820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Reusable defaults 61840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_Infinity = Decimal('Inf') 61850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_NegativeInfinity = Decimal('-Inf') 61860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_NaN = Decimal('NaN') 61870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_Zero = Decimal(0) 61880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_One = Decimal(1) 61890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_NegativeOne = Decimal(-1) 61900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# _SignedInfinity[sign] is infinity w/ that sign 61920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_SignedInfinity = (_Infinity, _NegativeInfinity) 61930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 61960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__': 61970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import doctest, sys 61980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao doctest.testmod(sys.modules[__name__]) 6199