浏览代码

I think that this properly handles the various multiplication cases... but I really ought to mathematically prove it (with test cases for enough possible combos).

Samuel Jaffe 8 年之前
父节点
当前提交
ee7077654f
共有 1 个文件被更改,包括 7 次插入0 次删除
  1. 7 0
      src/bigdecimal.cpp

+ 7 - 0
src/bigdecimal.cpp

@@ -167,6 +167,13 @@ bigdecimal & math::operator*=(bigdecimal & rhs, bigdecimal const & lhs) {
     rhs.is_negative = is_neg;
   } else {
     detail::multiply(rhs.data, lhs.data, offset);
+    int32_t const steps = add_scale(rhs.scale_), nsteps = add_scale(new_scale);
+    int32_t const off1 = (steps <= 0 ? 0 : 1), off2 = (steps < 0 ? 0 : 1);
+    if (steps - off1 > nsteps) {
+      rhs.data.erase(rhs.data.begin(), rhs.data.begin() + steps - nsteps - off1);
+    } else if (steps + off2 < nsteps) {
+      rhs.data.insert(rhs.data.begin(), size_t(nsteps-steps-off2), 0);
+    }
     rhs.scale_ = new_scale; // TODO: more steps in certain cases
     return rhs;
   }