//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 4.34 on page 86
//  More accurate recursive implementation of  
//     sine and cosine functions

public class Pr0434
{ public static void main(String[] args)
  { for (double x=0. ; x<1.0; x += 0.1)
      System.out.println(s(x) + "\t" + Math.sin(x));
    for (double x=0. ; x<1.0; x += 0.1)
      System.out.println(c(x) + "\t" + Math.cos(x));
  }
  public static double s(double x)
  { if (Math.abs(x) < 0.005) 
      return x*(1 - x*x/6*(1-x*x/20)) ;          // basis
    return 2*s(x/2)*c(x/2);         // recursion
  }
  public static double c(double x)
  { if (Math.abs(x) < 0.005) 
        return 1.0 - x*x/2*(1-x*x/12);          // basis
    return 1 - 2*s(x/2)*s(x/2);      // recursion
  }
}
