the function evaluates the answer indefinitely
there is a cubic equation, you need to find only 1 of its root. first, using the difference in signs, I find the gap where the root will be, then I find it with a binary search. coefficients can only be between -1000 and 1000 inclusive. the problem is that my code solves some equations calmly, and in some it goes into an infinite loop, although I found an approximate answer through for ((for example) 100) and it converged with the answer on any site that solves equations
#include <iostream>
#include <cmath>
using namespace std;
int a, b, c, d;
// -1, -1, -1, -1 -- work
// -1, -10, -20, -30 -- dont work
double func(double x) {
return (a)*pow(x, 3) + (b)*pow(x, 2) + (c)*x + (d);
}
int getSing(double n) {
if (n > 0) {
return 1;
} else if (n < 0) {
return -1;
} else {
return 0;
}
}
int main()
{
cin >> a >> b >> c >> d;
int left = -1000;
int step = 10;
int right = left + step;
int sing = getSing(func(right));
int opposite_sing = -sing;
int mid = 1;
while (sing != opposite_sing) {
left += step;
right = left + step;
sing = getSing(func(right));
}
while (func(mid) != 0) {
mid = (left + right) / 2;
if (func(mid) > 0) {
left = mid;
} else {
right = mid;
}
}
cout << mid;
}
Источник: Stack Overflow на русском