#include #include double const pi = 3.1415926535897932385; static double f0(double x){ if (x<0) return f0(-x); if (2.*x>pi) return -f0(x-pi); return 1;} static double fs(double x, int n){ double s = 0; while (n--) if(n&1) s += (n&2?-1:1)*cos(n*x)/n; return 4./pi*s;} static void rp(double x){ printf("%11.6f %11.6f %20.15f\n", x, f0(x), fs(x,10000));} int main(){ rp(0); rp(pi/2+0.01); rp(pi/2-0.01); rp(1); rp(-4); rp(7); rp(11.53); return 0;} /* 0.000000 1.000000 0.999936338023400 1.580796 -1.000000 -0.994543529084521 1.560796 1.000000 0.994543529084516 1.000000 1.000000 1.000112183603661 -4.000000 -1.000000 -0.999968592091892 7.000000 1.000000 0.999952084712269 11.530000 1.000000 1.000114735365318 */