[Python-checkins] r56775 - python/branches/decimal-branch/Lib/decimal.py
facundo.batista
python-checkins at python.org
Mon Aug 6 20:19:06 CEST 2007
Author: facundo.batista
Date: Mon Aug 6 20:19:06 2007
New Revision: 56775
Modified:
python/branches/decimal-branch/Lib/decimal.py
Log:
Coded next_plus() function, all tests pass ok. Also
cleaned slightly next_minus().
Modified: python/branches/decimal-branch/Lib/decimal.py
==============================================================================
--- python/branches/decimal-branch/Lib/decimal.py (original)
+++ python/branches/decimal-branch/Lib/decimal.py Mon Aug 6 20:19:06 2007
@@ -2698,21 +2698,68 @@
d = Decimal((newself._sign, newself._int+(0,)*expdif, newself._exp-expdif))
dif = Decimal((0, (0,)*(len(d._int)-1)+(1,), d._exp))
d = d - dif
- if d._isinfinity():
- return d
digdif = context.prec - len(d._int)
if digdif > 0 and d._exp-digdif > context.Emin:
d = Decimal((d._sign, d._int + (9,)*digdif, d._exp-digdif))
if d.adjusted() > context.Emax:
- if d._sign:
- return negInf
- else:
- return Inf
+ return negInf
return d
def next_plus(self, context=None):
"""Returns the smallest representable number larger than itself."""
+ if context is None:
+ context = getcontext()
+
+ ans = self._check_nans(self, context)
+ if ans:
+ return ans
+
+ if self._isinfinity() == 1:
+ return self
+ if self._isinfinity() == -1:
+ return Decimal((1, (9,)*context.prec, context.Emax-context.prec+1))
+
+ minime = context.Emin-context.prec+1
+ if self._exp < minime:
+ newself = Decimal((self._sign, self._int, minime))
+ if newself._sign == 0:
+ return newself
+ else:
+ newself = Decimal(self)
+
+ if not newself:
+ return Decimal((0, (1,), minime))
+ expdif = context.prec - len(newself._int)
+
+ if expdif < 0:
+ resto = newself._int[context.prec:]
+ if resto != (0,)*(-expdif):
+ # the rest is not all zeroes
+ d = newself._round_ceiling(context.prec, expdif, context)
+ return d
+ # negative expdif, but all zeroes
+ d = Decimal((newself._sign, newself._int[:expdif], newself._exp-expdif))
+ else:
+ # positive expdif
+ if newself._exp-expdif < minime:
+ expdif = newself._exp - minime
+ d = Decimal((newself._sign, newself._int+(0,)*expdif, newself._exp-expdif))
+
+ dif = Decimal((0, (0,)*(len(d._int)-1)+(1,), d._exp))
+ d = d + dif
+ if not d:
+ # restablish previos sign
+ d._sign = newself._sign
+ return d
+
+ digdif = context.prec - len(d._int)
+ if digdif > 0 and d._exp-digdif > context.Emin:
+ d = Decimal((d._sign, d._int + (9,)*digdif, d._exp-digdif))
+ if d.adjusted() > context.Emax:
+ return Inf
+ return d
+
def next_toward(self, other, context=None):
"""Returns the number closest to itself, in direction towards other.
More information about the Python-checkins
mailing list