cosinus function

Nov 24, 2013 at 11:32pm
Hi all,

I've been learning c++ this weekend because I have a university exam next thursday and I've missed all my classes up to now, I had some really important stuff going on... I'm trying to do an exercise where I need to calculate the cosinus of x with some precision that I can chose, here's what I've done. I write that on codepad.org and now I seem to have some issues but I really don't understand why... I must use this formula :
cos(x)=∑(i=0 to inf) ((-1)^i * x^2i) / (2i)!
It seems ok to me but I'm new to this language so I may not be able to see the problems as you could !


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
#include <iostream>
#include <cmath>
using namespace std;

double facto(int k){
	int n(1);
	while(n<k+1){
		n = n * (n+1);
		n++;
	}
	return n;
}


double sp(double x, int u){
	double y;
	int i(0);
	int w (0);
	while (i<u+1){
		y = ((pow(-1,i))*(pow(x,2i)))/(facto (2i)) + w;
  //it tells me "error: call of overloaded 'pow(int, int&)' is ambiguous"
  //for the line above
		w = y;
		i++;
	}
	return w;
}  //"error: imaginary constants are a GCC extension" for this line...

int main()
{
	double n;
        n= 2.0;  // the n and j values are just random testing values
	int j;   // i'll add some cin>> later when the problems are solved...
                 //n is for the cos(n) and j is the last element of the sum

        j=10;
        cout<<"cos("<< n << ") is about "<< sp(n,j)<<endl;
    return 0;
}


Thanks a lot for helping :)

I've read on tons of forums to find out what my problems were but couldn't find out by myself :(

Billy
Nov 25, 2013 at 12:17am
1
2
3
4
5
6
7
8
double facto(int k){
	int n(1);
	while(n<k+1){
		n = n * (n+1);
		n++;
	}
	return n;
}


You should really trace the logic in this function. Is facto somehow a better name than factorial?

What is 2i supposed to be in your sp function? (And couldn't you have come up with a more descriptive name for the function?)
Last edited on Nov 25, 2013 at 12:17am
Nov 25, 2013 at 12:30am
Hi cire,

Thank you for your answer,

Well, facto was faster to write than factorial ^^ and sp is for "somme partielle" in french, ie partial sum.

I don't know what the 2i stands for, it was a part of the sum in the formula given for the cosinus, i suppose that it is right but i think the problem doesn't come from that. If you have any idea as to what the problems (the 2 that the compiler highlighted) could be, i'll try right away.

Is my code anyhow hard to unserstand ?

Thanks!

Billy
Nov 25, 2013 at 12:48am
Your factorial function isn't working the way you think it is. You cannot use the same variable (n) for two different things (a counter, the resulting products).

Also remember that 0! is 1.


When you write 2i that is not a valid way to indicate multiplication. Further, the GCC has an extension that makes "2i" an imaginary number (apparently). If you want 2 times i, you must write it out as 2*i.

One other issue is choice of variable names. I think you are getting lost with all the little variables in there. Try to name them things that are a little longer and a lot more descriptive. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double cosine( double x, int iterations )
{
	double result;  // your 'w'
	double temp;    // your 'y'
	...
}

int main()
{
	double x;  // might as well -- it isn't the same 'x' as above, 
	           // but it is being use for the same thing
	int iterations = 10;

	cout << "Calculate cosine(x), x = ? ";
	cin >> x;

	cout << "cos("<< x << ") is about " << cosine(x,iterations) << endl;

	return 0;
}

BTW, in English cosinus is cosine.

Hope this helps.
Nov 25, 2013 at 12:30pm
Hi Duoas,

I corrected what you said about the 2*i (it's very disappointing as i had done this mistake 2 or 3 times already this weekend :(

I still get
"in function 'double sp(double, int)':
Line 20: error: call of overloaded 'pow(int, int)' is ambiguous
compilation terminated due to -Wfatal-errors.".

Ok, while i'm writing i just understood (feels good). It is ambiguous because my variable x is a double and it is supposed to be an int in the pow function ?


I don't get what you mean by "Your factorial function isn't working the way you think it is".

Thanks for your help :)

Billy
Nov 25, 2013 at 1:32pm
I don't get what you mean by "Your factorial function isn't working the way you think it is".

http://ideone.com/l4UJ0h
Nov 25, 2013 at 1:33pm
Hi again,

Following my previous message, i decided to write my own power function and put it in my code. Good news is that my code now works :D bad news is that the result is wrong...

Here's the 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
#include <iostream>
#include <cmath>
using namespace std;



double power(double a, double b){
double i(1.0);
double c(1);
while(i<b+1)
{
 c = c* a;
i++;
}
return c;

}

double facto(int k){
	int n(1);
	while(n<k+1){
		n = n * (n+1);
		n++;
	}
	return n;
}


double sp(double x, int u){
	double y;
	int i(0);
	double w (0);
	while (i<u+1){
		y = ((power(-1,i))*(power(x,2*i)))/(facto (2*i)) + w;
		w = y;
		i++;
	}
	return w;
}  

int main()
{
	double n;
        n= 2.0;
	int j; 

        j=10;
        cout<<"cos("<< n << ") is about "<< sp(n,j)<<endl;
    return 0;
}


According to you, is it a failure in my code or a failure in my formula ?
cos(x)=∑(i=0 to inf) ((-1)^i * x^2i) / (2i)!

As the results go so high, i think there is indeed a problem in the factorial formula.

Billy
Nov 25, 2013 at 8:39pm
That's a standard Taylor series function to compute cosine. The problem isn't with the formula.

Don't ignore the advice you have already been given.

factorial

To compute factorial, you need to compute two things:

    1
The series 1, 2, 3, ..., n-1, n
    2
The running product of the terms of that series

Your factorial uses the same variable ('n') for both of those things.

So, suppose you are calculating the factorial of 3. You start with n=1.

k=3,n=1:

n = n * (n+1) = 1 * 2 = 2
n++ = n = 3

n<k+1 = 3<3+1 = 3<4 = true, so:

k=3,n=3
n = n * (n+1) = 3 * 4 = 12
n++ = n = 13

n<k+1 = 13<3+1 = 13<4 = false, so you're done.

Do you see where the problem is? You are trying to use n for two different things.

Try thinking the other way. You are given a value, k. Start with n = 1 and multiply it by k. Decrement k by one and repeat. Stop when k == 1.

That way we get our sequence variable:

    1
k, k-1, k-2, ..., 2, 1
    2
n is product of terms of sequence K

Hope this helps.
Nov 25, 2013 at 8:52pm
Hi Duoas,

Thank you for your help, it's nice :)

Hum, I think I get it, but why couldn't I write that ?

1
2
3
4
5
6
7
8
int factorial(int n){
int i(1);      
int u(1);
while ( i<=n){
u = i*u;
i++;
}
}


It seems ok to me, I've just tried it at the same time and this one seems to work. Is it ok now as the result isn't the variable that i put in ?

Thanks :)

Billy
Nov 25, 2013 at 9:15pm
That's perfect. Don't forget to return u. (between lines 7 and 8)
Last edited on Nov 25, 2013 at 9:16pm
Nov 25, 2013 at 9:17pm
Great !

Thanks a lot Duoas :D
Topic archived. No new replies allowed.