#include /* 1 3 5 7 9 11 13 15 X X X X X X X X sin(X) = -- - -- + -- - -- + -- - --- + --- - --- + ... 1! 3! 5! 7! 9! 11! 13! 15! */ double power(const double a, const int p) { if (p == 0) return 1; else return a * power(a, p - 1); } double factorial(const int n) // WHY DOUBLE AND NOT INT ????? { if (n == 0) return 1; else return n * factorial(n - 1); } int plusminus(const int i) { const int rem = i % 4; if (rem == 1) return +1; else if (rem == 3) return -1; else return 0; } double sine_approx(const double x, const int last_N) { if (last_N < 0) return 0; const double previous_terms = sine_approx(x, last_N - 1); const double last_term = power(x, last_N) / factorial(last_N) * plusminus(last_N); return previous_terms + last_term; } const double pi = acos(-1.0); double degrees_to_radians(const double a) { return pi * a / 180.0; } void main() { cout << "Last term? "; const int N = read_int(); while (true) { cout << "x? "; const double x = degrees_to_radians( read_double() ); cout << "correct = " << setprecision(16) << sin(x) << "\n"; cout << " approx = " << setprecision(16) << sine_approx(x, N) << "\n"; } }