Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: decimal.py
# Copyright (c) 2004 Python Software Foundation.
[0] Fix | Delete
# All rights reserved.
[1] Fix | Delete
[2] Fix | Delete
# Written by Eric Price <eprice at tjhsst.edu>
[3] Fix | Delete
# and Facundo Batista <facundo at taniquetil.com.ar>
[4] Fix | Delete
# and Raymond Hettinger <python at rcn.com>
[5] Fix | Delete
# and Aahz <aahz at pobox.com>
[6] Fix | Delete
# and Tim Peters
[7] Fix | Delete
[8] Fix | Delete
# This module is currently Py2.3 compatible and should be kept that way
[9] Fix | Delete
# unless a major compelling advantage arises. IOW, 2.3 compatibility is
[10] Fix | Delete
# strongly preferred, but not guaranteed.
[11] Fix | Delete
[12] Fix | Delete
# Also, this module should be kept in sync with the latest updates of
[13] Fix | Delete
# the IBM specification as it evolves. Those updates will be treated
[14] Fix | Delete
# as bug fixes (deviation from the spec is a compatibility, usability
[15] Fix | Delete
# bug) and will be backported. At this point the spec is stabilizing
[16] Fix | Delete
# and the updates are becoming fewer, smaller, and less significant.
[17] Fix | Delete
[18] Fix | Delete
"""
[19] Fix | Delete
This is a Py2.3 implementation of decimal floating point arithmetic based on
[20] Fix | Delete
the General Decimal Arithmetic Specification:
[21] Fix | Delete
[22] Fix | Delete
http://speleotrove.com/decimal/decarith.html
[23] Fix | Delete
[24] Fix | Delete
and IEEE standard 854-1987:
[25] Fix | Delete
[26] Fix | Delete
http://en.wikipedia.org/wiki/IEEE_854-1987
[27] Fix | Delete
[28] Fix | Delete
Decimal floating point has finite precision with arbitrarily large bounds.
[29] Fix | Delete
[30] Fix | Delete
The purpose of this module is to support arithmetic using familiar
[31] Fix | Delete
"schoolhouse" rules and to avoid some of the tricky representation
[32] Fix | Delete
issues associated with binary floating point. The package is especially
[33] Fix | Delete
useful for financial applications or for contexts where users have
[34] Fix | Delete
expectations that are at odds with binary floating point (for instance,
[35] Fix | Delete
in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
[36] Fix | Delete
of the expected Decimal('0.00') returned by decimal floating point).
[37] Fix | Delete
[38] Fix | Delete
Here are some examples of using the decimal module:
[39] Fix | Delete
[40] Fix | Delete
>>> from decimal import *
[41] Fix | Delete
>>> setcontext(ExtendedContext)
[42] Fix | Delete
>>> Decimal(0)
[43] Fix | Delete
Decimal('0')
[44] Fix | Delete
>>> Decimal('1')
[45] Fix | Delete
Decimal('1')
[46] Fix | Delete
>>> Decimal('-.0123')
[47] Fix | Delete
Decimal('-0.0123')
[48] Fix | Delete
>>> Decimal(123456)
[49] Fix | Delete
Decimal('123456')
[50] Fix | Delete
>>> Decimal('123.45e12345678901234567890')
[51] Fix | Delete
Decimal('1.2345E+12345678901234567892')
[52] Fix | Delete
>>> Decimal('1.33') + Decimal('1.27')
[53] Fix | Delete
Decimal('2.60')
[54] Fix | Delete
>>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
[55] Fix | Delete
Decimal('-2.20')
[56] Fix | Delete
>>> dig = Decimal(1)
[57] Fix | Delete
>>> print dig / Decimal(3)
[58] Fix | Delete
0.333333333
[59] Fix | Delete
>>> getcontext().prec = 18
[60] Fix | Delete
>>> print dig / Decimal(3)
[61] Fix | Delete
0.333333333333333333
[62] Fix | Delete
>>> print dig.sqrt()
[63] Fix | Delete
1
[64] Fix | Delete
>>> print Decimal(3).sqrt()
[65] Fix | Delete
1.73205080756887729
[66] Fix | Delete
>>> print Decimal(3) ** 123
[67] Fix | Delete
4.85192780976896427E+58
[68] Fix | Delete
>>> inf = Decimal(1) / Decimal(0)
[69] Fix | Delete
>>> print inf
[70] Fix | Delete
Infinity
[71] Fix | Delete
>>> neginf = Decimal(-1) / Decimal(0)
[72] Fix | Delete
>>> print neginf
[73] Fix | Delete
-Infinity
[74] Fix | Delete
>>> print neginf + inf
[75] Fix | Delete
NaN
[76] Fix | Delete
>>> print neginf * inf
[77] Fix | Delete
-Infinity
[78] Fix | Delete
>>> print dig / 0
[79] Fix | Delete
Infinity
[80] Fix | Delete
>>> getcontext().traps[DivisionByZero] = 1
[81] Fix | Delete
>>> print dig / 0
[82] Fix | Delete
Traceback (most recent call last):
[83] Fix | Delete
...
[84] Fix | Delete
...
[85] Fix | Delete
...
[86] Fix | Delete
DivisionByZero: x / 0
[87] Fix | Delete
>>> c = Context()
[88] Fix | Delete
>>> c.traps[InvalidOperation] = 0
[89] Fix | Delete
>>> print c.flags[InvalidOperation]
[90] Fix | Delete
0
[91] Fix | Delete
>>> c.divide(Decimal(0), Decimal(0))
[92] Fix | Delete
Decimal('NaN')
[93] Fix | Delete
>>> c.traps[InvalidOperation] = 1
[94] Fix | Delete
>>> print c.flags[InvalidOperation]
[95] Fix | Delete
1
[96] Fix | Delete
>>> c.flags[InvalidOperation] = 0
[97] Fix | Delete
>>> print c.flags[InvalidOperation]
[98] Fix | Delete
0
[99] Fix | Delete
>>> print c.divide(Decimal(0), Decimal(0))
[100] Fix | Delete
Traceback (most recent call last):
[101] Fix | Delete
...
[102] Fix | Delete
...
[103] Fix | Delete
...
[104] Fix | Delete
InvalidOperation: 0 / 0
[105] Fix | Delete
>>> print c.flags[InvalidOperation]
[106] Fix | Delete
1
[107] Fix | Delete
>>> c.flags[InvalidOperation] = 0
[108] Fix | Delete
>>> c.traps[InvalidOperation] = 0
[109] Fix | Delete
>>> print c.divide(Decimal(0), Decimal(0))
[110] Fix | Delete
NaN
[111] Fix | Delete
>>> print c.flags[InvalidOperation]
[112] Fix | Delete
1
[113] Fix | Delete
>>>
[114] Fix | Delete
"""
[115] Fix | Delete
[116] Fix | Delete
__all__ = [
[117] Fix | Delete
# Two major classes
[118] Fix | Delete
'Decimal', 'Context',
[119] Fix | Delete
[120] Fix | Delete
# Contexts
[121] Fix | Delete
'DefaultContext', 'BasicContext', 'ExtendedContext',
[122] Fix | Delete
[123] Fix | Delete
# Exceptions
[124] Fix | Delete
'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero',
[125] Fix | Delete
'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow',
[126] Fix | Delete
[127] Fix | Delete
# Constants for use in setting up contexts
[128] Fix | Delete
'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING',
[129] Fix | Delete
'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN', 'ROUND_05UP',
[130] Fix | Delete
[131] Fix | Delete
# Functions for manipulating contexts
[132] Fix | Delete
'setcontext', 'getcontext', 'localcontext'
[133] Fix | Delete
]
[134] Fix | Delete
[135] Fix | Delete
__version__ = '1.70' # Highest version of the spec this complies with
[136] Fix | Delete
[137] Fix | Delete
import math as _math
[138] Fix | Delete
import numbers as _numbers
[139] Fix | Delete
[140] Fix | Delete
try:
[141] Fix | Delete
from collections import namedtuple as _namedtuple
[142] Fix | Delete
DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent')
[143] Fix | Delete
except ImportError:
[144] Fix | Delete
DecimalTuple = lambda *args: args
[145] Fix | Delete
[146] Fix | Delete
# Rounding
[147] Fix | Delete
ROUND_DOWN = 'ROUND_DOWN'
[148] Fix | Delete
ROUND_HALF_UP = 'ROUND_HALF_UP'
[149] Fix | Delete
ROUND_HALF_EVEN = 'ROUND_HALF_EVEN'
[150] Fix | Delete
ROUND_CEILING = 'ROUND_CEILING'
[151] Fix | Delete
ROUND_FLOOR = 'ROUND_FLOOR'
[152] Fix | Delete
ROUND_UP = 'ROUND_UP'
[153] Fix | Delete
ROUND_HALF_DOWN = 'ROUND_HALF_DOWN'
[154] Fix | Delete
ROUND_05UP = 'ROUND_05UP'
[155] Fix | Delete
[156] Fix | Delete
# Errors
[157] Fix | Delete
[158] Fix | Delete
class DecimalException(ArithmeticError):
[159] Fix | Delete
"""Base exception class.
[160] Fix | Delete
[161] Fix | Delete
Used exceptions derive from this.
[162] Fix | Delete
If an exception derives from another exception besides this (such as
[163] Fix | Delete
Underflow (Inexact, Rounded, Subnormal) that indicates that it is only
[164] Fix | Delete
called if the others are present. This isn't actually used for
[165] Fix | Delete
anything, though.
[166] Fix | Delete
[167] Fix | Delete
handle -- Called when context._raise_error is called and the
[168] Fix | Delete
trap_enabler is not set. First argument is self, second is the
[169] Fix | Delete
context. More arguments can be given, those being after
[170] Fix | Delete
the explanation in _raise_error (For example,
[171] Fix | Delete
context._raise_error(NewError, '(-x)!', self._sign) would
[172] Fix | Delete
call NewError().handle(context, self._sign).)
[173] Fix | Delete
[174] Fix | Delete
To define a new exception, it should be sufficient to have it derive
[175] Fix | Delete
from DecimalException.
[176] Fix | Delete
"""
[177] Fix | Delete
def handle(self, context, *args):
[178] Fix | Delete
pass
[179] Fix | Delete
[180] Fix | Delete
[181] Fix | Delete
class Clamped(DecimalException):
[182] Fix | Delete
"""Exponent of a 0 changed to fit bounds.
[183] Fix | Delete
[184] Fix | Delete
This occurs and signals clamped if the exponent of a result has been
[185] Fix | Delete
altered in order to fit the constraints of a specific concrete
[186] Fix | Delete
representation. This may occur when the exponent of a zero result would
[187] Fix | Delete
be outside the bounds of a representation, or when a large normal
[188] Fix | Delete
number would have an encoded exponent that cannot be represented. In
[189] Fix | Delete
this latter case, the exponent is reduced to fit and the corresponding
[190] Fix | Delete
number of zero digits are appended to the coefficient ("fold-down").
[191] Fix | Delete
"""
[192] Fix | Delete
[193] Fix | Delete
class InvalidOperation(DecimalException):
[194] Fix | Delete
"""An invalid operation was performed.
[195] Fix | Delete
[196] Fix | Delete
Various bad things cause this:
[197] Fix | Delete
[198] Fix | Delete
Something creates a signaling NaN
[199] Fix | Delete
-INF + INF
[200] Fix | Delete
0 * (+-)INF
[201] Fix | Delete
(+-)INF / (+-)INF
[202] Fix | Delete
x % 0
[203] Fix | Delete
(+-)INF % x
[204] Fix | Delete
x._rescale( non-integer )
[205] Fix | Delete
sqrt(-x) , x > 0
[206] Fix | Delete
0 ** 0
[207] Fix | Delete
x ** (non-integer)
[208] Fix | Delete
x ** (+-)INF
[209] Fix | Delete
An operand is invalid
[210] Fix | Delete
[211] Fix | Delete
The result of the operation after these is a quiet positive NaN,
[212] Fix | Delete
except when the cause is a signaling NaN, in which case the result is
[213] Fix | Delete
also a quiet NaN, but with the original sign, and an optional
[214] Fix | Delete
diagnostic information.
[215] Fix | Delete
"""
[216] Fix | Delete
def handle(self, context, *args):
[217] Fix | Delete
if args:
[218] Fix | Delete
ans = _dec_from_triple(args[0]._sign, args[0]._int, 'n', True)
[219] Fix | Delete
return ans._fix_nan(context)
[220] Fix | Delete
return _NaN
[221] Fix | Delete
[222] Fix | Delete
class ConversionSyntax(InvalidOperation):
[223] Fix | Delete
"""Trying to convert badly formed string.
[224] Fix | Delete
[225] Fix | Delete
This occurs and signals invalid-operation if a string is being
[226] Fix | Delete
converted to a number and it does not conform to the numeric string
[227] Fix | Delete
syntax. The result is [0,qNaN].
[228] Fix | Delete
"""
[229] Fix | Delete
def handle(self, context, *args):
[230] Fix | Delete
return _NaN
[231] Fix | Delete
[232] Fix | Delete
class DivisionByZero(DecimalException, ZeroDivisionError):
[233] Fix | Delete
"""Division by 0.
[234] Fix | Delete
[235] Fix | Delete
This occurs and signals division-by-zero if division of a finite number
[236] Fix | Delete
by zero was attempted (during a divide-integer or divide operation, or a
[237] Fix | Delete
power operation with negative right-hand operand), and the dividend was
[238] Fix | Delete
not zero.
[239] Fix | Delete
[240] Fix | Delete
The result of the operation is [sign,inf], where sign is the exclusive
[241] Fix | Delete
or of the signs of the operands for divide, or is 1 for an odd power of
[242] Fix | Delete
-0, for power.
[243] Fix | Delete
"""
[244] Fix | Delete
[245] Fix | Delete
def handle(self, context, sign, *args):
[246] Fix | Delete
return _SignedInfinity[sign]
[247] Fix | Delete
[248] Fix | Delete
class DivisionImpossible(InvalidOperation):
[249] Fix | Delete
"""Cannot perform the division adequately.
[250] Fix | Delete
[251] Fix | Delete
This occurs and signals invalid-operation if the integer result of a
[252] Fix | Delete
divide-integer or remainder operation had too many digits (would be
[253] Fix | Delete
longer than precision). The result is [0,qNaN].
[254] Fix | Delete
"""
[255] Fix | Delete
[256] Fix | Delete
def handle(self, context, *args):
[257] Fix | Delete
return _NaN
[258] Fix | Delete
[259] Fix | Delete
class DivisionUndefined(InvalidOperation, ZeroDivisionError):
[260] Fix | Delete
"""Undefined result of division.
[261] Fix | Delete
[262] Fix | Delete
This occurs and signals invalid-operation if division by zero was
[263] Fix | Delete
attempted (during a divide-integer, divide, or remainder operation), and
[264] Fix | Delete
the dividend is also zero. The result is [0,qNaN].
[265] Fix | Delete
"""
[266] Fix | Delete
[267] Fix | Delete
def handle(self, context, *args):
[268] Fix | Delete
return _NaN
[269] Fix | Delete
[270] Fix | Delete
class Inexact(DecimalException):
[271] Fix | Delete
"""Had to round, losing information.
[272] Fix | Delete
[273] Fix | Delete
This occurs and signals inexact whenever the result of an operation is
[274] Fix | Delete
not exact (that is, it needed to be rounded and any discarded digits
[275] Fix | Delete
were non-zero), or if an overflow or underflow condition occurs. The
[276] Fix | Delete
result in all cases is unchanged.
[277] Fix | Delete
[278] Fix | Delete
The inexact signal may be tested (or trapped) to determine if a given
[279] Fix | Delete
operation (or sequence of operations) was inexact.
[280] Fix | Delete
"""
[281] Fix | Delete
[282] Fix | Delete
class InvalidContext(InvalidOperation):
[283] Fix | Delete
"""Invalid context. Unknown rounding, for example.
[284] Fix | Delete
[285] Fix | Delete
This occurs and signals invalid-operation if an invalid context was
[286] Fix | Delete
detected during an operation. This can occur if contexts are not checked
[287] Fix | Delete
on creation and either the precision exceeds the capability of the
[288] Fix | Delete
underlying concrete representation or an unknown or unsupported rounding
[289] Fix | Delete
was specified. These aspects of the context need only be checked when
[290] Fix | Delete
the values are required to be used. The result is [0,qNaN].
[291] Fix | Delete
"""
[292] Fix | Delete
[293] Fix | Delete
def handle(self, context, *args):
[294] Fix | Delete
return _NaN
[295] Fix | Delete
[296] Fix | Delete
class Rounded(DecimalException):
[297] Fix | Delete
"""Number got rounded (not necessarily changed during rounding).
[298] Fix | Delete
[299] Fix | Delete
This occurs and signals rounded whenever the result of an operation is
[300] Fix | Delete
rounded (that is, some zero or non-zero digits were discarded from the
[301] Fix | Delete
coefficient), or if an overflow or underflow condition occurs. The
[302] Fix | Delete
result in all cases is unchanged.
[303] Fix | Delete
[304] Fix | Delete
The rounded signal may be tested (or trapped) to determine if a given
[305] Fix | Delete
operation (or sequence of operations) caused a loss of precision.
[306] Fix | Delete
"""
[307] Fix | Delete
[308] Fix | Delete
class Subnormal(DecimalException):
[309] Fix | Delete
"""Exponent < Emin before rounding.
[310] Fix | Delete
[311] Fix | Delete
This occurs and signals subnormal whenever the result of a conversion or
[312] Fix | Delete
operation is subnormal (that is, its adjusted exponent is less than
[313] Fix | Delete
Emin, before any rounding). The result in all cases is unchanged.
[314] Fix | Delete
[315] Fix | Delete
The subnormal signal may be tested (or trapped) to determine if a given
[316] Fix | Delete
or operation (or sequence of operations) yielded a subnormal result.
[317] Fix | Delete
"""
[318] Fix | Delete
[319] Fix | Delete
class Overflow(Inexact, Rounded):
[320] Fix | Delete
"""Numerical overflow.
[321] Fix | Delete
[322] Fix | Delete
This occurs and signals overflow if the adjusted exponent of a result
[323] Fix | Delete
(from a conversion or from an operation that is not an attempt to divide
[324] Fix | Delete
by zero), after rounding, would be greater than the largest value that
[325] Fix | Delete
can be handled by the implementation (the value Emax).
[326] Fix | Delete
[327] Fix | Delete
The result depends on the rounding mode:
[328] Fix | Delete
[329] Fix | Delete
For round-half-up and round-half-even (and for round-half-down and
[330] Fix | Delete
round-up, if implemented), the result of the operation is [sign,inf],
[331] Fix | Delete
where sign is the sign of the intermediate result. For round-down, the
[332] Fix | Delete
result is the largest finite number that can be represented in the
[333] Fix | Delete
current precision, with the sign of the intermediate result. For
[334] Fix | Delete
round-ceiling, the result is the same as for round-down if the sign of
[335] Fix | Delete
the intermediate result is 1, or is [0,inf] otherwise. For round-floor,
[336] Fix | Delete
the result is the same as for round-down if the sign of the intermediate
[337] Fix | Delete
result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded
[338] Fix | Delete
will also be raised.
[339] Fix | Delete
"""
[340] Fix | Delete
[341] Fix | Delete
def handle(self, context, sign, *args):
[342] Fix | Delete
if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN,
[343] Fix | Delete
ROUND_HALF_DOWN, ROUND_UP):
[344] Fix | Delete
return _SignedInfinity[sign]
[345] Fix | Delete
if sign == 0:
[346] Fix | Delete
if context.rounding == ROUND_CEILING:
[347] Fix | Delete
return _SignedInfinity[sign]
[348] Fix | Delete
return _dec_from_triple(sign, '9'*context.prec,
[349] Fix | Delete
context.Emax-context.prec+1)
[350] Fix | Delete
if sign == 1:
[351] Fix | Delete
if context.rounding == ROUND_FLOOR:
[352] Fix | Delete
return _SignedInfinity[sign]
[353] Fix | Delete
return _dec_from_triple(sign, '9'*context.prec,
[354] Fix | Delete
context.Emax-context.prec+1)
[355] Fix | Delete
[356] Fix | Delete
[357] Fix | Delete
class Underflow(Inexact, Rounded, Subnormal):
[358] Fix | Delete
"""Numerical underflow with result rounded to 0.
[359] Fix | Delete
[360] Fix | Delete
This occurs and signals underflow if a result is inexact and the
[361] Fix | Delete
adjusted exponent of the result would be smaller (more negative) than
[362] Fix | Delete
the smallest value that can be handled by the implementation (the value
[363] Fix | Delete
Emin). That is, the result is both inexact and subnormal.
[364] Fix | Delete
[365] Fix | Delete
The result after an underflow will be a subnormal number rounded, if
[366] Fix | Delete
necessary, so that its exponent is not less than Etiny. This may result
[367] Fix | Delete
in 0 with the sign of the intermediate result and an exponent of Etiny.
[368] Fix | Delete
[369] Fix | Delete
In all cases, Inexact, Rounded, and Subnormal will also be raised.
[370] Fix | Delete
"""
[371] Fix | Delete
[372] Fix | Delete
# List of public traps and flags
[373] Fix | Delete
_signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded,
[374] Fix | Delete
Underflow, InvalidOperation, Subnormal]
[375] Fix | Delete
[376] Fix | Delete
# Map conditions (per the spec) to signals
[377] Fix | Delete
_condition_map = {ConversionSyntax:InvalidOperation,
[378] Fix | Delete
DivisionImpossible:InvalidOperation,
[379] Fix | Delete
DivisionUndefined:InvalidOperation,
[380] Fix | Delete
InvalidContext:InvalidOperation}
[381] Fix | Delete
[382] Fix | Delete
##### Context Functions ##################################################
[383] Fix | Delete
[384] Fix | Delete
# The getcontext() and setcontext() function manage access to a thread-local
[385] Fix | Delete
# current context. Py2.4 offers direct support for thread locals. If that
[386] Fix | Delete
# is not available, use threading.currentThread() which is slower but will
[387] Fix | Delete
# work for older Pythons. If threads are not part of the build, create a
[388] Fix | Delete
# mock threading object with threading.local() returning the module namespace.
[389] Fix | Delete
[390] Fix | Delete
try:
[391] Fix | Delete
import threading
[392] Fix | Delete
except ImportError:
[393] Fix | Delete
# Python was compiled without threads; create a mock object instead
[394] Fix | Delete
import sys
[395] Fix | Delete
class MockThreading(object):
[396] Fix | Delete
def local(self, sys=sys):
[397] Fix | Delete
return sys.modules[__name__]
[398] Fix | Delete
threading = MockThreading()
[399] Fix | Delete
del sys, MockThreading
[400] Fix | Delete
[401] Fix | Delete
try:
[402] Fix | Delete
threading.local
[403] Fix | Delete
[404] Fix | Delete
except AttributeError:
[405] Fix | Delete
[406] Fix | Delete
# To fix reloading, force it to create a new context
[407] Fix | Delete
# Old contexts have different exceptions in their dicts, making problems.
[408] Fix | Delete
if hasattr(threading.currentThread(), '__decimal_context__'):
[409] Fix | Delete
del threading.currentThread().__decimal_context__
[410] Fix | Delete
[411] Fix | Delete
def setcontext(context):
[412] Fix | Delete
"""Set this thread's context to context."""
[413] Fix | Delete
if context in (DefaultContext, BasicContext, ExtendedContext):
[414] Fix | Delete
context = context.copy()
[415] Fix | Delete
context.clear_flags()
[416] Fix | Delete
threading.currentThread().__decimal_context__ = context
[417] Fix | Delete
[418] Fix | Delete
def getcontext():
[419] Fix | Delete
"""Returns this thread's context.
[420] Fix | Delete
[421] Fix | Delete
If this thread does not yet have a context, returns
[422] Fix | Delete
a new context and sets this thread's context.
[423] Fix | Delete
New contexts are copies of DefaultContext.
[424] Fix | Delete
"""
[425] Fix | Delete
try:
[426] Fix | Delete
return threading.currentThread().__decimal_context__
[427] Fix | Delete
except AttributeError:
[428] Fix | Delete
context = Context()
[429] Fix | Delete
threading.currentThread().__decimal_context__ = context
[430] Fix | Delete
return context
[431] Fix | Delete
[432] Fix | Delete
else:
[433] Fix | Delete
[434] Fix | Delete
local = threading.local()
[435] Fix | Delete
if hasattr(local, '__decimal_context__'):
[436] Fix | Delete
del local.__decimal_context__
[437] Fix | Delete
[438] Fix | Delete
def getcontext(_local=local):
[439] Fix | Delete
"""Returns this thread's context.
[440] Fix | Delete
[441] Fix | Delete
If this thread does not yet have a context, returns
[442] Fix | Delete
a new context and sets this thread's context.
[443] Fix | Delete
New contexts are copies of DefaultContext.
[444] Fix | Delete
"""
[445] Fix | Delete
try:
[446] Fix | Delete
return _local.__decimal_context__
[447] Fix | Delete
except AttributeError:
[448] Fix | Delete
context = Context()
[449] Fix | Delete
_local.__decimal_context__ = context
[450] Fix | Delete
return context
[451] Fix | Delete
[452] Fix | Delete
def setcontext(context, _local=local):
[453] Fix | Delete
"""Set this thread's context to context."""
[454] Fix | Delete
if context in (DefaultContext, BasicContext, ExtendedContext):
[455] Fix | Delete
context = context.copy()
[456] Fix | Delete
context.clear_flags()
[457] Fix | Delete
_local.__decimal_context__ = context
[458] Fix | Delete
[459] Fix | Delete
del threading, local # Don't contaminate the namespace
[460] Fix | Delete
[461] Fix | Delete
def localcontext(ctx=None):
[462] Fix | Delete
"""Return a context manager for a copy of the supplied context
[463] Fix | Delete
[464] Fix | Delete
Uses a copy of the current context if no context is specified
[465] Fix | Delete
The returned context manager creates a local decimal context
[466] Fix | Delete
in a with statement:
[467] Fix | Delete
def sin(x):
[468] Fix | Delete
with localcontext() as ctx:
[469] Fix | Delete
ctx.prec += 2
[470] Fix | Delete
# Rest of sin calculation algorithm
[471] Fix | Delete
# uses a precision 2 greater than normal
[472] Fix | Delete
return +s # Convert result to normal precision
[473] Fix | Delete
[474] Fix | Delete
def sin(x):
[475] Fix | Delete
with localcontext(ExtendedContext):
[476] Fix | Delete
# Rest of sin calculation algorithm
[477] Fix | Delete
# uses the Extended Context from the
[478] Fix | Delete
# General Decimal Arithmetic Specification
[479] Fix | Delete
return +s # Convert result to normal context
[480] Fix | Delete
[481] Fix | Delete
>>> setcontext(DefaultContext)
[482] Fix | Delete
>>> print getcontext().prec
[483] Fix | Delete
28
[484] Fix | Delete
>>> with localcontext():
[485] Fix | Delete
... ctx = getcontext()
[486] Fix | Delete
... ctx.prec += 2
[487] Fix | Delete
... print ctx.prec
[488] Fix | Delete
...
[489] Fix | Delete
30
[490] Fix | Delete
>>> with localcontext(ExtendedContext):
[491] Fix | Delete
... print getcontext().prec
[492] Fix | Delete
...
[493] Fix | Delete
9
[494] Fix | Delete
>>> print getcontext().prec
[495] Fix | Delete
28
[496] Fix | Delete
"""
[497] Fix | Delete
if ctx is None: ctx = getcontext()
[498] Fix | Delete
return _ContextManager(ctx)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function