@@ -366,15 +366,22 @@ namespace com::saxbophone::arby {
366366 * @param rhs value to multiply this Uint by
367367 * @returns resulting object after multiplication-assignment
368368 */
369- constexprvector Uint& operator *=(Uint rhs) {
370- // either operand being zero always results in zero
371- if (_digits.size () == 0 or rhs._digits .size () == 0 ) {
372- _digits.clear ();
373- } else {
374- // store a copy of this for use as lhs
375- const Uint lhs = *this ;
376- // reset this to zero
377- _digits.clear ();
369+ constexprvector Uint& operator *=(const Uint& rhs) {
370+ Uint product = *this * rhs; // uses friend *operator
371+ // assign product's digits back to our digits
372+ _digits = product._digits ;
373+ return *this ; // return the result by reference
374+ }
375+ /* *
376+ * @brief Multiplication operator for Uint
377+ * @param lhs,rhs operands for the multiplication
378+ * @returns product of lhs * rhs
379+ */
380+ friend constexprvector Uint operator *(const Uint& lhs, const Uint& rhs) {
381+ // init product to zero
382+ Uint product;
383+ // either operand being zero always results in zero, so only run the algorithm if they're both non-zero
384+ if (lhs._digits .size () != 0 and rhs._digits .size () != 0 ) {
378385 // multiply each digit from lhs with each digit from rhs
379386 for (std::size_t l = 0 ; l < lhs._digits .size (); l++) {
380387 for (std::size_t r = 0 ; r < rhs._digits .size (); r++) {
@@ -386,21 +393,12 @@ namespace com::saxbophone::arby {
386393 std::size_t shift_amount = (lhs._digits .size () - 1 - l) + (rhs._digits .size () - 1 - r);
387394 // add that many trailing zeroes to intermediate's digits
388395 intermediate._digits .insert (intermediate._digits .end (), shift_amount, 0 );
389- // finally, add it to this as an accumulator
390- * this += intermediate;
396+ // finally, add it to lhs as an accumulator
397+ product += intermediate;
391398 }
392399 }
393400 }
394- return *this ; // return the result by reference
395- }
396- /* *
397- * @brief Multiplication operator for Uint
398- * @param lhs,rhs operands for the multiplication
399- * @returns product of lhs * rhs
400- */
401- friend constexprvector Uint operator *(Uint lhs, const Uint& rhs) {
402- lhs *= rhs; // reuse compound assignment
403- return lhs; // return the result by value (uses move constructor)
401+ return product;
404402 }
405403 private: // private helper methods for Uint::divmod()
406404 // function that shifts up rhs to be just big enough to be smaller than lhs
0 commit comments