doing sin program without (math.h)

my instructor decided to pull a mean joke on us. He want us to program sin with out using the math.h. I understand that I can do this with a series such as: sin = x!/1- x3/3!+ x5/5! etc. ( I think I got that right lol) But I can't figure out how to put this in my program. and to show tolerances of +-.01.

Here is my sin program using math.h:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159265

/************************** MESSAGE FUNCTION *****************************/

void
msg1(void)
{

printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
printf("\n EE 150 \n" );
printf("\n The function of this programis to compute \n" );
printf("\n sin(x) for a given value of x \n" );
printf("\n requiring a certain tolerance on the result \n" );
printf("\n \n" );
printf("\n \n\n\n\n\n\n\n\n" );

}

/* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */

/*************************** MAIN FUNCTION *******************************/

int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, sinx;
char ans= 'y', unit, sum, dummy;

/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
while (ans == 'y'|| ans == 'Y')
{

printf ("\n\n What number do you want to use?\n\n");
scanf ("%lf%c", &param, &dummy);
result= sin(param*PI/180);
printf ("The sin of %lf is %lf\n", param, result);
printf("\n\n Would you like to enter another number? (Y for yes)(N for no)\n\n");
scanf("%c", &ans);

}

printf ("\n\n Thank you for using me to solve sine in radians, GOOD BY!! \n\n");
scanf ("%c", &ans);

return 0;

Where do I begin because Honestly I have looked but could not find a similar issue. the one I did find was just wrong. So once again I ask for your assistance in this matter. thanks in advance. not necessarily looking for u to do it for me but good guidance is needed.
Last edited on
this is what i have so far will work more on it tomorrow and keep everybody posted thanks again.

int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, degrees, gtol, ctol, sinx;
char ans= 'y', unit, sum, dummy;

/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
printf("what is the value of x (in radian)\n");
param = degrees;
printf("%lf radians is equal to %lf degrees \n");
printf("what is the tolerance for the results?\n");
scanf("%lf", gtol);

while(gtol >= ctol)
Here's an example (but it has errors) for cosine: http://www.cplusplus.com/forum/beginner/80317/

Since the above was written by a beginner, you might want to ignore it and write your own code from scratch, but it should give an idea.

Basically, you find the sum of the first several terms of a series.Each term can be derived from the previous one using multiplication and division.

From this reference, you can see the series: http://en.wikipedia.org/wiki/Taylor_series#List_of_Maclaurin_series_of_some_common_functions

sin(x) = x - x^3/3! + x^5/5! ...

where x is in radians.

As long as x is small, the terms fairly quickly become smaller.
The sign of each term alternates, plus, minus, plus, minus ....
numerator is
x
x*x*x
x*x*x*x*x
x*x*x*x*x*x*x

denominator is a factorial, that is
1
1*2*3
1*2*3*4*5
1*2*3*4*5*6*7

etc

so you find each term from the previous one, rather than doing the whole thing from scratch each time.

first term = x
Next term = first term * (-1) * (x*x) / (2 * 3)
Next term = 2nd term * (-1) * (x*x) / (4 * 5)
and so on.

The only real problem is knowing when to stop. You can take a fairly pragmatic approach here, simply use a fixed number of terms to begin with. Or you could compare the size of the latest term with the total so far, to see whether it is too small to matter (since a float or double has a limited precision).

Hope this helps a bit, if you need more help, just ask.
Last edited on
can I have the formula for calculating cos?
Sorry! forgot my best friend, GOOGLE.
Last edited on
this helps a lot. and google is my best friend when so happened to type in the perfect words. but sometimes google can be a little off. lol
this what a I have now. The compiler says I have undefined u, t, s, and I sworn that I did but maybe I did the wrong way. well here is the program.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159265

/************************** MESSAGE FUNCTION *****************************/

void
msg1(void)
{

printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
printf("\n EE 150 \n" );
printf("\n Here you should put a message that the user");
printf("\n can read to know what the program is about" );
printf("\n and what to do to use it. \n\n\n\n\n\n\n\n" );

}

/* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */

/*************************** MAIN FUNCTION *******************************/

int fact (int x), (float_x),num, x, input;
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double sin(float_x);
char tol, n;

/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computate ************/
x=input;
printf("Give x=\n");
scanf("%lf", &x);
tol=input;
printf("Give Tol=\n");
scanf("%lf", &tol);

u=x*(PI/180);
n=1;
t*n=u;
s*n=t*n;
while(1)
{
n=n+1;
t*n= -((u^2)*t(n-1)) / ((2*(n)-1)*(2*(n)-2));
s*n=s(n-1)+t(n);
if(abs(t(n))<tol);
{

printf("sin(x) at x=%d is %f',x,s(n)");

}

}
return 0;
}

please help with what I need to remove or add to make this work. constructive hints would be nice I am actually trying to learn this, lol. since it appears that my class is self taught.
Hi ! Please use code tags. U'll always get faster/friendlier/better help that way ;)

If the compiler says that u, t, s are undefined then you have to go through your code to see if there is a line that explicitly says what each of those are, like one of:
 ``123456`` ``````int u; char u; double u; float u; string u; //...etc ``````

Also, this line is very messy and inconsistent:
`int fact (int x), (float_x),num, x, input; //imo declare each one explicitly on `//different lines for now so that it's clear what u meant

This line has incorrect syntax:
`printf("sin(x) at x=%d is %f',x,s(n)");`

The "s*n" is incorrect too because the left of the = is the value that the calculation on the right side is being stored into:
`s*n=s(n-1)+t(n);`
Undefined code will do undefined things...
Last edited on
Thank you. for the reply , I know my questions might sound stupid I am just really trying to get my head wrapped around this.
 I know my questions might sound stupid

No such thing here. It takes time to get everything straight ;)
Thanks for all the help, and special thanks to the guys that wrote the tutorials. I finally got it to work the exact way I wanted. here it is :

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159265

/************************** MESSAGE FUNCTION *****************************/

void
msg1(void)
{

printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
printf("\n EE 150 \n" );
printf("\n The function of this programis to compute \n" );
printf("\n sin(x) for a given value of x \n" );
printf("\n requiring a certain tolerance on the result \n" );
printf("\n \n" );
printf("\n \n\n\n\n\n\n\n\n" );

}

/* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */

/*************************** MAIN FUNCTION *******************************/

int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/
double rad, atolerence, btolerence, newterm, oldterm, num, den;
int d, count, c, n, x;
char ans;
char headerStr[] = ("\n Finding sin(x) as the sum of the series x - x^3/3! + x^5/5! - x^7/7! ...\
n and using the tolerence inpputed by the user to deside when to stop the loop running the series.");
/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations *******/

ans = 'y';
while( ans == 'y' || ans == 'Y')
{
printf("What would you like the value of x (in radians) to be.\n");

printf("What is your desired tolerance for the results given?\n");
scanf(" %lf", &atolerence);
d = (-1);
count = 0;
oldterm = 0;
n = 1;

while(btolerence >= atolerence)
{

d = d*(-1);
den = 1;

for (c = 1; c <= n; c++)
{
den = den*c;
}
printf("%lf \n",den);
newterm = oldterm + ((d*num)/(den));
btolerence = fabs(oldterm-newterm);
n = n + 2;
oldterm = newterm;
count = count+1;

printf("btolerence is %lf \n",btolerence);

printf("The value for test %i is %lf \n", count, oldterm);

printf("and the tolerance is %lf \n", btolerence);
}
printf("btolerence is %lf \n",btolerence);

printf("It took %i tests of the Taylor Series to reach the\n", count);

printf("tolerance of %lf.\n\n", atolerence);

printf("The value of the sine of %lf rads found through the\n", rad);

printf("Taylor Series is %lf.\n\n", oldterm);

printf("Would you like to find another sine value? \n");
printf("Y or y for yes \n");
scanf(" %c", &ans);

}
return 0;
}

/***************************************************************************/

I am very proud of this program. : ) thanks again for all the help.
There is one problem: why are you using math.h in your code ????????????????????

You can't use fabs() function ....
We were ok'd to use the math function for everything but the sine. for sine we had to use the Taylor series.
Still, you could write your own version of fabs() in a couple of lines, if you wanted to keep things "pure".
. So far I had been lucky and actually happy that it works. lol but for some reason the instructor decided to throw a curve ball. He wants it to read positive and negative numbers. It took me for ever to get it to read positive numbers. I have read a couple of tutorials and found that I could use a long int. for abs or some how use fabs again to make this happen. How could I implement absolute value in my code with out changing to much.

Now as for the previous comment, would I be able to make it read negative numbers like this also.

Here is the current code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159265

/************************** MESSAGE FUNCTION *****************************/

void
msg1(void)
{

printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
printf("\n EE 150 \n" );
printf("\n The function of this program is to compute \n" );
printf("\n sin(x) for a given value of x \n" );
printf("\n requiring a certain tolerance on the result \n" );
printf("\n \n" );
printf("\n \n\n\n\n\n\n\n\n" );

}

/* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */

/*************************** MAIN FUNCTION *******************************/

int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/
double rad, atolerence, btolerence, newterm, oldterm, num, den;
long int abs(long int d);
int count, c, n, d;
char ans;
char headerStr[] = ("\n Finding sin(x) as the sum of the series x - x^3/3! + x^5/5! - x^7/7! ...\
n and using the tolerance inputed by the user to decide when to stop the loop running the series.\n\n\n\n\n");
/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations *******/

ans = 'y';
while( ans == 'y' || ans == 'Y')
{
printf("What would you like the value of x (in radians) to be.\n\n");

printf("What is your desired tolerence for the results given?\n\n");
scanf(" %lf", &atolerence);
d = (-1);
count = 0;
oldterm = 0;
n = 1;

while(btolerence >= atolerence)
{

d = d*(-1);
den = 1;

for (c = 1; c <= n; c++)
{
den = den*c;
}
printf("%lf \n",den);
newterm = oldterm + ((d*num)/(den));
btolerence = fabs(oldterm-newterm);
n = n + 2;
oldterm = newterm;
count = count+1;

printf("btolerence is %lf \n\n\n",btolerence);

printf("The value for test %i is %lf \n\n\n", count, oldterm);

printf("and the tolerence is %lf \n\n\n", btolerence);
}
printf("btolerence is %lf \n\n\n",btolerence);

printf("tolerence of %lf.\n\n\n", atolerence);

printf("The value of the sine of %lf rads found through the\n\n\n", rad);

printf("Taylor Series is %lf.\n\n\n", oldterm);

printf("Would you like to find another sine value in (radians)? \n\n");
printf("Y or y for yes \n\n");
scanf(" %c", &ans);

}
return 0;
}
Last edited on
anybody anything, not looking for direct answers. but a little help.
 He wants it to read positive and negative numbers.

I assume you mean you want to calculate the sine of negative as well as positive angles.

That's probably just a one line change.
current code: `while(btolerence >= atolerence)`
new code: `while(fabs(btolerence) >= fabs(atolerence))`
Last edited on
thanks that showed me what I needed . I had tried fabs there earlier but didn't use ()