I am solving the typical bisection method task but with fixed tolerance (1e-20)
, fixed interval (0, 10)
and given function: x^5 - a*x - 84422%100
, where user inputs a
as a parameter.
For the example with a = 5
I should get the answer 2.3227751229355622988
with precision 20
digits after floating point but I get 2.50000000000000000000
. Where am I wrong here is my code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double e = 1e-20;
const int fn = 84422;
double f(int a, int x)
{
double y = x * x * x * x * x - a * x - fn % 100;
return (double)y;
}
int main()
{
double lv, rv, midv, mid, root, tol;
int left = 0;
int right = 10;
int a;
cin >> a;
do
{
mid = (left + right) / 2.0;
rv = f(a, right);
lv = f(a, left);
midv = f(a, mid);
if(midv == 0)
{
root = mid;
break;
}
if(midv * lv < 0)
{
right = mid;
}
else
left = mid;
} while ((right - left) > e);
root = (left + right) / 2.0;
cout << " The Root is approximately: ";
cout << fixed << setprecision(20) << root << endl;
cin.get();
cin.get();
return 0;
}