Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with overflow in C

Tags:

c

overflow

I'm pretty new to programming and I was just playing around coding today trying to use functions and I made a simple piece of code that worked a math problem I had in one of my classes. it basically takes the formula (bacteria amount) * 2^hours and calculates that.

My issue is that when I get a really large number it doesn't return correctly, I always get -2147483648 back after a certain size of number. I'm guessing this has something to do with overflow but i'm not 100% sure how that works. What i'm trying to figure out is how to get the actual numbers i'm looking for after I hit this overflow. So what can I do to deal with the overflow?

I initially was only setting everything to int, but after some reading thought maybe changing everything over to long might help me out, but it didn't, if this is a terrible thing for me to do go ahead and let me know! Also the test numbers i'm using are 1500 and 24, that always returns the number above.

Here's the code Thanks!

#include<stdio.h>
#include<math.h>
long bacteria(long b, long h);

int main(void)
{
        long f,g;

        scanf("%ld%ld",&f,&g);
        f = bacteria(f,g);
        printf("%ld\n",f);

return 0;
}

long bacteria(long b,long h)
{
        long d;
        printf("%ld %ld\n",b,h);
        d = b * (pow(2,h));

return d;
}
like image 856
Yosvan10 Avatar asked Oct 09 '14 05:10

Yosvan10


2 Answers

Yes your suspect about overflow is right. C datatype have some range. You need to use some bignum library to handle cases where you need broader range. Also note that pow returns double and not long as you might expect.

If you don't care about precision, you can use double instead of long which serves much broader range.

Live example here

like image 50
Mohit Jain Avatar answered Sep 29 '22 07:09

Mohit Jain


Good Question. I faced same issue some time back used the below example. Check it out if this helps you

http://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial

Code below from the link above.

#include<stdio.h>
int main()
{
    int t;
    int a[200]; //array will have the capacity to store 200 digits.
    int n,i,j,temp,m,x;

    scanf("%d",&t);
    while(t--)
    {
       scanf("%d",&n);
       a[0]=1;  //initializes array with only 1 digit, the digit 1.
       m=1;    // initializes digit counter

       temp = 0; //Initializes carry variable to 0.
       for(i=1;i<=n;i++)
       {
            for(j=0;j<m;j++)
            {
               x = a[j]*i+temp; //x contains the digit by digit product
               a[j]=x%10; //Contains the digit to store in position j
               temp = x/10; //Contains the carry value that will be stored on later indexes
            }
             while(temp>0) //while loop that will store the carry value on array.
             { 
               a[m]=temp%10;
               temp = temp/10;
               m++; // increments digit counter
             }
      }
              for(i=m-1;i>=0;i--) //printing answer
              printf("%d",a[i]);
              printf("\n");
    }
    return 0;
}
like image 40
Vaibhav Avatar answered Sep 29 '22 06:09

Vaibhav