I am new to programming, and today I was just playing around coding trying to use functions, and I made a simple piece of code that worked on the math problem that I had in one of my classes. he basically takes the formula (number of bacteria) * 2 ^ hours and calculates this.
My problem is that when I get a really large amount, it does not return correctly, I always get -2147483648 back after a certain size of the number. I guess this has something to do with overflow, but I'm not 100% sure how this works. I am trying to figure out how to get the actual numbers I'm looking for after I hit this overflow. So what can I do to deal with overflow?
At first I just set everything to int, but after some reading I thought that maybe everything will change until a long time, it can help me, but itβs not so, if it is terrible for me, and I will let you know! In addition, the number of tests I use is 1500 and 24, which always returns a higher number.
Here is the code Thank you!
#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; }
source share