~ubuntu-branches/ubuntu/natty/python3.2/natty-security

« back to all changes in this revision

Viewing changes to Lib/decimal.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-12-06 12:19:09 UTC
  • mfrom: (1.1.3 upstream) (7.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20101206121909-c40vnqniur1fq5lx
Tags: 3.2~b1-1
* Python 3.2 beta1 release.
* Configure with --enable-loadable-sqlite-extensions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
132
132
]
133
133
 
134
134
__version__ = '1.70'    # Highest version of the spec this complies with
 
135
                        # See http://speleotrove.com/decimal/
135
136
 
136
137
import copy as _copy
137
138
import math as _math
957
958
        else:
958
959
            exp_hash = pow(_PyHASH_10INV, -self._exp, _PyHASH_MODULUS)
959
960
        hash_ = int(self._int) * exp_hash % _PyHASH_MODULUS
960
 
        return hash_ if self >= 0 else -hash_
 
961
        ans = hash_ if self >= 0 else -hash_
 
962
        return -2 if ans == -1 else ans
961
963
 
962
964
    def as_tuple(self):
963
965
        """Represents the number as a triple tuple.
5525
5527
 
5526
5528
##### Integer arithmetic functions used by ln, log10, exp and __pow__ #####
5527
5529
 
5528
 
# This function from Tim Peters was taken from here:
5529
 
# http://mail.python.org/pipermail/python-list/1999-July/007758.html
5530
 
# The correction being in the function definition is for speed, and
5531
 
# the whole function is not resolved with math.log because of avoiding
5532
 
# the use of floats.
5533
 
def _nbits(n, correction = {
5534
 
        '0': 4, '1': 3, '2': 2, '3': 2,
5535
 
        '4': 1, '5': 1, '6': 1, '7': 1,
5536
 
        '8': 0, '9': 0, 'a': 0, 'b': 0,
5537
 
        'c': 0, 'd': 0, 'e': 0, 'f': 0}):
5538
 
    """Number of bits in binary representation of the positive integer n,
5539
 
    or 0 if n == 0.
5540
 
    """
5541
 
    if n < 0:
5542
 
        raise ValueError("The argument to _nbits should be nonnegative.")
5543
 
    hex_n = "%x" % n
5544
 
    return 4*len(hex_n) - correction[hex_n[0]]
 
5530
_nbits = int.bit_length
5545
5531
 
5546
5532
def _sqrt_nearest(n, a):
5547
5533
    """Closest integer to the square root of the positive integer n.  a is
5990
5976
#
5991
5977
# A format specifier for Decimal looks like:
5992
5978
#
5993
 
#   [[fill]align][sign][0][minimumwidth][,][.precision][type]
 
5979
#   [[fill]align][sign][#][0][minimumwidth][,][.precision][type]
5994
5980
 
5995
5981
_parse_format_specifier_regex = re.compile(r"""\A
5996
5982
(?:
5998
5984
   (?P<align>[<>=^])
5999
5985
)?
6000
5986
(?P<sign>[-+ ])?
 
5987
(?P<alt>\#)?
6001
5988
(?P<zeropad>0)?
6002
5989
(?P<minimumwidth>(?!0)\d+)?
6003
5990
(?P<thousands_sep>,)?
6213
6200
 
6214
6201
    sign = _format_sign(is_negative, spec)
6215
6202
 
6216
 
    if fracpart:
 
6203
    if fracpart or spec['alt']:
6217
6204
        fracpart = spec['decimal_point'] + fracpart
6218
6205
 
6219
6206
    if exp != 0 or spec['type'] in 'eE':