[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.23,
1.24
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Fri Jun 25 13:46:23 EDT 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16613
Modified Files:
Decimal.py
Log Message:
Touchup isinfinity() and isnan().
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** Decimal.py 25 Jun 2004 06:17:02 -0000 1.23
--- Decimal.py 25 Jun 2004 17:46:20 -0000 1.24
***************
*** 99,103 ****
# 0.8.1 2004.06.23 Complies with immutability-near as discussed in c.l.py.
# 0.8.0 2004.06.21 Using the updated Spec (and complies with new test cases) from
! # Cowlishaw. Deprecated trim and rescale. Changed __str__ and
# __repr__ to return in sci format, added as_tuple(). Fixed the
# initial examples. Extracted lt, gt and eq methods from context.
--- 99,103 ----
# 0.8.1 2004.06.23 Complies with immutability-near as discussed in c.l.py.
# 0.8.0 2004.06.21 Using the updated Spec (and complies with new test cases) from
! # Cowlishaw. Deprecated trim and rescale. Changed __str__ and
# __repr__ to return in sci format, added as_tuple(). Fixed the
# initial examples. Extracted lt, gt and eq methods from context.
***************
*** 466,470 ****
if not isinstance(digit, (int,long)) or digit < 0:
raise ValueError, "The second value in the tuple must be composed of non negative integer elements."
!
self._sign = value[0]
self._int = tuple(value[1])
--- 466,470 ----
if not isinstance(digit, (int,long)) or digit < 0:
raise ValueError, "The second value in the tuple must be composed of non negative integer elements."
!
self._sign = value[0]
self._int = tuple(value[1])
***************
*** 521,525 ****
real_decimal_positions = positions + int_positions
d = d._round(real_decimal_positions, ROUND_HALF_UP)
!
return d
from_float = classmethod(from_float)
--- 521,525 ----
real_decimal_positions = positions + int_positions
d = d._round(real_decimal_positions, ROUND_HALF_UP)
!
return d
from_float = classmethod(from_float)
***************
*** 741,745 ****
#exp is closest mult. of 3 >= self._exp
exp = ((self._exp - 1)// 3 + 1) * 3
! if exp != self._exp:
s = '0.'+'0'*(exp - self._exp)
else:
--- 741,745 ----
#exp is closest mult. of 3 >= self._exp
exp = ((self._exp - 1)// 3 + 1) * 3
! if exp != self._exp:
s = '0.'+'0'*(exp - self._exp)
else:
***************
*** 955,959 ****
result.int = resultint = map(operator.add, op1.int, op2.int)
carry = 0
! for i in xrange(len(op1.int)):
tmp = resultint[i] + carry
carry = 0
--- 955,959 ----
result.int = resultint = map(operator.add, op1.int, op2.int)
carry = 0
! for i in xrange(len(op1.int)):
tmp = resultint[i] + carry
carry = 0
***************
*** 963,967 ****
resultint[i] = tmp
if carry:
! resultint.append(1)
else:
result.int = resultint = map(operator.sub, op1.int, op2.int)
--- 963,967 ----
resultint[i] = tmp
if carry:
! resultint.append(1)
else:
result.int = resultint = map(operator.sub, op1.int, op2.int)
***************
*** 975,979 ****
resultint[i] = tmp
assert not loan
!
while resultint[-1] == 0:
resultint.pop()
--- 975,979 ----
resultint[i] = tmp
assert not loan
!
while resultint[-1] == 0:
resultint.pop()
***************
*** 1733,1737 ****
if not modulo and n > 0 and (self._exp + len(self._int) - 1) * n > context.Emax \
and self:
!
tmp = Decimal('inf')
tmp._sign = sign
--- 1733,1737 ----
if not modulo and n > 0 and (self._exp + len(self._int) - 1) * n > context.Emax \
and self:
!
tmp = Decimal('inf')
tmp._sign = sign
***************
*** 1838,1842 ****
return self._isinfinity() and other._isinfinity() and True
return self._exp == other._exp
!
def _rescale(self, exp, rounding = None, context=None, watchexp = 1):
"""Rescales so that the exponent is exp.
--- 1838,1842 ----
return self._isinfinity() and other._isinfinity() and True
return self._exp == other._exp
!
def _rescale(self, exp, rounding = None, context=None, watchexp = 1):
"""Rescales so that the exponent is exp.
***************
*** 1873,1877 ****
if watchexp and digits > context.prec:
return context._raise_error(InvalidOperation, 'Rescale > prec')
!
tmp = Decimal(self)
tmp._int = (0,)+tmp._int
--- 1873,1877 ----
if watchexp and digits > context.prec:
return context._raise_error(InvalidOperation, 'Rescale > prec')
!
tmp = Decimal(self)
tmp._int = (0,)+tmp._int
***************
*** 1968,1972 ****
Emax, Emin = context.Emax, context.Emin
context.Emax, context.Emin = DEFAULT_MAX_EXPONENT, DEFAULT_MIN_EXPONENT
!
half = Decimal('0.5')
--- 1968,1972 ----
Emax, Emin = context.Emax, context.Emin
context.Emax, context.Emin = DEFAULT_MAX_EXPONENT, DEFAULT_MIN_EXPONENT
!
half = Decimal('0.5')
***************
*** 2109,2113 ****
int = property(_get_int)
exp = property(_get_exp)
!
# get rounding method function:
--- 2109,2113 ----
int = property(_get_int)
exp = property(_get_exp)
!
# get rounding method function:
***************
*** 2558,2574 ****
SetDefaultContext(DEFAULT_CONTEXT)
def isinfinity(num):
! """Determines whether a string or float is infinity."""
! try:
! if isinstance(num, float):
! num = str(num)
! num = num.lower()
! if num in ['inf', 'infinity', '+inf', '+infinity']:
! return 1
! if num in ['-inf', '-infinity']:
! return -1
! except AttributeError:
! pass
! return 0
def isnan(num):
--- 2558,2577 ----
SetDefaultContext(DEFAULT_CONTEXT)
+ _infinity_map = {
+ 'inf' : 1,
+ 'infinity' : 1,
+ '+inf' : 1,
+ '+infinity' : 1,
+ '-inf' : -1,
+ '-infinity' : -1
+ }
+
def isinfinity(num):
! """Determines whether a string or float is infinity.
!
! +1 for positive infinity; 0 for finite ; +1 for positive infinity
! """
! num = str(num).lower()
! return _infinity_map.get(num, 0)
def isnan(num):
***************
*** 2579,2591 ****
0 => not a NaN
"""
! if isinstance(num, float):
! num = str(num)
! if len(num) == 0:
return 0
! num = num.lower()
!
#get the sign, get rid of trailing [+-]
sign = 0
! if num[0] == '+':
num = num[1:]
elif num[0] == '-': #elif avoids '+-nan'
--- 2582,2592 ----
0 => not a NaN
"""
! num = str(num).lower()
! if not num:
return 0
!
#get the sign, get rid of trailing [+-]
sign = 0
! if num[0] == '+':
num = num[1:]
elif num[0] == '-': #elif avoids '+-nan'
More information about the Python-checkins
mailing list