add to subtraction

closed account (STR9GNh0)
a~~
Last edited on
Subtraction is really just addition where the second operand is multiplied by -1 before evaluation. You can't simplify it by changing the order of operands as you're doing, though.
If the minuend is greater than the subtrahend you can use complement to 9.
Change every digit of the subtrahend to 9-digit, sum 1 and then sum the numbers.
Example:
42 - 13
13 -> 86 -> 87
42 + 87 = 129
result = 29

10 - 1
10 + 99 = 109

closed account (STR9GNh0)
is there anyone that could guide me through this? i've been at hours and couldn't find the solution to it.

or is there a way for me to invoke a subtraction function from the above addition that i've computed?
Last edited on
closed account (STR9GNh0)
really don't understnad why is my question always remains unsolved.
I am not getting what exactly you want yo achive . please explain clearly .
the original function assumes that the two integer arrays represent the individual digits of arbitrarily large integers.
it then proceeds to add these two large integers together using the addition procedure taught it grade school. rather
than returning the result as a third integer array, it converts it to a human readable string.

op now wants to write a similar function that subtracts the two large integers.

the answer for OP is first to understand exactly how the above function works, then think about how subtraction
is taught in grade school and then implement that. if a - b < 0, then borrow a one from the next column (instead
of carrying) and perform ( 10 + a ) - b.
closed account (STR9GNh0)
sorry, i really have no idea on how to code subtraction thus the question. If only i am as good as you, i would be helping instead of asking.
I gave it to you.

Let A and B be two integers held as arrays of digits. Let Aones and Bones be the ones digit of A and B respectively.
Assume that the function is to compute S = A - B.

If Aones >= Bones, then Sones = Aones - Bones.
If Aones < Bones, then Sones = ( 10 + Aones ) - Bones, and remember to subtract 1 from Atens.

Repeat until all digits are done.

This works only if A > B.
Topic archived. No new replies allowed.