/* ---------------------------------------------------------------- */ /* This is an example showing the effect of losing significant */ /* digits. This program computes sin(x) using an alternating */ /* infinite series. */ /* ---------------------------------------------------------------- */ #include #include #include #define EPSILON 1.0e-8 float MySin(float x) { float sinx = 0.0; /* the sum of the series */ float term = x; /* a term of the series */ float sign = 1.0; /* sign has values 1, -1, 1, ... */ float denom = 1.0; /* the denominator of a term */ while (fabs(term) >= EPSILON) { sinx = sinx + sign*term; term = term*x/(++denom); term = term*x/(++denom); sign = -sign; } return sinx; } void main(void) { float x; float ans1, ans2; printf(" x\t\tMySin\t\tsin(x)\n"); printf("---\t\t-----\t\t------\n"); for (x = -30.0; x <= 30.0; x += 5.0) { ans1 = MySin(x); ans2 = sin(x); printf("%f\t%f\t%f\n", x, ans1, ans2); } }