浏览代码

Fix rescaling bug to force lossyness.

Samuel Jaffe 8 年之前
父节点
当前提交
ac7e0d0a4d
共有 2 个文件被更改,包括 10 次插入0 次删除
  1. 5 0
      src/bigdecimal.cpp
  2. 5 0
      test/bigdecimal.t.h

+ 5 - 0
src/bigdecimal.cpp

@@ -85,6 +85,11 @@ void bigdecimal::rescale(int32_t nscale) {
   } else if (steps < nsteps) {
     data.insert(data.begin(), size_t(nsteps-steps), 0);
   }
+  auto const idx = (scale_ - nscale) % SEG_DIGITS;
+  if (scale_ > nscale && nscale < 0 && idx) {
+    data.back() /= detail::powers[idx];
+    data.back() *= detail::powers[idx];
+  }
   scale_ = nscale;
 }
 

+ 5 - 0
test/bigdecimal.t.h

@@ -45,6 +45,11 @@ public:
     TS_ASSERT_EQUALS(dec.to_string(), "100.00");
   }
   
+  void testNegativeScaleLosesLowerDigits() {
+    math::bigdecimal dec("123", -2);
+    TS_ASSERT_EQUALS(dec.to_string(), "100");
+  }
+  
   void testConstructWithScaleEqualsWithout() {
     math::bigdecimal scl(100, -2);
     TS_ASSERT_EQUALS(scl.to_string(), "100");