Euler's Number Loop

closed account (owCkSL3A)
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
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 !!
closed account (owCkSL3A)
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
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
closed account (owCkSL3A)
I tried that with the code above and it is still doing the same thing. Any suggestions?
With factorial *= i; , i is initially zero, so factorial will always be zero. Sorry I didn't notice that earlier :+)
closed account (owCkSL3A)
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
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.
closed account (owCkSL3A)
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;
}



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.