# Thread: A function called sine()

1. ## A function called sine()

I'm trying to write a function called sine() that calculates and returns the value of the Taylor series.
This is what I have so far:

I posted wrong code, I updated below.

2. You don't even use the d parameter in your function.
And you haven't shown your power and factorial functions.

3. Well I was pretty sure they were right. I thought the problem would be with my sine function. Here is my power and factorial.

Code:
```long power(int val,int pow)
{
int i;
int ans = 1;
if(pow==0){

return(1);}

if(pow==1){

return(val);}

else
{
for(i=1;i<=pow;i++)
ans = ans*val;
return(ans);
}
}```
Code:
```double factorial (double n)
{
int i;
double fact = 1;

if(n > 1)
for(i=2; i<=n;i++)
fact *= i;
return(fact);
}```

4. From what I see you're pretty close. Couple problems.
1) Did you ever use d in the function body? Don't you sorta need to.
2) Did you ever declare x and i? Do you need both of them?
3) Is there a connection between 1) and 2)?

5. Yeah, I just realized that. The x is where the d should be. I'll change that. But i still get the same results.

6. Good eye, javaeyes! Your code won't even compile since you're using i instead of k i sine.

7. Code:
```double sine(double d,int list)
{
int k;

double q=0.0;

for(k=0;k<list;k++){
q+=(power(-1,list)*power(d,2*list+1))/(factorial(2*list+1));

}

return(q);

}```

8. It does compile, it's just that I changed the code before posting here for some reason.

9. Are you ever using k?

10. Well I thought i needed k to calculate the sum. It would keep adding the values until it reached list -1.

11. The value has to be a function of r,radians, which goes from 0 to PI/2.
Code:
```double sine(double d,int list)
{
int k;

double q=0.0;

for(k=0;k<list;k++){
q+=(power(-1,list)*power(d,2*list+1))/(factorial(2*list+1));

}

return(q);

}```
Code:
```#include <stdio.h>
#include <math.h>

#define SIZE 4
#define R 0.01
double sine(double x,int terms);

void main(void)

FILE *file_1;

double x[]={3,5,7,20}; //list of numbers for taylor series
int i;
double t,mathsin,tf;

file_1= fopen("taylor.txt","w");
for(r=0.0;r<=(M_PI/2);r=r+R){

fprintf(file_1,"%lf\t",r);

mathsin= sin(r);  // comparing results with math function of sin

fprintf(file_1,"%lf\t",mathsin);

for(a=0;a<SIZE;a++){
tf=sine(r,x[a]);

fprintf(file_1,"%lf\t",tf); //print results of sine function
}

fprintf(file_1,"\n");```
And this is what i get when i print it into a .txt file.
The values of the sine() function have to be closer to the values of the sin function as the term values for the taylor function increase.

12. And this is what i get when i print it into a .txt file.
The values of the sine() function have to be closer to the values of the sin function as the term values for the taylor function increase.

Code:
```x    sin(x)    sine(x,3)    sine(x,5)    sine(x,7)    sine(x,20)
0.000000    0.000000    0.000000    0.000000    0.000000    0.000000
0.010000    0.010000    0.000000    0.000000    0.000000    0.000000
0.020000    0.019999    0.000000    0.000000    0.000000    0.000000
0.030000    0.029996    0.000000    0.000000    0.000000    0.000000
0.040000    0.039989    0.000000    0.000000    0.000000    0.000000
0.050000    0.049979    0.000000    0.000000    0.000000    0.000000
0.060000    0.059964    0.000000    0.000000    0.000000    0.000000
0.070000    0.069943    0.000000    0.000000    0.000000    0.000000
0.080000    0.079915    0.000000    0.000000    0.000000    0.000000
0.090000    0.089879    0.000000    0.000000    0.000000    0.000000
0.100000    0.099833    0.000000    0.000000    0.000000    0.000000
0.110000    0.109778    0.000000    0.000000    0.000000    0.000000
0.120000    0.119712    0.000000    0.000000    0.000000    0.000000
0.130000    0.129634    0.000000    0.000000    0.000000    0.000000
0.140000    0.139543    0.000000    0.000000    0.000000    0.000000
0.150000    0.149438    0.000000    0.000000    0.000000    0.000000
0.160000    0.159318    0.000000    0.000000    0.000000    0.000000
0.170000    0.169182    0.000000    0.000000    0.000000    0.000000
0.180000    0.179030    0.000000    0.000000    0.000000    0.000000
0.190000    0.188859    0.000000    0.000000    0.000000    0.000000
0.200000    0.198669    0.000000    0.000000    0.000000    0.000000
0.210000    0.208460    0.000000    0.000000    0.000000    0.000000
0.220000    0.218230    0.000000    0.000000    0.000000    0.000000
0.230000    0.227978    0.000000    0.000000    0.000000    0.000000
0.240000    0.237703    0.000000    0.000000    0.000000    0.000000
0.250000    0.247404    0.000000    0.000000    0.000000    0.000000
0.260000    0.257081    0.000000    0.000000    0.000000    0.000000
0.270000    0.266731    0.000000    0.000000    0.000000    0.000000
0.280000    0.276356    0.000000    0.000000    0.000000    0.000000
0.290000    0.285952    0.000000    0.000000    0.000000    0.000000
0.300000    0.295520    0.000000    0.000000    0.000000    0.000000
0.310000    0.305059    0.000000    0.000000    0.000000    0.000000
0.320000    0.314567    0.000000    0.000000    0.000000    0.000000
0.330000    0.324043    0.000000    0.000000    0.000000    0.000000
0.340000    0.333487    0.000000    0.000000    0.000000    0.000000
0.350000    0.342898    0.000000    0.000000    0.000000    0.000000
0.360000    0.352274    0.000000    0.000000    0.000000    0.000000
0.370000    0.361615    0.000000    0.000000    0.000000    0.000000
0.380000    0.370920    0.000000    0.000000    0.000000    0.000000
0.390000    0.380188    0.000000    0.000000    0.000000    0.000000
0.400000    0.389418    0.000000    0.000000    0.000000    0.000000
0.410000    0.398609    0.000000    0.000000    0.000000    0.000000
0.420000    0.407760    0.000000    0.000000    0.000000    0.000000
0.430000    0.416871    0.000000    0.000000    0.000000    0.000000
0.440000    0.425939    0.000000    0.000000    0.000000    0.000000
0.450000    0.434966    0.000000    0.000000    0.000000    0.000000
0.460000    0.443948    0.000000    0.000000    0.000000    0.000000
0.470000    0.452886    0.000000    0.000000    0.000000    0.000000
0.480000    0.461779    0.000000    0.000000    0.000000    0.000000
0.490000    0.470626    0.000000    0.000000    0.000000    0.000000
0.500000    0.479426    0.000000    0.000000    0.000000    0.000000
0.510000    0.488177    0.000000    0.000000    0.000000    0.000000
0.520000    0.496880    0.000000    0.000000    0.000000    0.000000
0.530000    0.505533    0.000000    0.000000    0.000000    0.000000
0.540000    0.514136    0.000000    0.000000    0.000000    0.000000
0.550000    0.522687    0.000000    0.000000    0.000000    0.000000
0.560000    0.531186    0.000000    0.000000    0.000000    0.000000
0.570000    0.539632    0.000000    0.000000    0.000000    0.000000
0.580000    0.548024    0.000000    0.000000    0.000000    0.000000
0.590000    0.556361    0.000000    0.000000    0.000000    0.000000
0.600000    0.564642    0.000000    0.000000    0.000000    0.000000
0.610000    0.572867    0.000000    0.000000    0.000000    0.000000
0.620000    0.581035    0.000000    0.000000    0.000000    0.000000
0.630000    0.589145    0.000000    0.000000    0.000000    0.000000
0.640000    0.597195    0.000000    0.000000    0.000000    0.000000
0.650000    0.605186    0.000000    0.000000    0.000000    0.000000
0.660000    0.613117    0.000000    0.000000    0.000000    0.000000
0.670000    0.620986    0.000000    0.000000    0.000000    0.000000
0.680000    0.628793    0.000000    0.000000    0.000000    0.000000
0.690000    0.636537    0.000000    0.000000    0.000000    0.000000
0.700000    0.644218    0.000000    0.000000    0.000000    0.000000
0.710000    0.651834    0.000000    0.000000    0.000000    0.000000
0.720000    0.659385    0.000000    0.000000    0.000000    0.000000
0.730000    0.666870    0.000000    0.000000    0.000000    0.000000
0.740000    0.674288    0.000000    0.000000    0.000000    0.000000
0.750000    0.681639    0.000000    0.000000    0.000000    0.000000
0.760000    0.688921    0.000000    0.000000    0.000000    0.000000
0.770000    0.696135    0.000000    0.000000    0.000000    0.000000
0.780000    0.703279    0.000000    0.000000    0.000000    0.000000
0.790000    0.710353    0.000000    0.000000    0.000000    0.000000
0.800000    0.717356    0.000000    0.000000    0.000000    0.000000
0.810000    0.724287    0.000000    0.000000    0.000000    0.000000
0.820000    0.731146    0.000000    0.000000    0.000000    0.000000
0.830000    0.737931    0.000000    0.000000    0.000000    0.000000
0.840000    0.744643    0.000000    0.000000    0.000000    0.000000
0.850000    0.751280    0.000000    0.000000    0.000000    0.000000
0.860000    0.757843    0.000000    0.000000    0.000000    0.000000
0.870000    0.764329    0.000000    0.000000    0.000000    0.000000
0.880000    0.770739    0.000000    0.000000    0.000000    0.000000
0.890000    0.777072    0.000000    0.000000    0.000000    0.000000
0.900000    0.783327    0.000000    0.000000    0.000000    0.000000
0.910000    0.789504    0.000000    0.000000    0.000000    0.000000
0.920000    0.795602    0.000000    0.000000    0.000000    0.000000
0.930000    0.801620    0.000000    0.000000    0.000000    0.000000
0.940000    0.807558    0.000000    0.000000    0.000000    0.000000
0.950000    0.813416    0.000000    0.000000    0.000000    0.000000
0.960000    0.819192    0.000000    0.000000    0.000000    0.000000
0.970000    0.824886    0.000000    0.000000    0.000000    0.000000
0.980000    0.830497    0.000000    0.000000    0.000000    0.000000
0.990000    0.836026    0.000000    0.000000    0.000000    0.000000
1.000000    0.841471    0.841667    0.841471    0.841471    0.841471
1.010000    0.846832    0.841667    0.841471    0.841471    0.841471
1.020000    0.852108    0.841667    0.841471    0.841471    0.841471
1.030000    0.857299    0.841667    0.841471    0.841471    0.841471
1.040000    0.862404    0.841667    0.841471    0.841471    0.841471
1.050000    0.867423    0.841667    0.841471    0.841471    0.841471
1.060000    0.872355    0.841667    0.841471    0.841471    0.841471
1.070000    0.877201    0.841667    0.841471    0.841471    0.841471
1.080000    0.881958    0.841667    0.841471    0.841471    0.841471
1.090000    0.886627    0.841667    0.841471    0.841471    0.841471
1.100000    0.891207    0.841667    0.841471    0.841471    0.841471
1.110000    0.895699    0.841667    0.841471    0.841471    0.841471
1.120000    0.900100    0.841667    0.841471    0.841471    0.841471
1.130000    0.904412    0.841667    0.841471    0.841471    0.841471
1.140000    0.908633    0.841667    0.841471    0.841471    0.841471
1.150000    0.912764    0.841667    0.841471    0.841471    0.841471
1.160000    0.916803    0.841667    0.841471    0.841471    0.841471
1.170000    0.920751    0.841667    0.841471    0.841471    0.841471
1.180000    0.924606    0.841667    0.841471    0.841471    0.841471
1.190000    0.928369    0.841667    0.841471    0.841471    0.841471
1.200000    0.932039    0.841667    0.841471    0.841471    0.841471
1.210000    0.935616    0.841667    0.841471    0.841471    0.841471
1.220000    0.939099    0.841667    0.841471    0.841471    0.841471
1.230000    0.942489    0.841667    0.841471    0.841471    0.841471
1.240000    0.945784    0.841667    0.841471    0.841471    0.841471
1.250000    0.948985    0.841667    0.841471    0.841471    0.841471
1.260000    0.952090    0.841667    0.841471    0.841471    0.841471
1.270000    0.955101    0.841667    0.841471    0.841471    0.841471
1.280000    0.958016    0.841667    0.841471    0.841471    0.841471
1.290000    0.960835    0.841667    0.841471    0.841471    0.841471
1.300000    0.963558    0.841667    0.841471    0.841471    0.841471
1.310000    0.966185    0.841667    0.841471    0.841471    0.841471
1.320000    0.968715    0.841667    0.841471    0.841471    0.841471
1.330000    0.971148    0.841667    0.841471    0.841471    0.841471
1.340000    0.973485    0.841667    0.841471    0.841471    0.841471
1.350000    0.975723    0.841667    0.841471    0.841471    0.841471
1.360000    0.977865    0.841667    0.841471    0.841471    0.841471
1.370000    0.979908    0.841667    0.841471    0.841471    0.841471
1.380000    0.981854    0.841667    0.841471    0.841471    0.841471
1.390000    0.983701    0.841667    0.841471    0.841471    0.841471
1.400000    0.985450    0.841667    0.841471    0.841471    0.841471
1.410000    0.987100    0.841667    0.841471    0.841471    0.841471
1.420000    0.988652    0.841667    0.841471    0.841471    0.841471
1.430000    0.990105    0.841667    0.841471    0.841471    0.841471
1.440000    0.991458    0.841667    0.841471    0.841471    0.841471
1.450000    0.992713    0.841667    0.841471    0.841471    0.841471
1.460000    0.993868    0.841667    0.841471    0.841471    0.841471
1.470000    0.994924    0.841667    0.841471    0.841471    0.841471
1.480000    0.995881    0.841667    0.841471    0.841471    0.841471
1.490000    0.996738    0.841667    0.841471    0.841471    0.841471
1.500000    0.997495    0.841667    0.841471    0.841471    0.841471
1.510000    0.998152    0.841667    0.841471    0.841471    0.841471
1.520000    0.998710    0.841667    0.841471    0.841471    0.841471
1.530000    0.999168    0.841667    0.841471    0.841471    0.841471
1.540000    0.999526    0.841667    0.841471    0.841471    0.841471
1.550000    0.999784    0.841667    0.841471    0.841471    0.841471
1.560000    0.999942    0.841667    0.841471    0.841471    0.841471
1.570000    1.000000    0.841667    0.841471    0.841471    0.841471```

13. Originally Posted by javaeyes
Are you ever using k?
Did you mean like this?

Code:
```double sine(double d,int list)
{
int k;

double q=0.0;

for(k=0;k<list;k++){
q+=(power(-1,k)*power(d,2*k+1))/(factorial(2*k+1));

}

return(q);

}```

14. I tested the power and factorial functions and they both work fine. I think the problem is with the sine function.

15. Your power function can be optimized like the power2 listed below.
the original function took 13+ seconds and for the same input power2 took less than 5 seconds for 90 million calls.
Note: I changed both to use long long instead of int for the result.
Also with smaller values for pow the optimization is not as good.

Since the original function did not detect over flow power2 does not check for overflow.

Code:
```prompt > cat power.test.c
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

long long power(int val,int pow)
{
int i;
long long  ans = 1;
if(pow==0)
{

return(1);
}

if(pow==1)
{

return(val);
}

else
{
for(i=1; i<=pow; i++)
{
ans = ans*val;
}
return(ans);
}
}

long long power2(int val,unsigned int pow)
{
long long  ans = 1;
if(pow==0)
{

return(1);
}

if(pow==1)
{

return(val);
}
#if 0
if(pow == 2)
{
return(val *val);
}
#endif
ans = power2(val, pow/2);
if(pow&1)
{
return(ans * ans *val);
}
else
{
return(ans * ans) ;
}

}
typedef struct results
{
long long ans;
long long ans2;
int  val;
int  pow;

} RESULTS;

static void
print_times(const char *title, struct timeval *end, struct timeval *start)
{
struct timeval diff;
printf("%-30.30s %9ld.%.3ld ", title, (long)end->tv_sec, (long)end->tv_usec/1000);
if(!start)
{
printf("\n");
return;
}
diff.tv_sec = end->tv_sec - start->tv_sec;
if((diff.tv_usec = end->tv_usec - start->tv_usec) < 0)
{
diff.tv_sec--;
diff.tv_usec += 1000000;
}
printf("  %9ld.%.3ld ",  (long)start->tv_sec, (long)start->tv_usec/1000);
printf("  %9ld.%.3ld\n",  (long) diff.tv_sec, (long)diff.tv_usec/1000);

}

int
main(int argc, char **argv, char **envr)
{
RESULTS results[500];
int i;
const int cycles = 180000;
int j;
struct timeval times[3];
/*** verify that both functions return same value for same input ***/
for(i=sizeof(results)/sizeof(results[0]); i--> 0; )
{
results[i].val  = i/64;
results[i].pow  = i%64;
results[i].ans  = power(results[i].val, results[i].pow);
results[i].ans2 = power2(results[i].val, results[i].pow);
if(results[i].ans2 != results[i].ans)
{
fprintf(stderr, "functions to not compute same value for power(%d,%d) = %lld  power2(%d,%d) = %lld\n",

results[i].val, results[i].pow,results[i].ans,
results[i].val, results[i].pow,results[i].ans2);
}
}
/*** run timing test ***/
gettimeofday(times,NULL);
for(j=0; j<cycles; j++)
{
for(i=sizeof(results)/sizeof(results[0]); i--> 0; )
{
long long ans;
results[i].val  = i/100;
ans  = power(results[i].val, results[i].pow);
}
}
gettimeofday(times+1,NULL);
for(j=0; j<cycles; j++)
{
for(i=sizeof(results)/sizeof(results[0]); i--> 0; )
{
long long ans;
results[i].val  = i/100;
ans  = power2(results[i].val, results[i].pow);
}
}
gettimeofday(times+2,NULL);
print_times("start", times+0, NULL);
print_times("power done/power2 start", times+1, times+0);
print_times("power2 done",  times+2, times+1);
return(0);
}
prompt > gcc -Wall power.test.c -o pt
prompt > ./pt
start                          1333758647.940
power done/power2 start        1333758661.689   1333758647.940          13.749
power2 done                    1333758667.437   1333758661.689           5.747
prompt >```

Popular pages Recent additions