I coded a "bisection method" but it run forever, please help me, thanks a lot my code, just a simple to find nearest root of x^3+4*x^2-10, think it will stop in 14 times , but it not(i try a and b as 1 and 2)
void bis(double a, double b)
{
cout << fixed << setprecision(8);
double x = (a + b) / 2;
double result = pow(x, 3) + 4 * pow(x, 2) - 10;
int n = 0;
while (abs(result) > 0,0001) {
x = (a + b) / 2;
result = pow(x, 3) + 4 * pow(x, 2) - 10;
if (pow(a, 3) + 4 * pow(a, 2) - 10 > 0 && pow(b, 3) + 4 * pow(b, 2) - 10 < 0) {
n++;
if (result > 0) {
cout << n << " " << a << " " << b << " " << x << " " << result << endl;
a = x;
} else if (result < 0) {
cout << n << " " << a << " " << b << " " << x << " " << result << endl;
b = x;
}
}
if (pow(a, 3) + 4 * pow(a, 2) - 10 < 0 && pow(b, 3) + 4 * pow(b, 2) - 10 > 0) {
n++;
if (result > 0) {
cout << n << " " << a << " " << b << " " << x << " " << result << endl;
b = x;
} else if (result < 0) {
cout << n << " " << a << " " << b << " " << x << " " << result << endl;
a = x;
}
}
}
}