#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main () {
    cout << "\nRoundoff errors in quadratic formula"
         << "\n------------------------------------"
         << "\nSolve quadratic  ax^2 + bx + c = 0"
         << "\nUsing (-b +- sqrt(b^2 - 4ac) / (2a)"
         << "\nCompare with better smaller = c / (a * larger)"
         << "\n\nEnter two roots x1 x2: " << flush;
    double x1, x2;
    cin >> x1 >> x2;

    double a = 1;
    double b = - (x1 + x2);
    double c = x1 * x2;
    double discr = b * b - 4 * a * c;
    cout << "a = " << a << ", b = " << b << " c = " << c
         << "\ndiscriminant b^2 - 4ac = " << discr << endl;

    double larger = (-b + sqrt(discr)) / (2 * a);
    double smaller = (-b - sqrt(discr)) / (2 * a);
    if (b > 0) {
        double swap = larger;
        larger = smaller;
        smaller = swap;
    }
    double better = c / (a * larger);
    if (abs(x1) < abs(x2)) {
        double swap = x1;
        x1 = x2;
        x2 = swap;
    }

    cout.precision(16);
    cout.setf(ios::left);
    cout << "-------------------------------------------------------------\n"
         << "                Value of root           Fractional error     \n"
         << "-------------------------------------------------------------\n";
    cout << "Larger |root|   " << setw(22) << larger << "  "
         << abs((larger - x1) / x1) << '\n'
         << "Smaller |root|  " << setw(22) << smaller << "  "
         << abs((smaller - x2) / x2) << '\n'
         << "Better smaller  " << setw(22) << better << "  "
         << abs((better - x2) / x2) << '\n'
         << "-------------------------------------------------------------\n";
}

