Reputation: 29
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 obtain the following response 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 have increased the variable 'amount' by 1 in each case.
Why does this happen?
Although the code doesn't seem to be very useful, I have just written the part in question. I actually wanted to use it in a larger program.
Thanks!
Upvotes: 3
Views: 199
Reputation: 726619
This is an issue with the formatting, not the number. If you change printf
to
printf("%.12lf\n", sum);
the results look more along the lines of what you've expected:
sum = increase = amount = 432.438851000198
sum = increase = amount = 432.438852000198
sum = increase = amount = 432.438853000198
The "junk" at the end is due to the limited precision of floating point numbers.
Upvotes: 3