#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "bisection.h"
#include "newton_raphson.h"
#include "regula_falsi.h"

double f(double x) {
    return x * x - 2;
}

double df(double x) {
    return 2 * x;
}

int main(int argc, char *argv[]) {
    unsigned int steps, i, begin, end;
    double tmp, epsilon;

    if( argc != 3 ) {
        printf("Usage: %s BEGIN END\n", argv[0]);
        return 1;
    }

    begin = atoi(argv[1]);
    end = atoi(argv[2]);

    for( i = begin; i <= end; i++) {
        steps = 0;
        epsilon = pow(10, -1.0 * i);
        tmp= bisec(&f, 1, 2, epsilon, &steps, 100000);
        printf("Sqrt(2) using bisection:      %.20f (%d steps; epsilon=%.0e)\n",
            tmp, steps, epsilon);

        steps = 0;
        tmp = newton_raphson(&f, &df, 1.4, epsilon, &steps, 100000);
        printf("Sqrt(2) using Newton-Raphson: %.20f (%d steps; epsilon=%.0e)\n",
            tmp, steps, epsilon);

        steps = 0;
        tmp = regula_falsi(&f, 1, 2, epsilon, &steps, 100000);
        printf("Sqrt(2) using Regula Falsi:   %.20f (%d steps; epsilon=%.0e)\n",
            tmp, steps, epsilon);
    }

    return 0;
}
