On Wed, 02 Feb 2000, Rohit Sharma wrote:
>Hey gang ,
>
>    I have a c program to calculate sin of a no using exp series (DSP
>assigmment :)).It compile properly and then dumps core. When I use gdb to
>analyse the core Gdb dumps core!!!.

On my system, it didn't dump core, ran well, but generated wrong output.  (sin pi/2 = 
a very large
number > 10^6). Heres a corrected version with more terms in the series.  
-----cut here----- 
/* Working code */

#include <stdio.h>


void main (void)
{
float mysin(float x);
float no,finalval;
printf("Enter no\n");
scanf("%f",&no);
/* Small degree to rad convertor */
no = no*3.14159/180; /* Because I hate entering rad values :)*/
finalval=mysin(no);
printf("answer is %f\n\n",finalval);
}

float mysin(float x)
{
float term,value;
int i, sign_int;
term = x;
i = 1;
value = term; /* Set equal to first term of sine series */



/* Originalcode */
/* while (i<=10)
{
value=((-1)*term*x*x)/((i+1)*(i+2));
sum=sum+value;
i=i+2;
term=value;
}*/
/* x-x^3/3!+x^5/5!...*/




sign_int = -1; /* Can be eliminated, but KISS , -1 because we have already considered 
first term*/

for(i=1;i<21;i+=2)
        {
        term = term*x*x/((i+1)*(i+2));  /* Get next element of series */
        value += sign_int*term;         /* Add to the value of sine */
        sign_int = -1*sign_int;         /* Switch sign */
        }
return(value);
}
-----cut here-----
 /* Comment your programs */

Devdas Bhagat

To subscribe / unsubscribe goto the site www.ilug-bom.org ., click on the mailing list 
button and fill the appropriate information 
and submit. For any other queries contact the ML maintener

Reply via email to