Problem calculating sine of an angle

Hi!
First I want to say thanks to those who helped me start learning C++ one week ago!
Now I am learning, I wrote some Calculating programs, They're all working fine, but in my new program I want to calculate sine of an angle.
As you now sine of an angle is calculated in this way: ("x" is angle in Radian)

x-x3/3!+x5/5!-x7/7!+...

Well, I don't think there is any problem with that, but my program doesn't works; , when I enter the angle nothing happens, like the program didn't get any enterance!
This is my program, please tell me where is wrong? (I think the problem is in the "FOR" loop, because I just learned it and don't know exactly how it works) :

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
#include <iostream>
	using namespace std;
int main ()
{
	long double x; long double sin;signed char sign=-1;unsigned char k=1;long double y;long double enterance;
	cout<<"Please enter the angle you want me to calculate it's sine (in Radian):";
	cin>>x;
	loop:
		sin=x;y=x*x;
		for (k=253;k>0;k=k+2)
		{
sin=sin+(sign*y)/(k*(k+1));sign=((-1)*sign);
		}
		cout<<sin;
		cout<<"\nPlease enter another angle or enter ''123456789'' to exit:";
			cin>>enterance;
		if (enterance=123456789)
		{
			return 0;
		}
		else
		{
			enterance=x;
			goto loop;
		}
}


Note: I am using Visual C++ 2010.
Last edited on
Yes the for loop!.

Hint: initial value of k is 253, terminating condition is loop as long as (k>0), and then k is incremented by 2
But since k is signed char k = 253 actually means k holds -3

If you change the char to unsigned char or int its going to end in a infinite loop; hint: loop at (k=k+2)
I don't see the need for long doubles - an ordinary double should be fine. Declare them on their own line:


1
2
3
4
5
6
double x;
double sin;
signed char sign=-1;
unsigned  k=1;
double y;
double enterance;


It's not usual to have multiple statements on 1 line - separate them:

1
2
sin=x;
y=x*x;


Why does the for loop start at 253?

Another thing - do not ever use goto's they are really really bad & evil.

Use a while loop instead.

I think you would benefit from writing out your method as comments - this will help you to organise your thoughts logically.

Hope all goes well, good to see you having a go - we all look forward to helping out more.

There is really good reference on this site - look at the top left of this page.

Here is the info about for loops.

http://www.cplusplus.com/doc/tutorial/control/


If you are using something, and are unsure how it works, always do a little research to see how to use that function.

There is a lot to learn here:
http://www.cplusplus.com/doc/tutorial/
Last edited on
Thank you both, my question is solved, but things you have learned me created some other questions for me:
1-Codewalker, you said if I change the char to unsigned char or int, it's going to end an infinite loop, why?
2-TheIdeasMan, you said there is no need for long double. I thought long double is more precise than double!!! (and I wanted a more precise answer) Am I wrong? By the way, what is the difference between Double and Long double? (I have read the part "Variables. Data Types" but I didn't mention such a thing about their difference)
3-TheIdeasMan, you were right, the GOTO was an evil and it didn't work correctly, instead I sued a WHILE loop and it's working fine. Why is GOTO not working?
And Finally I put the code I changed, Please check if there is anything that you can learn me more. Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
	using namespace std;
int main ()
{
	double x;double sin;signed char sign;unsigned char k;double y;double enterance;
	cout<<"Please enter the angle you want me to calculate it's sine or enter ''123456789'' to exit (in Radian):\n";
	cin>>x;
while (x!=123456789)
{
		sin=x;y=x*x;sign=(-1);k=3;
		for (k=3;k<252;k=k+2)
		{
sin=sin+(sign*y)/(k*(k-1));
sign=((-1)*sign);
		}
		cout<<sin;
		cout<<"\nPlease enter another angle or enter ''123456789'' to exit:";
			cin>>x;
}
return 0;
}


And about the Documentation, it is my source for learning and I have read the Control Structures before, but as my native language is not English, It was a little confusing for me, But now I know Exactly how FOR loop works.
Thanks again!
(sorry if it is too long)
Last edited on
You have solved the problem of infinite loop already

for (k=253;k>0;k=k+2) would have meant that k would be always greater than zero (in case of unsigned int and unsigned char) and hence the condition k>0 would have been always true; result infinite loop

you have solved that by changing the loop to for (k=3;k<252;k=k+2) now the initial value is 3, you increment that by two every time till k >= 252, works fine

Difference between double and long double: just the size usually
"Type double is a floating type that is larger than or equal to type float, but shorter than or equal to the size of type long double."


One tip, instead of k = k +2 you can have k += 2
If you google C++ long double you will see that long double might not have more precision than a double, and it may only have 18sf as opposed to 16sf. If you have the right combination of compiler, OS etc, you might be lucky to have a long double that is 80 bits or 128bits, as opposed to a double which is 53 bits.

The thing is, why do you need more than 16sf?

Also calculating 252 terms is probably way too much, consider rearranging the while so that it terminates when a term becomes less than some value like 0.000000001 say.

HTH
Is there any reason you're not using the math library?
Thank every one!
well, now it's getting complicated.
what is math library?
Last edited on
If you include math.h you're including a library that already includes a sine function.

Therefore, you don't need to write your own using a Taylor series. It works in radians but if that bothers you then you can easily write a rad to deg conversion function (there might even already be one).

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <math.h>

int main( int argc, char **argv )
{
   double value = 1.57;  // Approximately 90 degrees
   std::cout << "Value: " << value << "\nSine of value " << sin( value ) << std::endl;
   return 0;
}
Last edited on
I think the purpose of the assignment is for the OP to write their own sine function, and not to trivially use the math library.

One thing in the math library, that might not be considered as cheating, is the predefined constant for pi and e, for example. Pi is M_PI, e is M_E, which you can use directly. You might need this if using Microsoft Visual C++:

#define _USE_MATH_DEFINES

There are a lot of other constants as well.

Edit: The other thing to check out is the boost library.

HTH
Last edited on
Topic archived. No new replies allowed.