15,614,943 members
1.00/5 (2 votes)
See more:
evaluation of sinx using infinite series

What I have tried:

C
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int terms;
int count=0;
int sum = 0;int x;int n;
printf("enter number of terms");
scanf("%d",&terms);
printf("enter value of x");
scanf("%d",&x);
while(count <= terms){

n = 2*count+1;

sum = sum + ((pow(-1,count))*(pow(x,n)/(fact(n))));
count++;
}
printf("%d",sum);
}
int fact(int f){
int number = 1;
int product = 1;
while(number<=f){

product = number*product;
number++;

}
return product;
}```
Posted
Updated 15-Oct-18 21:55pm
v2
Comments
Mehdi Gholam 14-Oct-18 8:30am
... and what is your question (other than stating your homework)?
[no name] 14-Oct-18 8:46am
Don't use ints. These are real numbers. http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)FloatingPoint.html
Kornfeld Eliyahu Peter 14-Oct-18 16:49pm
Make it an answer - it is after all...
[no name] 15-Oct-18 5:19am
You should modify this post to make your question clear because so far there is no question. We may guess at your problem but there is no point to that.

## Solution 1

Using an infinite series to calculate a function will take a long time. Before doing so, ask yourself:

1. Is the function periodic? If so, reduce the argument to a single period.
2. Is it odd or even (f(x) == -f(-x) or f(x) == f(-x) )? If so, write the odd function as for example x*F(x^2), and the even function as for example G(x^2).
3. Are there any other attributes of the function that can help?

Once you have done that, you will find that only a finite number of elements are required to get the accuracy that you want.

Good luck!

## Solution 2

As suggested, use `double`s instead of `int`. Enjoy:
C
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double fact(unsigned n)
{
double result =1.0;
unsigned int i;
for (i=2; i<=n; ++i )
result *= i;
return result;
}

int main()
{
unsigned terms;
unsigned count=0;
double result = 0.0;
double angle;

printf("enter number of terms ");
scanf("%d",&terms);
printf("enter value of x ");
scanf("%lf",&angle);
while(count <= terms)
{
double sign = count & 1 ? -1.0 : 1.0;
unsigned n = 2*count+1;

result += sign*(pow(angle,(double)n)/fact(n));
count++;
}
double br = sin(angle);
printf("approximation %g, builtin function result %g, difference  %5.2f%%", result, br, fabs(result-br)/br*100.0);
}```

Comments
Daniel Pfeffer 16-Oct-18 4:40am
This will be extremely inaccurate for even small values of x. You should at the very least use Horner's rule in order to evaluate the polynomial. See my solution for other considerations.
CPallini 16-Oct-18 5:34am
Did you actually try it?
Daniel Pfeffer 16-Oct-18 7:10am
1. pow(x, y) _may_ have an optimization for the case of integral y, but that is not guaranteed. Otherwise, it calculates exp(y*log(x)).
2. Even if the optimization exists, you will have between lg(n) and 2*lg(n) multiplications for _each_ power calculation, with all the associated errors and extra calculation.
3. Horner's rule - f(x) = (...((a[n]*x + a[n-1])*x + a[n-2])*x ...) +a[0] uses n multiplications and n additions to perform the calculation, which is both faster and more accurate.
4. A modern C or C++ compiler provides fma(). Using that reduces the calculation to n fma() operations, with a corresponding increase in accuracy.
5. There is also no need to recalculate the factorial at each stage. It may be calculated "for free" by setting a[2*k+1] = +/-1.0/(2*k*(2*k+1)), a[1] = 1.0.
6. Assuming x is even moderately large (say 100.0), your method will overflow well before you reach acceptable accuracy - you will need at least 150 elements (n = 300), and 300! will overflow well before you reach that.
CPallini 16-Oct-18 8:56am
What is 'x' you wrote at point (6)?
By the way, you didn't answer me.
Daniel Pfeffer 16-Oct-18 12:38pm
x is the argument to your sin() calculation.

No, I didn't run the code; merely looking at the way you implemented the power series is enough. Were any of my comments about the code incorrect? :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 130 raddevus 75 CHill60 35 Rick York 30 Richard MacCutchan 30
 OriginalGriff 2,996 Richard MacCutchan 1,266 Graeme_Grant 965 Dave Kreskowiak 649 CHill60 360

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900