Question about sine function.

Oct 19, 2015 at 5:39am
I was trying to make my own sin function and I'm running into some weird values for my value of sin(x)

If I put in 2.61799 radians for x and do 3 sumations I get 0.485031 which is right I think.

but when I put in 12.2173 radians and do 3 sumations I get -6084.84 which is wrong, it's supposed to be -0.34202014.

I have run it over and it looks like it's doing the math right but I don't understand why I get one that looks right and one that is completely wrong.

here's the site I use to calculate sin(x).
http://www.rapidtables.com/calc/math/Sin_Calculator.htm

Oh also my program is a little racist, I get bored asking normal questions all the time :/

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
59
void Sineequations::getX()
{
	double tempX;
	int tempN;
	char tempDegreeType, repeatCheck = 'Y';

	while (repeatCheck == 'Y' || repeatCheck == 'y') {
		cout << "Sup nigga, just wondering what the value of X might be.\nCould you give it to me?\n";
		cin >> tempX;
		cout << "\nOkay now would that numba be in Radians or Degrees?\nSay R for radians and D for degrees. I ain't good at listening to long words.\n";
		cin >> tempDegreeType;
		cout << "\nOkay so how many sumations ya want me ta do?\n";
		cin >> tempN;

		while (tempN < 1) {
			cout << "\nPlease give a positive number for number of sumations.\n";
			cin >> tempN;
		}

		sineMath(tempX, tempN, tempDegreeType);

		cout << "\nWould you like to try another value for x? Y/N\n";
		cin >> repeatCheck;
	}
	

}

void Sineequations::sineMath(double x, int N, char degree)
{
	const double PI = 3.14159;
	double tempSum = 0, sum = 0, numerator;
	int n = 0, factProduct = 1, factorial;
	if (degree == 'D' || degree == 'd') {
		x = (x*PI) / 180;
		cout << "\nDegree converted to Radians = " << x << endl;
	}

	while (N >= 0) {
		factorial = ((2 * n) + 1);
		while (factorial > 0) {
			factProduct = factorial*factProduct;
			cout <<"F: "<< factorial <<" FP: "<< factProduct << endl;
			factorial--;
		}
		numerator = ((pow(-1, n))*(pow(x, (2 * n + 1))));
		cout << "N: " << N << ", n: " << n << ", factProduct: " << factProduct << " numerator: " << numerator << endl;
		tempSum = (numerator / factProduct);
		cout << "tempSum: " << tempSum << endl;
		n++;
		factProduct = 1;
		sum += tempSum;
		cout << "sum: " << sum << endl;
		N--;
	}

	cout << "Here is the value of sine: " << sum << endl;

}
Last edited on Oct 19, 2015 at 5:42am
Oct 19, 2015 at 8:01am
https://en.wikipedia.org/wiki/Taylor%27s_theorem#Statement_of_the_theorem
see the term h_k(x) (x-a)**k
the approximation is good if x-a is small, that's it, for points near `a'

In your case, a=0 and 12 is quite far.
Oct 20, 2015 at 2:14am
So my code is fine it just doesn't handle big numbers well?
Oct 20, 2015 at 3:39am
Didn't check your code.

To handle big numbers you may use the periodic property http://www.cplusplus.com/reference/cmath/fmod/ and adjust it to the range [-pi/2; pi/2]
Oct 20, 2015 at 4:24am
closed account (48T7M4Gy)
Oh also my program is a little racist, I get bored asking normal questions all the time


Maybe if you didn't waste your time being a big racist, you'd wouldn't write rubbish code. Good luck in a commercial environment.
Last edited on Oct 20, 2015 at 5:56am
Oct 20, 2015 at 6:15pm
I found out what was wrong. I needed to convert large angles into their corresponding angle between 0 and 360.


x = ((int)x%360)*PI / 180);

instead of

x = (x*PI) / 180;
Topic archived. No new replies allowed.