Join 132,377 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,147 people online right now. Registration is fast and FREE... Join Now!
void main() //Main function starts here { float sum,n,fact; float i; printf("\n Enter the value of n: "); scanf("%f",&n); for(i=0;i<5000;i++) { fact=fac(i); sum=sum+(pow(n,i)/fact); } printf("\n e^%f=%e",n,sum); getch(); }
Wierd. If I run the program on turbo c++ 3.0 (Ive got access only to this compiler at the moment). Tc++ closes and I return to windows.
Oops... I think I'm not responsible...
But here's the weirdest part. The program actually RAN SUCCESSFULLY the first few times I ran it. But suddenly it started working erroneously while I was trying to alter the limit of i to get precise values. Initially, I thought that it was due to the huge value of i. But then I figured out that it didn't work altogether.
My problem is a bit different. First it was showing some weird values as the output, something like #IND08e+002. Now, when I run the program, it asks for the value of n and does nothing. After I hit the Enter key a few times, the window closes. One more thing I found out just now. Before the window closes, I do get some output for a fraction of a second but it doesn't stay there long enough for me to see what exactly it is. But I had a glance of something starting with e^5 (my input for n was 5). So if I could somehow find a way to make it stay there, the program would be solved. I tried using getch() but it doesn't work.
The reason is that you aren't realising the limitation of your algorithm. The actual formula requires iteration till infinity. Here you have chosen the iteration count as 5000. Now, at a point depending on whether you are using a 16-bit or 32-bit compiler, the factorial function will fail for larger numbers.
factorial(5000) is totally out of bounds. And because of this, the result may up as negative and hence you get wierd results. (If you want to know why it becomes negative, check my Blog on </dic>)
So the solution to this is to limit your iteration count to 10 or 20. That way you'll get the answer, but it'll be less accurate...But it's better than getting some wierd values.
void main() //Main function starts here { float sum,n; long int fact; float i; printf("\n Enter the value of n: "); scanf("%f",&n); for(i=0;i<10;i++) { fact=fac(i); sum=sum+(pow(n,i)/fact); } printf("\n e^%f=%e",n,sum); getch(); }
void main() //Main function starts here { float sum,n; long int fact; float i; printf("\n Enter the value of n: "); scanf("%f",&n); for(i=0;i<10;i++) { fact=fac(i); sum=sum+(pow(n,i)/fact); } printf("\n e^%f=%e",n,sum); getch(); }
__int64 is a 64-bit variable that occupies 8 bytes (which means...more range) See how I've reduced the iteration count to 10.
Calculating e^2 gives a respectable approximation with this algorithm.
b2c, I think you've forgotten to declare i as int instead of float. Stepping aside, isn't there anyway in which I can get a program which gives a somewhat-precise answer? Because, this way, the error of approximation is in powers of ten...
This program gives fairly good values (though I've altered the limit of i to 200 and that's the only change I've made) upto e^29 after which the errors in the powers of 10 are evident. Is there any way in which I can alter this to be even more efficient?
CODE
#include<stdio.h> #include<conio.h> #include<math.h> //To evaluate exponential series float fac(float); //Main function starts here void main() { float sum=0,n=0,fact=0; float i=0; printf("\n Enter the value of n: "); scanf("%f",&n); for(i=1;i<200;i++) { fact=fac(i); sum=sum+(pow(n,i)/fact); } printf("\n e^%f=%e",n,sum); main(); getch(); } //Sub function to calculate factorial float fac(float i) { if(i==0) return(1); else return(i*fac(i-1)); }
void main() //Main function starts here { float sum,n; long int fact; float i; printf("\n Enter the value of n: "); scanf("%f",&n); for(i=0;i<10;i++) { fact=fac(i); sum=sum+(pow(n,i)/fact); } printf("\n e^%f=%e",n,sum); getch(); }
__int64 is a 64-bit variable that occupies 8 bytes (which means...more range) See how I've reduced the iteration count to 10.
Calculating e^2 gives a respectable approximation with this algorithm.
b2c, I think you've forgotten to declare i as int instead of float.
That is interesting aaru_89, I think you should be asking yourself that question. I just pasted your code and compiled it and fixed the errors. I didn't write the code. My Coding Style is different.
Al least you realised where I (I mean you) went wrong