-1

This was the third or fourth time that encounter this problem and it just annoys me because it makes no sense for me as beginner programmer.

So for my class I had to make a program(function inside a program) that will find largest and smallest digit in a number and return the digit count as a variable. I hate to use references but thats not the point right now.

My program works well, as expected nothing bad is happening until I try to send the largest long long int number to the function.

Because I don't need a negative number inside my function I used an if statment to check if number is <0 and if it is then it should be multiplied by -1 to get a positive(I know that I could use the abs function but same result).

When I pass the largest long long int which is −9,223,372,036,854,775,807 program returns that the number is smaller than 0 but also returns a negative number, not a positive one, but when I test the −9,223,372,036,854,775,807 which is that number -1 everything works again.

So my question is what is actually happening here, why is the n*=-1 part ignored, is it because the number is using all of the memory or ?

int Digits(long long int n,int &c_min,int &c_max){

    c_min=9;
    c_max=0;

    if(n<0){
        n*=-1;
    }

    int digit;
    int numberOfDigits(0);

    while(n!=0){
        digit=n%10;
        if(digit > c_max) c_max=digit;
        if(digit< c_min) c_min=digit;
        numberOfDigits++;
        n/=10;
    }



    if(numberOfDigits==0){
        c_min=0;
        c_max=0;
        return 1;
    }

    return numberOfDigits;
}

MAIN FUNCTION :

int main ()
{
    int mini,maxi;
    int e = Digits(std::numeric_limits<long long int>::min(), mini, maxi);

    std::cout << e;

    return 0;
}
stiMULAnt
  • 91
  • 4
  • 10
  • The answer to your question is that's simply how two's complement arithmetic works. A Google search will give you all the information you need. But you should also take the following two-part pop quiz: what is `-75 % 10` and `-75 / 10`. After you figure out the answer to this first part, the second part is: can you figure out how to implement your function in a way that works with negative numbers, but does not require that multiplication by -1? – Sam Varshavchik Apr 14 '19 at 17:39
  • _When I pass the largest long long int which is −9,223,372,036,854,775,807_ Do you mean `−9,223,372,036,854,775,808`? – Paul Sanders Apr 14 '19 at 17:41
  • The largest negative number does not have a corresponding positive value so the multiplication by -1 overflows and stays negative. – stark Apr 14 '19 at 17:42
  • Thanks guys for the help It gave me a lot of things to explore I replaceted *=1 with digit=abs(n%10); and it works all fine now, – stiMULAnt Apr 14 '19 at 17:47
  • This is related to how two’s complement arithmetic works. See [this earlier question](https://stackoverflow.com/questions/8917233/) for details. – templatetypedef Apr 14 '19 at 17:52

2 Answers2

1

Imagine that you have 4 bits for your number, which means you can store 2^4 numbers, which is 16. Now, since you want positive and negative numbers, you might want to split it in half and have 8 positive, 8 negative numbers. But you also need zero, which means besides zero, you have 15 numbers. What happens is you have numbers from -8 to positive 7, precisely 16 numbers:

-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7

Now as you can see, you can't have a positive 8 in 4 bits. This is the case here, The largest negative number does not have a corresponding positive value so the multiplication by -1 overflows and stays negative. Overflow actually causes undefined behavior, meaning you need look no further.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

I think this could be the reason. The abs(min()) returns a value that is 1 bigger than max() of a certain type.

For a 32 bit int, min() returns -2^31, while max returns 2^31 - 1 For a 64 bit long long int,

min() returns -2^63 while max() returns 2^63 - 1

So when you do a n = n*-1, it will not hold that number as it overflows.

So the min value you can test is min()+1

BTW, it is higher efficiency to do it this way

n = -n
instead of 
n = n * -1
Tony
  • 632
  • 1
  • 4
  • 18