#include <stdio.h>
#include <math.h>

double f(double x) {
    return x * x - 1.0 - sin(x);
}

void bisection(double xl, double xr) {
    double xi;
    int count = 0;
    while (1) {
        xi = (xl + xr) / 2.0;
        count++;
        if (fabs(f(xi)) < 1e-5) break;
        
        if (f(xl) * f(xi) < 0) xr = xi;
        else xl = xi;
    }
    printf("Bisection: x = %f (Iterations: %d)\n", xi, count);
}

void falsePosition(double xl, double xr) {
    double xi;
    int count = 0;
    while (1) {
        // Linear interpolation formula
        xi = (xl * f(xr) - xr * f(xl)) / (f(xr) - f(xl));
        count++;
        if (fabs(f(xi)) < 1e-5) break;
        
        if (f(xl) * f(xi) < 0) xr = xi;
        else xl = xi;
    }
    printf("False Position: x = %f (Iterations: %d)\n", xi, count);
}

int main() {
    // Root 1 interval approx [-1, 0]
    // Root 2 interval approx [1, 2]
    printf("Finding root in [1, 2]:\n");
    bisection(1.0, 2.0);
    falsePosition(1.0, 2.0);
    
    return 0;
}
