When I run the following program:
#include <stdio.h>
#include <math.h>
int main()
{
double sum, increase;
long amount, j;
printf("sum = ");
scanf("%lf", &sum);
printf("increase = ");
scanf("%lf", &increase);
printf("amount = ");
scanf("%ld", &amount);
for (j = 1; j <= amount; j++)
{
sum += increase;
}
printf("%lf\n", sum);
return 0;
}
I get the following answer for these values:
MacBook:c benjamin$ ./test
sum = 234.4
increase = 0.000001
amount = 198038851
432.438851
MacBook:c benjamin$ ./test
sum = 234.4
increase = 0.000001
amount = 198038852
432.438851
MacBook:c benjamin$ ./test
sum = 234.4
increase = 0.000001
amount = 198038853
432.438852
where I increased the variable "quantity" by 1 in each case.
- In the first case, the summation gives what I expect.
- In the second case, this unexpectedly gives the same meaning.
- In the third case, he goes to the summation.
Why is this happening?
Although the code is not very useful, I just wrote this part. I really wanted to use it in a larger program.
Thank!
source
share