#include <iostream>
#include <iomanip>
#include <cmath>
 
using namespace std;
 
double f(double x) {
    return x*x*x - 4;
}
 
int main() {
    double x1, x2, x0, f1, f2, f0;
    double true_root = cbrt(4);
 
    x1 = 1.0;
    x2 = 3.0;
 
    f1 = f(x1);
    f2 = f(x2);
 
    if (f1 * f2 > 0) {
        cout << "Initial values do not bracket a root!" << endl;
        return 1;
    }
 
    cout << fixed << setprecision(6);
    cout << "False Position Method for f(x) = x³ - 4" << endl;
    cout << "True root: " << true_root << endl << endl;
 
    cout << "Iteration\tx1\t\tx2\t\tx0\t\tf(x1)\t\tf(x2)\t\tf(x0)\t\tError\t\t% Error" << endl;
    cout << "--------------------------------------------------------------------------------------------------------" << endl;
 
    for (int iteration = 1; iteration <= 3; iteration++) {
 
        x0 = x1 - (f1 * (x2 - x1)) / (f2 - f1);
        f0 = f(x0);
 
        double error = fabs(x0 - true_root);
        double percent_error = (error / true_root) * 100;
 
        cout << iteration << "\t\t" << x1 << "\t" << x2 << "\t" << x0
             << "\t" << f1 << "\t" << f2 << "\t" << f0
             << "\t" << error << "\t" << percent_error << endl;
 
        if (f1 * f0 < 0) {
            x2 = x0;
            f2 = f0;
        } else {
            x1 = x0;
            f1 = f0;
        }
    }
 
 
    x0 = x1 - (f1 * (x2 - x1)) / (f2 - f1);
    f0 = f(x0);
    double error = fabs(x0 - true_root);
    double percent_error = (error / true_root) * 100;
 
    cout << "\nAfter 3 iterations:" << endl;
    cout << "x3 = " << x0 << endl;
    cout << "f(x3) = " << f0 << endl;
    cout << "Error = " << error << endl;
    cout << "Percentage Error = " << percent_error << "%" << endl;
 
    return 0;
}