Selaa lähdekoodia

Fix pop-back when no overflow occurs in multiplication.

Samuel Jaffe 8 vuotta sitten
vanhempi
commit
4ab111f25f
2 muutettua tiedostoa jossa 6 lisäystä ja 1 poistoa
  1. 1 1
      src/biginteger.cpp
  2. 5 0
      test/biginteger.t.h

+ 1 - 1
src/biginteger.cpp

@@ -193,7 +193,7 @@ namespace detail {
         rval[i+1] += overflow;
         rval[i+1] += overflow;
       }
       }
     }
     }
-    if (rval[ubnd] == 0) { rval.pop_back(); }
+    while (rval.back() == 0 && rval.size() > 1) { rval.pop_back(); }
     return rval;
     return rval;
   }
   }
 }
 }

+ 5 - 0
test/biginteger.t.h

@@ -90,4 +90,9 @@ public:
     TS_ASSERT_EQUALS((big*big).to_string(),
     TS_ASSERT_EQUALS((big*big).to_string(),
                      "999999996000000005999999996000000001");
                      "999999996000000005999999996000000001");
   }
   }
+  
+  void testMultiplyNoOverflow() {
+    math::biginteger bi{1000};
+    TS_ASSERT_EQUALS((bi*bi).to_string(), "1000000");
+  }
 };
 };