]> granicus.if.org Git - python/commitdiff
Issue #7233: Fix Decimal.shift and Decimal.rotate methods for
authorMark Dickinson <dickinsm@gmail.com>
Thu, 29 Oct 2009 12:11:18 +0000 (12:11 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Thu, 29 Oct 2009 12:11:18 +0000 (12:11 +0000)
arguments with more digits than the current context precision.
Bug reported by Stefan Krah.

Lib/decimal.py
Lib/test/decimaltestdata/extra.decTest
Misc/NEWS

index be1a82783fd4073e6e5e20e257418107eb88f61e..83933651d21ab8a2a220f33365a3b00ad784d5f5 100644 (file)
@@ -3439,8 +3439,10 @@ class Decimal(object):
         torot = int(other)
         rotdig = self._int
         topad = context.prec - len(rotdig)
-        if topad:
+        if topad > 0:
             rotdig = '0'*topad + rotdig
+        elif topad < 0:
+            rotdig = rotdig[-topad:]
 
         # let's rotate!
         rotated = rotdig[torot:] + rotdig[:torot]
@@ -3493,22 +3495,22 @@ class Decimal(object):
 
         # get values, pad if necessary
         torot = int(other)
-        if not torot:
-            return Decimal(self)
         rotdig = self._int
         topad = context.prec - len(rotdig)
-        if topad:
+        if topad > 0:
             rotdig = '0'*topad + rotdig
+        elif topad < 0:
+            rotdig = rotdig[-topad:]
 
         # let's shift!
         if torot < 0:
-            rotated = rotdig[:torot]
+            shifted = rotdig[:torot]
         else:
-            rotated = rotdig + '0'*torot
-            rotated = rotated[-context.prec:]
+            shifted = rotdig + '0'*torot
+            shifted = shifted[-context.prec:]
 
         return _dec_from_triple(self._sign,
-                                    rotated.lstrip('0') or '0', self._exp)
+                                    shifted.lstrip('0') or '0', self._exp)
 
     # Support for pickling, copy, and deepcopy
     def __reduce__(self):
index f80d5f3fc538786ecc93aea1f8d8cac3ead564a1..2640842c68eb9b4e32a8f54317bcd040c1371caa 100644 (file)
@@ -186,6 +186,35 @@ extr1507 comparetotal -sNaN45 -sNaN123 -> 1
 extr1510 comparetotal -sNaN63450748854172416 -sNaN911993 -> -1
 extr1511 comparetotmag NaN1222222222222 -NaN999999 -> 1
 
+-- Issue #7233: rotate and scale should truncate an argument
+-- of length greater than the current precision.
+precision: 4
+extr1600 rotate 1234567 -5 -> NaN Invalid_operation
+extr1601 rotate 1234567 -4 -> 4567
+extr1602 rotate 1234567 -3 -> 5674
+extr1603 rotate 1234567 -2 -> 6745
+extr1604 rotate 1234567 -1 -> 7456
+extr1605 rotate 1234567 0 -> 4567
+extr1606 rotate 1234567 1 -> 5674
+extr1607 rotate 1234567 2 -> 6745
+extr1608 rotate 1234567 3 -> 7456
+extr1609 rotate 1234567 4 -> 4567
+extr1610 rotate 1234567 5 -> NaN Invalid_operation
+
+extr1650 shift 1234567 -5 -> NaN Invalid_operation
+extr1651 shift 1234567 -4 -> 0
+extr1652 shift 1234567 -3 -> 4
+extr1653 shift 1234567 -2 -> 45
+extr1654 shift 1234567 -1 -> 456
+extr1655 shift 1234567 0 -> 4567
+extr1656 shift 1234567 1 -> 5670
+extr1657 shift 1234567 2 -> 6700
+extr1658 shift 1234567 3 -> 7000
+extr1659 shift 1234567 4 -> 0
+extr1660 shift 1234567 5 -> NaN Invalid_operation
+
+
+
 -- Tests for the is_* boolean operations
 precision: 9
 maxExponent: 999
index 50257007e7bc1dffa4593b3a6f4918a4373de202..85fdc21ae67a7ec021af5616423f0d63f5fee810 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -427,8 +427,10 @@ Core and Builtins
 Library
 -------
 
-- Issue #7233: Fix a number of two-argument Decimal methods to make sure
-  that they accept an int or long as the second argument.
+- Issue #7233: Fix a number of two-argument Decimal methods to make
+  sure that they accept an int or long as the second argument.  Also
+  fix buggy handling of large arguments (those with coefficient longer
+  than the current precision) in shift and rotate.
 
 - Issue #4750: Store the basename of the original filename in the gzip FNAME
   header as required by RFC 1952.