[Python-checkins] r57784 - in python/branches/decimal-branch/Lib: decimal.py test/test_decimal.py

facundo.batista python-checkins at python.org
Fri Aug 31 02:10:35 CEST 2007


Author: facundo.batista
Date: Fri Aug 31 02:10:34 2007
New Revision: 57784

Modified:
   python/branches/decimal-branch/Lib/decimal.py
   python/branches/decimal-branch/Lib/test/test_decimal.py
Log:

Added function name translation for compare_signal and reduce/normalize 
in the test_decimal.py file.

Fixed comparesig to make sure that diagnostic information attached to 
NaNs isn't lost.  Added a call to _fix at the end of quantize; this 
takes care of any folddown that's necessary. And finally, make sure 
that normalize doesn't remove too many zeros.

Thanks Mark Dickinson.


Modified: python/branches/decimal-branch/Lib/decimal.py
==============================================================================
--- python/branches/decimal-branch/Lib/decimal.py	(original)
+++ python/branches/decimal-branch/Lib/decimal.py	Fri Aug 31 02:10:34 2007
@@ -2335,6 +2335,9 @@
     def normalize(self, context=None):
         """Normalize- strip trailing 0s, change anything equal to 0 to 0e0"""
 
+        if context is None:
+            context = getcontext()
+
         if self._is_special:
             ans = self._check_nans(context=context)
             if ans:
@@ -2346,9 +2349,10 @@
 
         if not dup:
             return Decimal( (dup._sign, (0,), 0) )
+        exp_max = [context.Emax, context.Etop()][context._clamp]
         end = len(dup._int)
         exp = dup._exp
-        while dup._int[end-1] == 0:
+        while dup._int[end-1] == 0 and exp < exp_max:
             exp += 1
             end -= 1
         return Decimal( (dup._sign, dup._int[:end], exp) )
@@ -2359,6 +2363,8 @@
 
         Similar to self._rescale(exp._exp) but with error checking.
         """
+        if context is None:
+            context = getcontext()
         if self._is_special or exp._is_special:
             ans = self._check_nans(exp, context)
             if ans:
@@ -2367,11 +2373,12 @@
             if exp._isinfinity() or self._isinfinity():
                 if exp._isinfinity() and self._isinfinity():
                     return self  # if both are inf, it is OK
-                if context is None:
-                    context = getcontext()
                 return context._raise_error(InvalidOperation,
                                         'quantize with one INF')
-        return self._rescale(exp._exp, rounding, context, watchexp=0, fromQuantize=True)
+        ans = self._rescale(exp._exp, rounding, context, watchexp=0, fromQuantize=True)
+        # call to fix takes care of any necessary folddown
+        ans = ans._fix(context)
+        return ans
 
     def same_quantum(self, other):
         """Test whether self and other have the same exponent.
@@ -2694,8 +2701,23 @@
         It's pretty much like compare(), but all NaNs signal, with signaling
         NaNs taking precedence over quiet NaNs.
         """
-        if self._isnan() or other._isnan():
-            return context._raise_error(InvalidOperation)
+        if context is None:
+            context = getcontext()
+
+        self_is_nan = self._isnan()
+        other_is_nan = other._isnan()
+        if self_is_nan == 2:
+            return context._raise_error(InvalidOperation, 'sNaN',
+                                        1, self)
+        if other_is_nan == 2:
+            return context._raise_error(InvalidOperation, 'sNaN',
+                                        1, other)
+        if self_is_nan:
+            return context._raise_error(InvalidOperation, 'NaN in compare_signal',
+                                        1, self)
+        if other_is_nan:
+            return context._raise_error(InvalidOperation, 'NaN in compare_signal',
+                                        1, other)
         return self.compare(other, context=context)
 
     def compare_total(self, other):

Modified: python/branches/decimal-branch/Lib/test/test_decimal.py
==============================================================================
--- python/branches/decimal-branch/Lib/test/test_decimal.py	(original)
+++ python/branches/decimal-branch/Lib/test/test_decimal.py	Fri Aug 31 02:10:34 2007
@@ -110,6 +110,7 @@
                'squareroot':'sqrt',
                'apply':'_apply',
                'class':'number_class',
+               'comparesig':'compare_signal',
                'comparetotal':'compare_total',
                'comparetotmag':'compare_total_mag',
                'copyabs':'copy_abs',
@@ -125,7 +126,7 @@
                'nextminus':'next_minus',
                'nextplus':'next_plus',
                'nexttoward':'next_toward',
-               'trim':'normalize',
+               'reduce':'normalize',
               }
 
 # For some operations (currently exp, ln, log10, power), the decNumber


More information about the Python-checkins mailing list