# Basic Arithmetic and Comparison Operations

Learn to write code for comparison and basic arithmetic operations using operators.

We'll cover the following

In this lesson, we’ll be using the following operators in our HugeInt class:

• Addition operators (operator+, operator++ (pre- and post-increment), operator+=)

• Subtraction operators (operator- (binary), operator-- (pre- and post-decrement), operator-=)

• Sign operator (operator- (unary))

• Comparison operators (operator<, operator>, operator==, operator<=, and operator>=)

We’ll see how easy and elegant the implementations of these functions can be after implementing the comparison, addition, and subtraction functions.

Here, we have the playground, where we’ll implement various operator functions for performing basic mathematical operations on huge integers. In this context, we have incorporated addition, subtraction, and comparison functions as private helper functions. This design choice enables huge integers to perform signed operations while maintaining the encapsulation of functionality within the abstract data type. As we progress, we’ll expose the necessary functionality through well-defined functions, ensuring that the implementation remains organized and accessible.

5
12
200
2
999999999999999999999999999999
-61969987109750917095790
The arithmetic and comparison operations

## Addition-based operations using operator+

In the previous lessons, we implemented add() and sub() functions along with isLess() for addition, subtraction, and comparison without taking into account the signs of the numbers.

In this section, we’ll employ operator+ to add two numbers while taking the sign of the two numbers into account.

The addition of huge integers (with signs) has two cases:

• Case 1: We have two numbers with the same sign, for example:

- 1 2 3 4- 3 4 5 5or + 1 2 3 4+ 3 4 5 5

For the above examples, the result can be easily achieved by adding the two numbers and taking the sign of any of the numbers, giving -12690 and +12690 as the results for the first and second examples, respectively.

Note: If both operands have the same sign, the addition result will also have the same sign.

• Case 2: We have two numbers with different signs, for example:

+ 1 2 3 4- 3 4 5or - 1 2 3 4+ 3 4 5

In this case, the value of the result is achieved by subtraction of the two numbers, i.e., 889, and the sign of the greater (in terms of value) of the two numbers will be taken, i.e., +889 for the first example and -889 for the second example.

Note: For addition, if both operands have different signs, we subtract the smaller number from the larger one and pick the sign of the larger number.

This is exactly what we’ll implement through code. Let’s look at the implementation:

HugeInt HugeInt::operator+(const HugeInt& I2)const{    if(this->isNeg == I2.isNeg)    {        if(this->isGreater(I2))        {            HugeInt R = this->add(I2);            R.isNeg = this->isNeg;            return R;        }        else        {            HugeInt R = I2.add(*this);            R.isNeg = this->isNeg;            return R;        }    }    else    {        if(this->isGreater(I2))        {            HugeInt R = this->sub(I2);            R.isNeg = this->isNeg;            return R;        }        else        {            HugeInt R = I2.sub(*this);            R.isNeg = this->isNeg;            return R;        }    }}
• Lines 3–17: The code first checks whether both HugeInt objects have the same sign. If they do, it checks which number is greater. If the first number is greater, it adds the first number to the second number. Otherwise, it adds the second number to the first number. Since both operands have the same sign, the sign of the result R is set to this->isNeg.

• Lines 18–31: If both HugeInt objects have different signs, then the code checks which number is greater. If the first number is greater, it subtracts the second number from the first number. Otherwise, it subtracts the first number from the second number. Since the operands have different signs, the sign of the result R is set according to the larger number.

## Exercise: operator++ (pre/post-increment) and operator+=

Using operator+(), implement operator++(int and operator++(), and write a code for operator+=() in your implementation.

Hint: All of these are 2–3 line functions.

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

## The working of operator-

To implement the subtraction operator, operator-, using the addition operator operator+ as a subroutine helper, we can leverage the concept that subtracting a number $b$ from $a$ is equivalent to adding the negation of $b$ to $a$. In other words, $a - b$ is the same as $a + (-b)$. When we apply this idea, it allows us to reuse the already implemented addition operator to perform subtraction.

In order to support this subtraction implementation, we first need to overload the unary operator operator-(). The purpose of this unary operator is to return a copy of the same huge integer object with its sign negated. This is necessary to obtain the negation of a number for the subtraction operation.

Let’s look at the complete implementation:

// Unary operator-HugeInt HugeInt::operator-()const{    HugeInt I = *this; // Create a copy of the current object    I.isNeg = !I.isNeg;  // Negate the sign attribute of the copy    return I; // Return the negated copy}// Binary operator-HugeInt HugeInt::operator-(const HugeInt& I2)const{    HugeInt T = -I2; // Obtain the negation of I2    return *this + T; // Add the negated value of I2 to the current object}

Here are the descriptions of the two functions:

• HugeInt operator-() const: This function overloads the unary minus operator - and creates a negated copy of the current object. It first creates a copy of the current object *this and stores it in a new HugeInt object I. Then, it negates the sign attribute of the copy I by flipping the value of its isNeg flag using the logical negation operator !. Finally, it returns the negated copy I as a new HugeInt object representing the negation of the current object.

• HugeInt operator-(const HugeInt& I2) const: This function overloads the subtraction operator - and takes a constant reference to another HugeInt object I2 as the operand to be subtracted. It first obtains the negation of I2 by using the unary minus operator - to create a temporary HugeInt object T. Then, it performs addition by adding the negated value of I2 (T) to the current object *this using the addition operator +. The result is returned as a new HugeInt object that represents the subtraction of I2 from the current object.

Your task is to write a code for the unary operator operator-() and binary operator operator-() in your implementation.

## Exercise: operator-– (pre/post-decrement) and operator-=

Using operator-(), implement operator--(int) and operator--() , and write the code for operator-=() in your implementation.

Note: All of these are 2–3 line functions.

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

## The working of operator<

The return type of operator< is bool. If the first number is less than the second number, true is returned, and if it’s not, false is returned. Let’s discuss the implementation approach:

• If the first number is negative and the second number is positive, the operator returns true.

• If the first number is positive and the second number is negative, the operator returns false.

• If both numbers are positive, we use isLess() to compare and return the appropriate result.

• If both numbers are negative, we use isGreater() to compare and return the appropriate result.

Here is how it is implemented:

bool HugeInt::operator<(const HugeInt& I2)const{    if (this->isNeg == false && I2.isNeg == true)    {        return false;    }    else if (this->isNeg == true && I2.isNeg == false)    {        return true;    }    else if (this->isNeg == false && I2.isNeg == false)    {        return (*this).isLess(I2);    }    else        return (*this).isGreater(I2);}

## Exercise: implementing >,  >=, <=, ==, and !=
Your task is to write the code for operator()>, operator()<=, operator()>= , and operator()==, != in your implementation.
Note: You don’t need to reimplement these functions. You can use the functionality of operator< to implement operator>. Similarly, operator== can be implemented using operator< and operator>. And obviously, once you have operator<, operator>, and operator==, we can easily write one-line functions to write <= and >=. For the != operator, we can use == as a helper function.