[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.19,
1.20
facundobatista at users.sourceforge.net
facundobatista at users.sourceforge.net
Sat Jun 19 21:55:02 EDT 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6411
Modified Files:
Decimal.py
Log Message:
Changes because of the attributes and methods name changes.
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** Decimal.py 5 Apr 2004 22:07:21 -0000 1.19
--- Decimal.py 20 Jun 2004 01:54:59 -0000 1.20
***************
*** 97,100 ****
--- 97,101 ----
# facundobatista:
+ # 0.1.8 2003.06.19 Lots of name changes.
# 0.7.4 2003.04.05 Added rdiv, rdivmod, rmod, rpow, rfloordiv special methods.
# Fixed sub.
***************
*** 515,519 ****
if positions < 0:
raise TypeError, "positions must be not negative in Decimal.from_float(value, [positions])"
! # d = d.fix(positions)
# we must know what precision to pass to round
--- 516,520 ----
if positions < 0:
raise TypeError, "positions must be not negative in Decimal.from_float(value, [positions])"
! # d = d._fix(positions)
# we must know what precision to pass to round
***************
*** 666,670 ****
return 1
! def DecimalCmp(self, other, context=None):
"""Compares one to another.
--- 667,671 ----
return 1
! def compare(self, other, context=None):
"""Compares one to another.
***************
*** 796,800 ****
context = getcontext()
if context.rounding_decision in (ALWAYS_ROUND,):
! return str(self.fix(context=context))
return str(self)
--- 797,801 ----
context = getcontext()
if context.rounding_decision in (ALWAYS_ROUND,):
! return str(self._fix(context=context))
return str(self)
***************
*** 818,822 ****
sign = 1
if context.rounding_decision in (ALWAYS_ROUND,):
! return Decimal((sign, self._int, self._exp)).fix(context=context)
return Decimal( (sign, self._int, self._exp))
--- 819,823 ----
sign = 1
if context.rounding_decision in (ALWAYS_ROUND,):
! return Decimal((sign, self._int, self._exp))._fix(context=context)
return Decimal( (sign, self._int, self._exp))
***************
*** 838,842 ****
if context.rounding_decision in (ALWAYS_ROUND,):
! ans = self.fix(context=context)
else:
ans = Decimal(self)
--- 839,843 ----
if context.rounding_decision in (ALWAYS_ROUND,):
! ans = self._fix(context=context)
else:
ans = Decimal(self)
***************
*** 905,909 ****
ans = other.rescale(exp, watchexp=0, context=context)
if shouldround:
! ans = ans.fix(context=context)
return ans
if not other:
--- 906,910 ----
ans = other.rescale(exp, watchexp=0, context=context)
if shouldround:
! ans = ans._fix(context=context)
return ans
if not other:
***************
*** 912,916 ****
ans = self.rescale(exp, watchexp=0, context=context)
if shouldround:
! ans = ans.fix(context=context)
return ans
--- 913,917 ----
ans = self.rescale(exp, watchexp=0, context=context)
if shouldround:
! ans = ans._fix(context=context)
return ans
***************
*** 969,973 ****
ans = Decimal(result)
if shouldround:
! ans = ans.fix(context=context)
return ans
--- 970,974 ----
ans = Decimal(result)
if shouldround:
! ans = ans._fix(context=context)
return ans
***************
*** 1002,1006 ****
return other.__add__(tmp, context=context)
! def increment(self, round=1, context=None):
"""Special case of add, adding 1eExponent
--- 1003,1007 ----
return other.__add__(tmp, context=context)
! def _increment(self, round=1, context=None):
"""Special case of add, adding 1eExponent
***************
*** 1009,1013 ****
For example:
! Decimal('5.624e10').increment() == Decimal('5.625e10')
"""
if context is None:
--- 1010,1014 ----
For example:
! Decimal('5.624e10')._increment() == Decimal('5.625e10')
"""
if context is None:
***************
*** 1030,1034 ****
if round and context.rounding_decision in (ALWAYS_ROUND,):
! ans = ans.fix(context=context)
return ans
--- 1031,1035 ----
if round and context.rounding_decision in (ALWAYS_ROUND,):
! ans = ans._fix(context=context)
return ans
***************
*** 1065,1069 ****
if shouldround:
#Fixing in case the exponent is out of bounds
! ans = ans.fix(context=context)
return ans
--- 1066,1070 ----
if shouldround:
#Fixing in case the exponent is out of bounds
! ans = ans._fix(context=context)
return ans
***************
*** 1072,1081 ****
ans = Decimal((resultsign, other._int, resultexp))
if shouldround:
! ans = ans.fix(context=context)
return ans
if other._int == (1,):
ans = Decimal((resultsign, self._int, resultexp))
if shouldround:
! ans = ans.fix(context=context)
return ans
--- 1073,1082 ----
ans = Decimal((resultsign, other._int, resultexp))
if shouldround:
! ans = ans._fix(context=context)
return ans
if other._int == (1,):
ans = Decimal((resultsign, self._int, resultexp))
if shouldround:
! ans = ans._fix(context=context)
return ans
***************
*** 1106,1110 ****
ans = Decimal( (resultsign, accumulator, resultexp))
if shouldround:
! ans = ans.fix(context=context)
return ans
--- 1107,1111 ----
ans = Decimal( (resultsign, accumulator, resultexp))
if shouldround:
! ans = ans._fix(context=context)
return ans
***************
*** 1205,1209 ****
ans2 = self.rescale(exp, context=context, watchexp=0)
if shouldround:
! ans2 = ans2.fix(context=context)
return (Decimal( (sign, (0,), 0) ),
ans2)
--- 1206,1210 ----
ans2 = self.rescale(exp, context=context, watchexp=0)
if shouldround:
! ans2 = ans2._fix(context=context)
return (Decimal( (sign, (0,), 0) ),
ans2)
***************
*** 1230,1234 ****
(len(op2.int) == len(op1.int) and op2.int <= op1.int)):
#Meaning, while op2.int < op1.int, when normalized.
! res.increment()
op1.subtract(op2.int)
if res.exp == 0 and divmod:
--- 1231,1235 ----
(len(op2.int) == len(op1.int) and op2.int <= op1.int)):
#Meaning, while op2.int < op1.int, when normalized.
! res._increment()
op1.subtract(op2.int)
if res.exp == 0 and divmod:
***************
*** 1243,1247 ****
context.regard_flags(*frozen)
if shouldround:
! otherside = otherside.fix(context=context)
return (Decimal(res), otherside)
--- 1244,1248 ----
context.regard_flags(*frozen)
if shouldround:
! otherside = otherside._fix(context=context)
return (Decimal(res), otherside)
***************
*** 1284,1288 ****
ans = Decimal(res)
if shouldround:
! ans = ans.fix(context=context)
return ans
--- 1285,1289 ----
ans = Decimal(res)
if shouldround:
! ans = ans._fix(context=context)
return ans
***************
*** 1369,1373 ****
#Get flags now
self.__divmod__(other, context=context)
! return r.fix(context=context)
r._sign, comparison._sign = s1, s2
--- 1370,1374 ----
#Get flags now
self.__divmod__(other, context=context)
! return r._fix(context=context)
r._sign, comparison._sign = s1, s2
***************
*** 1400,1404 ****
r._sign, comparison._sign = s1, s2
! return r.fix(context=context)
def __floordiv__(self, other, context=None):
--- 1401,1405 ----
r._sign, comparison._sign = s1, s2
! return r._fix(context=context)
def __floordiv__(self, other, context=None):
***************
*** 1445,1449 ****
return int(self.__long__())
! def fix(self, prec=None, rounding=None, folddown=None, context=None):
"""Round if it is necessary to keep self within prec precision.
--- 1446,1450 ----
return int(self.__long__())
! def _fix(self, prec=None, rounding=None, folddown=None, context=None):
"""Round if it is necessary to keep self within prec precision.
***************
*** 1464,1476 ****
prec = context.prec
ans = Decimal(self)
! ans = ans.fixexponents(prec, rounding, folddown=folddown,
context=context)
if len(ans._int) > prec:
ans = ans.round(prec, rounding, context=context)
! ans = ans.fixexponents(prec, rounding, folddown=folddown,
context=context)
return ans
! def fixexponents(self, prec=None, rounding=None, folddown=None,
context=None):
"""Fix the exponents and return a copy with the exponent in bounds."""
--- 1465,1477 ----
prec = context.prec
ans = Decimal(self)
! ans = ans._fixexponents(prec, rounding, folddown=folddown,
context=context)
if len(ans._int) > prec:
ans = ans.round(prec, rounding, context=context)
! ans = ans._fixexponents(prec, rounding, folddown=folddown,
context=context)
return ans
! def _fixexponents(self, prec=None, rounding=None, folddown=None,
context=None):
"""Fix the exponents and return a copy with the exponent in bounds."""
***************
*** 1584,1588 ****
# Okay, let's round and lose data
! this_function = getattr(temp, self.pick_rounding_function[rounding])
#Now we've got the rounding function
--- 1585,1589 ----
# Okay, let's round and lose data
! this_function = getattr(temp, self._pick_rounding_function[rounding])
#Now we've got the rounding function
***************
*** 1596,1600 ****
return ans
! pick_rounding_function = {}
def _round_down(self, prec, expdiff, context):
--- 1597,1601 ----
return ans
! _pick_rounding_function = {}
def _round_down(self, prec, expdiff, context):
***************
*** 1608,1612 ****
tmp = Decimal( (self._sign,self._int[:prec], self._exp - expdiff))
if self._int[prec] >= 5:
! tmp = tmp.increment(round=0, context=context)
if len(tmp._int) > prec:
return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1))
--- 1609,1613 ----
tmp = Decimal( (self._sign,self._int[:prec], self._exp - expdiff))
if self._int[prec] >= 5:
! tmp = tmp._increment(round=0, context=context)
if len(tmp._int) > prec:
return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1))
***************
*** 1647,1651 ****
for digit in self._int[prec:]:
if digit != 0:
! tmp = tmp.increment(round=1, context=context)
if len(tmp._int) > prec:
return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1))
--- 1648,1652 ----
for digit in self._int[prec:]:
if digit != 0:
! tmp = tmp._increment(round=1, context=context)
if len(tmp._int) > prec:
return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1))
***************
*** 1667,1671 ****
return self._round_up(prec, expdiff, context)
! def fixedPoint(self, digits, rounding = None, context=None):
"""Rounds to a number of digits around the decimal point.
--- 1668,1672 ----
return self._round_up(prec, expdiff, context)
! def _fixedPoint(self, digits, rounding = None, context=None):
"""Rounds to a number of digits around the decimal point.
***************
*** 1674,1678 ****
rounding before the decimal point.
! >>> str(Decimal("1234.34567").fixedPoint(2))
'1234.35'
"""
--- 1675,1679 ----
rounding before the decimal point.
! >>> str(Decimal("1234.34567")._fixedPoint(2))
'1234.35'
"""
***************
*** 1705,1709 ****
return tmp
! def eng(self, context=None):
"""Convert to engineering-type string.
--- 1706,1710 ----
return tmp
! def to_eng_string(self, context=None):
"""Convert to engineering-type string.
***************
*** 1718,1722 ****
ans = self
if shouldround:
! ans = ans.fix(context=context)
return ans.__str__(1)
--- 1719,1723 ----
ans = self
if shouldround:
! ans = ans._fix(context=context)
return ans.__str__(1)
***************
*** 1738,1742 ****
return ans
! if not n._isinfinity() and not n.isinteger():
return context.raise_error(InvalidOperation, 'x ** (non-integer)')
--- 1739,1743 ----
return ans
! if not n._isinfinity() and not n._isinteger():
return context.raise_error(InvalidOperation, 'x ** (non-integer)')
***************
*** 1808,1812 ****
if shouldround:
! return val.fix(context=context)
return val
--- 1809,1813 ----
if shouldround:
! return val._fix(context=context)
return val
***************
*** 1825,1829 ****
return ans
! copy = self.fix(context=context)
if copy._isinfinity():
return copy
--- 1826,1830 ----
return ans
! copy = self._fix(context=context)
if copy._isinfinity():
return copy
***************
*** 1858,1862 ****
return self.rescale(exp._exp, rounding, context, watchexp)
! def samequantum(self, other, context=None):
"""Test whether self and other have the same exponent.
--- 1859,1863 ----
return self.rescale(exp._exp, rounding, context, watchexp)
! def same_quantum(self, other, context=None):
"""Test whether self and other have the same exponent.
***************
*** 1934,1938 ****
return self.rescale(0, rounding, context=context)
! def tointegral(self, rounding = None, context=None):
"""Rounds to the nearest integer, without raising inexact, rounded."""
if context is None:
--- 1935,1939 ----
return self.rescale(0, rounding, context=context)
! def to_integral(self, rounding = None, context=None):
"""Rounds to the nearest integer, without raising inexact, rounded."""
if context is None:
***************
*** 2048,2052 ****
context.prec = firstprec
context.rounding = rounding
! ans = ans.fix(context=context)
rounding = context.set_rounding_decision(NEVER_ROUND)
--- 2049,2053 ----
context.prec = firstprec
context.rounding = rounding
! ans = ans._fix(context=context)
rounding = context.set_rounding_decision(NEVER_ROUND)
***************
*** 2068,2072 ****
context.Emax, context.Emin = Emax, Emin
! return ans.fix(context=context)
def max(self, other, context=None):
--- 2069,2073 ----
context.Emax, context.Emin = Emax, Emin
! return ans._fix(context=context)
def max(self, other, context=None):
***************
*** 2089,2093 ****
shouldround = context.rounding_decision in (ALWAYS_ROUND,)
if shouldround:
! ans = ans.fix(context=context)
return ans
--- 2090,2094 ----
shouldround = context.rounding_decision in (ALWAYS_ROUND,)
if shouldround:
! ans = ans._fix(context=context)
return ans
***************
*** 2112,2116 ****
if context.rounding_decision in (ALWAYS_ROUND,):
! ans = ans.fix(context=context)
return ans
--- 2113,2117 ----
if context.rounding_decision in (ALWAYS_ROUND,):
! ans = ans._fix(context=context)
return ans
***************
*** 2126,2137 ****
# return None
! def isinteger(self):
"""Returns whether self is an integer"""
if self._exp >= 0:
! return Decimal(1)
rest = self._int[self._exp:]
if rest == (0,)*len(rest):
! return Decimal(1)
! return Decimal(0)
def _iseven(self):
--- 2127,2138 ----
# return None
! def _isinteger(self):
"""Returns whether self is an integer"""
if self._exp >= 0:
! return True
rest = self._int[self._exp:]
if rest == (0,)*len(rest):
! return True
! return False
def _iseven(self):
***************
*** 2156,2160 ****
globalname = name[1:].upper()
val = globals()[globalname]
! Decimal.pick_rounding_function[val] = name
--- 2157,2161 ----
globalname = name[1:].upper()
val = globals()[globalname]
! Decimal._pick_rounding_function[val] = name
***************
*** 2299,2305 ****
return a.__add__(b, context=self)
def apply(self, a):
! return str(a.fix(context=self))
def compare(self, a, b):
! return a.DecimalCmp(b, context=self)
def lt(self, a, b):
return Decimal(int(self.compare(a, b)) == -1)
--- 2300,2306 ----
return a.__add__(b, context=self)
def apply(self, a):
! return str(a._fix(context=self))
def compare(self, a, b):
! return a.compare(b, context=self)
def lt(self, a, b):
return Decimal(int(self.compare(a, b)) == -1)
***************
*** 2310,2319 ****
def divide(self, a, b):
return a.__div__(b, context=self)
! def divideint(self, a, b):
return a.__floordiv__(b, context=self)
def divmod(self, a, b):
return a.__divmod__(b, context=self)
- def fix(self, a, prec=None):
- return a.fix(context=self, prec=prec)
def integer(self, a):
return a.round_to_integer(context=self)
--- 2311,2318 ----
def divide(self, a, b):
return a.__div__(b, context=self)
! def divide_int(self, a, b):
return a.__floordiv__(b, context=self)
def divmod(self, a, b):
return a.__divmod__(b, context=self)
def integer(self, a):
return a.round_to_integer(context=self)
***************
*** 2338,2342 ****
def remainder(self, a, b):
return a.__mod__(b, context=self)
! def remaindernear(self, a, b):
return a.remainder_near(b, context=self)
def quantize(self, a, b):
--- 2337,2341 ----
def remainder(self, a, b):
return a.__mod__(b, context=self)
! def remainder_near(self, a, b):
return a.remainder_near(b, context=self)
def quantize(self, a, b):
***************
*** 2344,2359 ****
def rescale(self, a, b):
return a.rescale(b, context=self)
! def samequantum(self, a, b):
! return a.samequantum(b, context=self)
def squareroot(self, a):
return a.sqrt(context=self)
def subtract(self, a, b):
return a.__sub__(b, context=self)
! def toeng(self, a):
! return a.eng(context=self)
! def tosci(self, a):
return a.sci(context=self)
! def tointegral(self, a):
! return a.tointegral(context=self)
def trim(self, a):
return a.trim(context=self)
--- 2343,2358 ----
def rescale(self, a, b):
return a.rescale(b, context=self)
! def same_quantum(self, a, b):
! return a.same_quantum(b, context=self)
def squareroot(self, a):
return a.sqrt(context=self)
def subtract(self, a, b):
return a.__sub__(b, context=self)
! def to_eng_string(self, a):
! return a.to_eng_string(context=self)
! def to_sci_string(self, a):
return a.sci(context=self)
! def to_integral(self, a):
! return a.to_integral(context=self)
def trim(self, a):
return a.trim(context=self)
***************
*** 2422,2426 ****
return 0
! def increment(self):
curspot = len(self.int) - 1
self.int[curspot]+= 1
--- 2421,2425 ----
return 0
! def _increment(self):
curspot = len(self.int) - 1
self.int[curspot]+= 1
More information about the Python-checkins
mailing list