//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 4.35 on page 87
//  Recursive implementation of  
//     sinh and cosh functions

public class Pr0435
{ public static void main(String[] args)
  { for (double x=0. ; x<1.0; x += 0.1)
      System.out.println(s(x) + "\t" + sinh(x));
    for (double x=0. ; x<1.0; x += 0.1)
      System.out.println(c(x) + "\t" + cosh(x));
  }
  public static double s(double x)
  { if (Math.abs(x) < 0.005) 
      return x*(1 + x*x/6) ;          // 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;          // basis
    return 1 + 2*s(x/2)*s(x/2);      // recursion
  }
  //nonRecursive methods
  public static double sinh(double x)
  {  return (Math.exp(x) - Math.exp(-x))/2;
  }
  public static double cosh(double x)
  {  return (Math.exp(x) + Math.exp(-x))/2;
  }
}
