#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void dumps(char *messtart,double *y,char *mesend){
  int i;
  unsigned char *p;
  p=(unsigned char *)y;
  printf("%s",messtart);
  for (i=0;i<8;i++){
    printf("%02x ",*(p+i));
  }
  printf("%s",mesend);
}
double f(double x){
    return exp(x)-2*x-1;  /* $BNmE@$r5a$a$kH!?t(B */
}
double g(double x){
    return exp(x)-2;      /* $B$=$NF34X?t(B */
}
int main(void){
    int i;
    double x,y,z,fx,fy,fz,m;
    double A=(double)1, B=(double)2;
    double epsilon=1.0e-16;
    /* 2$BJ,K!(B */
    x = A;
    y = B;
    fx = f(x);
    fy = f(y);
    for(i = 1; i <= 54; i++){
        z = (x + y)/2;
        fz = f(z);
        if (fx*fz < 0) {
            y = z; fy=fz;
        } else if (fy*fz < 0){
            x = z; fx = fz;
	} else {
          break;
        }
        if (y == x) break;
    }
    printf("Bisection method: %18.15lf  at i = %d\n",z,i);
    /* Newton $BK!(B */
    x=B;
    for(i=1;i<=20;i++){
       y=f(x)/g(x);
       x=x-y;
       if (fabs(y)<epsilon) break;
    }
    printf("Newton's method : %18.15lf  at i = %d\n",x,i);
    /* $B@~7?H?I|K!(B */
    x = B;
    m = (f(B)-f(A))/(B-A); /* f'(x) $B$N8GDjCM(B */
    for(i = 0; i < 100; i++){
        y = f(x)/m;
        x = x - y;
        if (fabs(y)<epsilon) break;
    }
    printf("Linear iteration: %18.15lf  at i = %d\n",x,i);
    return 0;
}


