Thread: Need Help In Correcting Degree Values in Homemade Sin, Cos, and Tan functions

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
2

Need Help In Correcting Degree Values in Homemade Sin, Cos, and Tan functions

Hello,

So I made sin, cos, and tan functions in main based on the initial radian angle of 45 radians.

I converted radians into degrees, by dividing the angle by 180 and then multiplying by pi later on.

I then sent this value to their appropriate functions, which take in this angle, and approximate a sin, cos, or tan value given the correct use of the Taylor series for each function.

The thing is, it works in radians, and just checked via my Texas instruments calculator (aka a TI-30), and the sin, cos, and tan values are approximately okay in radians, NOT DEGREES.

I'm pretty sure that in order to go from radians to degrees, you have to divide by 180, and multiply by pi, so I don't understand how this is still giving me a radian value...

All that I could think of are two possiblities:

1. I didn't convert to degrees correctly

or

2. The Taylor series maybe uses radians, or maybe I didn't write the functions correctly.

Here's my code:
Code:
```#include <iostream>
#include <cmath> // This library is used for the sin, tan, and cos functions later

using std::cout;
using std::endl;

/*
3. The trigonometry functions (sin(), cos(), and tan()) in the standard <cmath>

Write three equivalent functions, called sind(), cosd(), and tand(),
which take arguments in degrees.

All arguments and return values should be type double.
*/

// Function Prototype for Sin function:
double sind(double sin_degree_arg);

// Function Prototype for Cos function:
double cosd(double cos_degree_arg);

// Function Prototype for Tan function:
double tand(double tan_degree_arg);

int main()
{

// Instead of making the functions on their own, start making
// them in main and get them working FIRST

double sin_degree_arg;

double cos_degree_arg;

double tan_degree_arg;

double pi;

pi = 3.14159265;

// Goals for Sin function:

// Find out how to convert a radian argument into degrees to use for
// the sin function:

// We're basically converting degree values in radians into degrees
// sin of angle = len of opp side / hyp
// Multiply the angle by pi, and divide by 180 (remember the Unit Circle? :D)

// Always assume hypotenuse is 1

// http://www.mathsisfun.com/sine-cosine-tangent.html

// http://www.homeschoolmath.net/teaching/sine_calculator.php

sin_degree_arg*= pi;

cout << "Degree_arg value = " << sin_degree_arg << endl;

sind(sin_degree_arg);

// Okay, so basically a calculator will take in a radian amount, convert it
// to degrees by multiplying the number by 180, and THEN dividing it by pi

// So divide the degree amount by pi:

// Model equation is as follows, basically its a Taylor Series from Calc 2:

// sinx = x - ((x^3)/3) + ((x^5) / 5) - ((x^7) / 7)

// Use pow() from cmath: pow(number, exponent)

// sin_value = degree_arg - (pow(degree_arg, 3))/(3*2*1) + (pow(degree_arg,5))/(5*4*3*2*1) - (pow(degree_arg,7))/(7*6*5*4*3*2*1);

// Cos:

// Taylor series for cosx = 1 - x^2 / 2! + x^4 / 4!

cos_degree_arg *= pi;

cosd(cos_degree_arg);

// Tan:

// Taylor Series for tan: tan x = x + x^3/3 + 2*x^5/15 + 17*x^7/315

tan_degree_arg *= pi;

tand(tan_degree_arg);

return 0;

}

double sind(double sin_degree_arg)
{
double sin_value;

cout << "Sin Degree Arg received = " << sin_degree_arg << endl;

sin_value = sin_degree_arg - (pow(sin_degree_arg, 3))/(3*2*1) + (pow(sin_degree_arg,5))/(5*4*3*2*1) - (pow(sin_degree_arg,7))/(7*6*5*4*3*2*1) + (pow(sin_degree_arg, 9))/(9*8*7*6*5*4*3*2*1);

cout << "Sin value = " << sin_value << endl;

return sin_value;

}

double cosd(double cos_degree_arg)
{
double cos_value;

cout << "Cos Degree Arg Received = " << cos_degree_arg << endl;

cos_value = 1 - (pow(cos_degree_arg, 2))/(2*1) + (pow(cos_degree_arg, 4))/(4*3*2*1) - (pow(cos_degree_arg, 6))/(6*5*4*3*2*1) + (pow(cos_degree_arg, 8))/(8*7*6*5*4*3*2*1);

cout << "cos_value = " << cos_value << endl;

return cos_value;
}

double tand(double tan_degree_arg)
{
double tan_value;

cout << "Tan Degree Arg Received = " << tan_degree_arg << endl;

// Value is NOT correct --> check math, its probably a matter of
// using parenthesis incorrectly

tan_value = tan_degree_arg + pow(tan_degree_arg,3)/3 + 2*pow(tan_degree_arg,5)/15 + 17*pow(tan_degree_arg,7)/315;

cout << "tan_value = " << tan_value << endl;

return tan_value;
}```
2. Looking at only your first sentence, you do realize that 45 radians is about seven and a quarter full turns?

Code:
```   NB. j.  www.jsoftware.com
divmod =: <.@% , |~
45 divmod TAU
7 1.0177```
3. Taylor series use radians. Every calculus book tells you that and every reference that describes Taylor series should also tell you that. In fact, throughout all three semesters of calculus all angles we worked with were in radians and never in degrees. When you call the library trig functions, you always have to pass them radians. Since most users think in degrees instead of radians, we need to convert the user's input from degrees to radians before we call a trig function.

I converted radians into degrees, by dividing the angle by 180 and then multiplying by pi later on.
You just described how to convert from degrees to radians. Yet your description in the program's comments are correct:
Code:
```	// Okay, so basically a calculator will take in a radian amount, convert it
// to degrees by multiplying the number by 180, and THEN dividing it by pi```
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
2
Ah cool.

So I basically kept the lines that convert degrees to radians, except that I put them in the actual functions that require them.

I also made pi a double variable inside each of the functions, though I could probably just make it global at the top of the code.

And I think it looks like that's it.

I think I might add user input now to specify any degree value instead of just 45 degrees each time.

Anyway, here's my code, thanks a bunch guys!:
Code:
```#include <iostream>
#include <cmath> // This library is used for the sin, tan, and cos functions later

using std::cout;
using std::endl;

/*
3. The trigonometry functions (sin(), cos(), and tan()) in the standard <cmath>

Write three equivalent functions, called sind(), cosd(), and tand(),
which take arguments in degrees.

All arguments and return values should be type double.
*/

// Function Prototype for Sin function:
double sind(double sin_degree_arg);

// Function Prototype for Cos function:
double cosd(double cos_degree_arg);

// Function Prototype for Tan function:
double tand(double tan_degree_arg);

int main()
{

// Instead of making the functions on their own, start making
// them in main and get them working FIRST

double sin_degree_arg;

double cos_degree_arg;

double tan_degree_arg;

sin_degree_arg = 45;

cos_degree_arg = 45;

tan_degree_arg = 45;

// Goals for Sin function:

// Find out how to convert a radian argument into degrees to use for
// the sin function:

// We're basically converting degree values in radians into degrees
// sin of angle = len of opp side / hyp
// Multiply the angle by pi, and divide by 180 (remember the Unit Circle? :D)

// Always assume hypotenuse is 1

// http://www.mathsisfun.com/sine-cosine-tangent.html

// http://www.homeschoolmath.net/teaching/sine_calculator.php

sind(sin_degree_arg);

// Okay, so basically a calculator will take in a radian amount, convert it
// to degrees by multiplying the number by 180, and THEN dividing it by pi

// So divide the degree amount by pi:

// Model equation is as follows, basically its a Taylor Series from Calc 2:

// sinx = x - ((x^3)/3) + ((x^5) / 5) - ((x^7) / 7)

// Use pow() from cmath: pow(number, exponent)

// sin_value = degree_arg - (pow(degree_arg, 3))/(3*2*1) + (pow(degree_arg,5))/(5*4*3*2*1) - (pow(degree_arg,7))/(7*6*5*4*3*2*1);

// Cos:

// Taylor series for cosx = 1 - x^2 / 2! + x^4 / 4!

cosd(cos_degree_arg);

// Tan:

// Taylor Series for tan: tan x = x + x^3/3 + 2*x^5/15 + 17*x^7/315

tand(tan_degree_arg);

return 0;

}

double sind(double sin_degree_arg)
{
double sin_value;

double pi;

pi = 3.14159265;

cout << "Sin Degree Arg received = " << sin_degree_arg << endl;

cout << "Sin value = " << sin_value << endl;

return sin_value;

}

double cosd(double cos_degree_arg)
{
double cos_value;

double pi;

pi = 3.14159265;

cout << "cos_degree_arg received = " << cos_degree_arg << endl;

cout << "cos_value = " << cos_value << endl;

return cos_value;
}

double tand(double tan_degree_arg)
{
double tan_value;

double pi;

pi = 3.14159265;

cout << "tan_degree_arg received = " << tan_degree_arg << endl;

// Value is NOT correct --> check math, its probably a matter of
// using parenthesis incorrectly

cout << "tan_value = " << tan_value << endl;

return tan_value;
}```
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
2
With user input:
Code:
```#include <iostream>
#include <cmath> // This library is used for the sin, tan, and cos functions later

using std::cout;
using std::endl;
using std::cin;

/*
3. The trigonometry functions (sin(), cos(), and tan()) in the standard <cmath>

Write three equivalent functions, called sind(), cosd(), and tand(),
which take arguments in degrees.

All arguments and return values should be type double.
*/

// Function Prototype for Sin function:
double sind(double sin_degree_arg);

// Function Prototype for Cos function:
double cosd(double cos_degree_arg);

// Function Prototype for Tan function:
double tand(double tan_degree_arg);

int main()
{

// Instead of making the functions on their own, start making
// them in main and get them working FIRST

double sin_degree_arg;

double cos_degree_arg;

double tan_degree_arg;

sin_degree_arg = 45;

cos_degree_arg = 45;

tan_degree_arg = 45;

cout << "Please enter a degree value to take the sin() of: ";
cin >> sin_degree_arg;
cout << endl;

cout << "Please enter a degree value to take the cos() of: ";
cin >> cos_degree_arg;
cout << endl;

cout << "Please enter a degree value to take the tan() of: ";
cin >> tan_degree_arg;
cout << endl;

// Goals for Sin function:

// Find out how to convert a radian argument into degrees to use for
// the sin function:

// We're basically converting degree values in radians into degrees
// sin of angle = len of opp side / hyp
// Multiply the angle by pi, and divide by 180 (remember the Unit Circle? :D)

// Always assume hypotenuse is 1

// http://www.mathsisfun.com/sine-cosine-tangent.html

// http://www.homeschoolmath.net/teaching/sine_calculator.php

sind(sin_degree_arg);

// Okay, so basically a calculator will take in a radian amount, convert it
// to degrees by multiplying the number by 180, and THEN dividing it by pi

// So divide the degree amount by pi:

// Model equation is as follows, basically its a Taylor Series from Calc 2:

// sinx = x - ((x^3)/3) + ((x^5) / 5) - ((x^7) / 7)

// Use pow() from cmath: pow(number, exponent)

// sin_value = degree_arg - (pow(degree_arg, 3))/(3*2*1) + (pow(degree_arg,5))/(5*4*3*2*1) - (pow(degree_arg,7))/(7*6*5*4*3*2*1);

// Cos:

// Taylor series for cosx = 1 - x^2 / 2! + x^4 / 4!

cosd(cos_degree_arg);

// Tan:

// Taylor Series for tan: tan x = x + x^3/3 + 2*x^5/15 + 17*x^7/315

tand(tan_degree_arg);

return 0;

}

double sind(double sin_degree_arg)
{
double sin_value;

double pi;

pi = 3.14159265;

cout << "Sin Degree Arg received = " << sin_degree_arg << endl;

cout << "Sin value = " << sin_value << endl;

return sin_value;

}

double cosd(double cos_degree_arg)
{
double cos_value;

double pi;

pi = 3.14159265;

cout << "cos_degree_arg received = " << cos_degree_arg << endl;

cout << "cos_value = " << cos_value << endl;

return cos_value;
}

double tand(double tan_degree_arg)
{
double tan_value;

double pi;

pi = 3.14159265;

cout << "tan_degree_arg received = " << tan_degree_arg << endl;

// Value is NOT correct --> check math, its probably a matter of
// using parenthesis incorrectly

cout << "tan_value = " << tan_value << endl;

return tan_value;
}```
6. Now that you've solved the problem, you should know that while with Taylor series you can get error estimates and arbitrary precision with enough terms, they are otherwise a poor choice. Search here for Donald Murray's post.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
232
Rep Power
2
Originally Posted by b49P23TIvg
Now that you've solved the problem, you should know that while with Taylor series you can get error estimates and arbitrary precision with enough terms, they are otherwise a poor choice. Search here for Donald Murray's post.
Ah, so they use CORDIC (Coordinate Rotation Digital Computer), an algorithm that calculates hyperbolic and trig functions. Cool.

Yeah, I realized through trial and error that I received more decimal points as I continued the Taylor series. It is continuous, so that makes sense.

Thanks a bunch guys!