#include #include double sin0(double x); double sin1(double x); static const double PI = 3.14159265358979323846; static const double EPS = 1e-9; int main() { while (true) { double x; printf("x: "); if (scanf("%lf", &x) < 1) break; printf( "sin0(x) = %.8lf, sin1(x) = %.8lf\n", sin0(x), sin1(x) ); printf("Test: standard sin(x) = %.8lf\n", sin(x)); } return 0; } // Compute sin(x) as a sum of Taylor series double sin0(double x) { // sin(x) = x - x^3 / 3! + x^5 / 5! - x^7 / 7! + ... double s = 0.; double r = x;; double n = 1.; while (fabs(r) >= EPS) { s += r; r *= -x*x/((n+1.)*(n+2.)); n += 2.; } return s; } // Reduce an argument of sin to the interval [-PI, PI] // before computing of the Taylor series, // using the periodicity of sin double sin1(double x) { x = fmod(x, 2.*PI); if (x > PI) x -= 2.*PI; else if (x < -PI) x += 2.*PI; return sin0(x); }