// Newton-Raphson method for the root of f(x)
// Analytically the solution is x^3-28 = 0
// => x = 28^(1/3) = 3.0365889718756625194208095785057...
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double f(double x) { return x*x*x-28.; } // The function f(x)
double d(double x) { return 3*x*x; }     // The derivative of f(x)

int main() {

  double  x=3.5, Tolerance=1.0E-12;
  cout << setprecision(18) << fixed;

  do {

    double Error = f(x) / d(x);               // Estimate the error
    cout << "Root = " << x << " Error = " << Error << endl;

    if ( abs(Error) < Tolerance ) break;      // Terminate if the tolerance is satisfied
    x = x - Error;                            // Subtract the error estimate

  } while(1);

}
