PDA

View Full Version : What's wrong with this program?

hoangluong
Jul 25, 2009, 06:43 AM
Hi, this program (a programming project in King's book) is for calculating the mathematical constant e as an infinite series e=1 + 1/1! + 1/2! +... + 1/n!, where n is entered by the user and the terms continue to be added until the current term is less than epsilon, which is entered by the user. I don't know where it goes wrong involving the if statement. Could you please help? Thanks in advance.

#include<stdio.h>

int main(void)
{
float d, f, e, epsilon;
int n, i;

printf("Enter an integer: ");
scanf("%d", &n);
printf("Enter an epsilon: ");
scanf("%f", &epsilon);

f=1.0f;
e=1.0f;
epsilon=0.0f;
i=1;

for(i=1; i<=n; i++) {
f=f*i;
d=1.0f/f;
if(d>=epsilon)
e+=d;
}
printf("The approximate value of e for n = %d is: %0.8f.\n", n, e);

return 0;
}

lee1210
Jul 25, 2009, 07:14 AM
Epsilon is always 0, so the if condition is a tautology until f gets so big that the floating point approximation of 1.0/f is 0.

Also, you're not dividing by f!, just f. Also, never use floats for anything. By the time you run into the very few exceptions you'll know it.

-Lee

hoangluong
Jul 25, 2009, 07:24 AM
Epsilon is always 0, so the if condition is a tautology until f gets so big that the floating point approximation of 1.0/f is 0.

Also, you're not dividing by f!, just f. Also, never use floats for anything. By the time you run into the very few exceptions you'll know it.

-Lee

Thanks a lot for your help.