1

It is taught to us in grade school that you can add (positive) numbers like this:

    5 2 3
    4 5 6
   -------
    5 7 9

But if we change it to 523 + (-456), we cannot use the same algorithm

    5  2  3
   -4 -5 -6  
   ---------
    1 -3 -3 

I know that I can evaluate that to 100 + -(30) + (-3) which gets 67, but in my situation, 17 is the maximum integer, and -17 is the minimum integer (excluding the actual numbers being added). Is there a way to do this where the 523 + 456 also works?

Ank i zle
  • 127
  • 2
    "but in my situation, 17 is the maximum integer, and -17 is the minimum integer (excluding the actual numbers being added)" What does that mean? Where did $17$ come from. Why are the maximum and minimum and why is it a problem. It seems like you have come up with an algorithm that does work for all four cases. I don't think you have an issue. – fleablood Jun 02 '20 at 04:58
  • 17 is the maximum integer because 9 * 2 - 1 = 17. The maximum integer in my case is determined by (the base (10) - 1) * 2 - 1. – Ank i zle Jun 02 '20 at 05:01
  • The maximum integer for what? ANd why is that an issue? Just do what you did. It works. So what if you get $17$ or $-17$. (BTW why isn't the maximum $18$?) – fleablood Jun 02 '20 at 05:14
  • Sorry for the confusion but I am doing this for a programming project, and in my actual case 2 ^ 63 - 1 is the real maximum integer for my project (because of the maximum long long in c++), but for this question I changed the 2 ^ 63 - 1, to 17. – Ank i zle Jun 02 '20 at 05:19
  • 1
    Long subtraction is similar to long addition, but you carry $-1$ instead of $+1$, that's all. for (int i=0,x=0;i<N;++i) {x+=(a[i]-b[i]);c[i]=x%d;x/=d;} – Alexey Burdin Jun 02 '20 at 05:31
  • what are d, x, and c in that loop? – Ank i zle Jun 02 '20 at 05:36
  • 1
    Why don't you actually ask the question you have in mind? Trying to put it in simpler terms leaving us to have to guess what you really want and asking a completely different question doesn't make things easy. – fleablood Jun 02 '20 at 06:35
  • The question I asked was my real question. I was going to take the concept given by an answer and convert that into code that performs the task I assign it. – Ank i zle Jun 02 '20 at 06:40

1 Answers1

1

The reason why long long data type in c++ represents numbers range from $\ -2^{63}\ $ to $\ 2^{63}-1\ $ is because the representation used is $64$-bit twos complement. An analogous representations in decimal would be $n$-digit "tens complement", for some $\ n\ $, which could represent all the numbers in the range $\ -\frac{10^n}{2}\ $ to $\ \frac{10^n}{2}-1\ $, so it's not at all clear (not, at lest, to me) where your "maximum integer" of $17$ comes from, or what it is meant to be the maximum of.

Nevertheless, if you represent decimal numbers using sequences of positive and negative digits from $-9$ to $9$, as you have done, you can convert any representation to any equivalent one without needing to use any numbers outside the range $-9$ to $9$.

In your example, $\ 1\ -3\ -3\ $, for instance, first replace the rightmost digit by $7$ and decrease the next digit to the left by $1$ to get the equivalent representation $\ 1\ -4\ \ 7\ $. You don't even need to get the $7$ by subtracting $3$ from $10$, since you can get it by subtracting one less than $3$, namely $2$, from $9$.

Next, replace the new middle digit, $-4$, with $6$, and decrease the leftmost digit by $1$ to get the equivalent representation $\ 0\ 6\ 7\ $, from which the new leftmost digit $0$ can be deleted to give you $67$.

lonza leggiera
  • 28,646
  • 2
  • 12
  • 33
  • So, for each negative digit, get the complement from 10 and subtract the right digit by 1? – Ank i zle Jun 02 '20 at 08:00
  • Yes, except that it's the digit to the left of the one you complement that you need to decrease by $1$, not the one to its right, That doesn't change the number represented because you've increased the number represented by the first digit by $10$ (or by $10^n$ if it's in the $n^\text{th}$ position from the right, and decreased the number represented by the digit on its left by the same amount. – lonza leggiera Jun 02 '20 at 13:40