...

/

Multiplication and Division Operators

Multiplication and Division Operators

Learn to write the code for multiplication and division using operators.

Now, we’ll build on the previous lesson and implement the most important and nontrivial functions, i.e., operator* , operator/, operator%, and their assignment versions *=, /=, and %=.

Your implementation

In this lesson, we’ll guide you through the implementation of three essential operators. By building upon the concepts and implementations covered in the previous lessons, you’ll now be able to harness the benefits of the groundwork laid so far. Get ready to unlock the full potential of our huge integer class!

5
12
200
2
999999999999999999999999999999
-61969987109750917095790
Multiplication and division

So far we have implemented several operators such as the < , <=, > , >= , == , and != comparison operators, + , - , += , -= , ++, and -- arithmetic operators, and the << basic input output operator for printing huge integers. Now, let’s move toward more complex arithmetic operators like multiplication * and division /.

Multiplication and division of huge integers

Let’s say we want to multiply two numbers, 5050 and 6060. The first and simple idea to multiply these two numbers is by addition. For example, 3×A3 \times A is A+A+AA+A+A. In the same way, if we add 5050 to itself 6060 times, then we can get the result of 50×6050\times 60.

We can implement this idea as follows:

Press + to interact
HugeInt HugeInt::operator*(const HugeInt & b)const
{
HugeInt R = HugeInt::Zero();
for (HugeInt a=HugeInt::One(); a <= b; ++a)
{
R += *this;
}
return R;
}
  • Line 3: The code first initializes a new HugeInt object R with a value of zero. This object will hold the result of the multiplication.

  • Line 4: The loop initialization statement, HugeInt a = HugeInt::One(), creates a new HugeInt object with a value of 1, which is used as the loop index variable. The a <= I2 loop condition checks whether the current value of a is less than or equal to the value of I2. The a++ loop update statement increments the value of a by 1 after each iteration.

  • Lines 4–7: The multiplication result is maintained using a simple repeated addition algorithm. A for loop is used to iterate from the 1 value to the value of I2. At each iteration, the current object is added to R.

Test the code above in your implementation.

Power of object-oriented design

Look at the power of the class we’ve already built. Imagine the challenges we would have faced without implementing these essential operators. Take a closer look at the multiplication algorithm, where we seamlessly utilize operators such as operator<=, operator++ (pre-increment), operator+=, and operator= (which is called inside operator+= and operator++ too). This exemplifies the true power of object-oriented programming, as it hides the intricate details and provides a clean and elegant syntax.

Our huge integer class makes complex calculations simpler and more accessible, allowing us to utilize our coding potential and achieve a truly elegant code.

Implementing operator/ and operator%

How can we perform division (operator/) using subtraction? Write the code in your implementation above. Look at the image below explaining the process.

If you’re stuck, click the “Show Solution” button.

Press + to interact
canvasAnimation-image
1 / 6

If you’re stuck, click the “Show Solution” button.

How can we perform the modulus (operator%) using subtraction? Write the code above in your implementation. Refer again to the image above explaining the process.

If you’re stuck, click the “Show Solution” button.

Challenge: The serious impracticality

There is a ...