Euler's Number Loop
Mar 30, 2016 at 2:11am UTC
I have to find euler's number to whatever the user's desired accuracy is (.001,.0001....) i cant figure out why my code is returning inf as the answer for everything. I have looked over my code many times and hand wrote a few loops but i cant find any issues. Im sure its something small but i could use some help. thanks.
the seris i am using is
1+ 1/1 + 1/1*2 + 1/ 1*2*3 and so on .....
Updated code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int fact(int n);
int main()
{
double ans;
double acc;
double e = 0;
double e2 = 1;
double diff;
int x = 0;
cin >> acc;
do
{
e = 1 + 1 / fact(x);
diff = abs(e - e2);
e2 = e;
x++;
} while (diff > acc);
ans = e;
cout << ans << endl;
system("pause" );
return 0;
}
int fact(int n)
{
int i;
int factorial;
for (i = 0; i <= n; i++)
{
if (i == 0)
factorial = 1;
else
factorial *= i;
}
return factorial;
}
Last edited on Mar 30, 2016 at 2:40am UTC
Mar 30, 2016 at 2:26am UTC
Hi,
Your function returns 0.0 so on line 24, that is the value being used, hence the inf. Instead of modifying
a
, just return the factorial value.
49 50 51 52 53
double i;
double n = a;
double factorial;
for (i = 0; i <= n; i++)
Doubles are not represented exactly, so one shouldn't use them in a for loop. And using them in equality comparisons won't always work either.
Why do you need a double for a factorial anyway? Just use
std::size_t
, and in main use
static_cast <double >(a)
to cast it to double.
Line 58 can be written
factorial *= i;
Good Luck !!
Mar 30, 2016 at 2:38am UTC
I changed the factorial function to int values since the doubles could be causing some issues.
Now every accuracy is being displayed with an answer of 2 or 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int fact(int n);
int main()
{
double ans;
double acc;
double e = 0;
double e2 = 1;
double diff;
int x = 0;
cin >> acc;
do
{
e = 1 + 1 / fact(x);
diff = abs(e - e2);
e2 = e;
x++;
} while (diff > acc);
ans = e;
cout << ans << endl;
system("pause" );
return 0;
}
int fact(int n)
{
int i;
int factorial;
for (i = 0; i <= n; i++)
{
if (i == 0)
factorial = 1;
else
factorial *= i;
}
return factorial;
}
Last edited on Mar 30, 2016 at 2:45am UTC
Mar 30, 2016 at 2:52am UTC
Now every accuracy is being displayed with an answer of 2 or 1
TheIdeasMan wrote:Just use std::size_t, and in main use static_cast<double>(a) to cast it to double.
You didn't cast the result, so now it does integer division :+)
Edit:
You can force the result to be double by always putting numbers before and after the decimal point:
e = 1.0 + 1.0 / fact(x);
This will do an implicit cast.
Last edited on Mar 30, 2016 at 2:54am UTC
Mar 30, 2016 at 3:24am UTC
I tried that with the code above and it is still doing the same thing. Any suggestions?
Mar 30, 2016 at 3:39am UTC
With factorial *= i;
, i is initially zero, so factorial will always be zero. Sorry I didn't notice that earlier :+)
Mar 30, 2016 at 3:57am UTC
I have made both of thoes changes and am still getting errors.
Could it possibly be an issue within my formula for calculating e?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int fact(int n);
int main()
{
double ans;
double acc;
double e = 0;
double e2 = 1;
double diff;
int x = 1;
cin >> acc;
do
{
e = 1.0 + 1.0 / fact(x);
diff = abs(e - e2);
e2 = e;
x++;
} while (diff > acc);
ans = e;
cout << ans << endl;
system("pause" );
return 0;
}
int fact(int n)
{
int i;
int factorial;
for (i = 1; i <= n; i++)
{
if (i == 0)
factorial = 1;
else
factorial *= i;
}
return factorial;
}
Last edited on Mar 30, 2016 at 3:57am UTC
Mar 30, 2016 at 4:36am UTC
Compilation, with warnings turned on:
In function 'int main()':
54:18: warning: 'factorial' may be used uninitialized in this function [-Wmaybe-uninitialized]
7:6: note: 'factorial' was declared here In function 'int fact(int)':
57:9: warning: 'factorial' may be used uninitialized in this function [-Wmaybe-uninitialized]
Always initialise variables to something.
Mar 30, 2016 at 5:04am UTC
To simplify things i decided to take the function to find the factorial and do the same thing in the formula itself.
this is my code
now i am just trying to get the math correct. i do belive that the factorial is correct and that i just need to correct a few small mistakes in the math. is there anything you see that i may need to correct?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
double fact(double & a);
void main()
{
double acc;
double dif;
double i=1;
double x = 1;
double e;
double e2=1;
cin >> acc;
do
{
i *= x;
x++;
e = 1 + 1 / i;
dif = abs(e - e2);
cout << endl << e << endl;
} while (dif > acc);
cout << e;
system("pause" );
return ;
}
Mar 30, 2016 at 6:15am UTC
Hi,
Using your previous code, some poor man's debugging:
1 2 3 4 5 6 7 8 9 10
do
{
e = 1.0 + 1.0 / fact(x);
cout << "e = " << e << endl;
diff = abs(e - e2);
e2 = e;
x++;
} while (diff > acc);
Can you see what's happening there?
Topic archived. No new replies allowed.