Subtraction underflow???

Mar 7, 2022 at 11:45pm
Hello all,

I am writing a class called bigInts that deals with bigInts (dynamic arrays of shorts up to 40 digits) and I am having some trouble overloading the operators. I was able to get the addition one working with correct overflow, but subtraction is a different thing altogether. I was just wondering if anyone has any ideas on how to deal with subtraction underflow, I have an idea but since I am only a few months into my c++ journey nothing has been able to work yet.
Mar 8, 2022 at 12:01am
Bigint subtraction works the same way you do it on a piece of paper.

    12 - 57

    5 - 12-(12 - 5)-7

Is that what you are asking about?
Mar 8, 2022 at 12:22am
not really Duthomhas, since it is an array of shorts, if i get the result -2, i have to adjust the next index by something. For example

obj1 = {7,6,5,4,3,2,1}
obj2 = {7,8,9,1,2,3,4}

obj1 - obj2 = {0,-2,-4,3,1,-1,-3} when the result should be 7654231 - 7891234 = 236913
Mar 8, 2022 at 4:34am
7654231 - 7891234 = 236913 

7654231 - 7891234 = -237003 and the way to get this is to use a carryover flag.


Set carryover to 0, and change as required as the right-to-left calculation proceeds through the array

7 6 5 4 3 2 1
7 8 9 1 2 3 4 subtract
--------------
9 7 6 2 9 9 7 with the final carryover flag value 1
--------------

Now, because the flag is 1, that result is the one's complement of the answer
1 0 0 0 0 0 0 0
0 9 7 6 2 9 9 7 subtract
----------------
0 0 2 3 7 0 0 3 which is -ve because the flag is 1
----------------

Mar 8, 2022 at 4:34am
Sorry I can’t craft a more directed answer for you ATM. Have you looked through this?:
http://www.cplusplus.com/forum/lounge/32041/2/#msg173887
Mar 8, 2022 at 4:38am
Big integer Addition and Subtraction are easy, Multiplication is not too bad but Division is a challenge.

I now see from your other posts and the advice given that the concept of a carry should be no surprise, especially if you had tried the exercise on paper, which is fairly early learning in slightly advanced arithmetic.
Last edited on Mar 8, 2022 at 8:45am
Topic archived. No new replies allowed.