我实现了一版,供你参考。如果有帮助,望采纳。
#include <math.h>
#include <stdio.h>
double f(double x) {
return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6;
}
int main() {
double lower = -10;
double upper = 10;
double x, y, _y;
while (1) {
x = (lower + upper) / 2;
y = f(x);
if (y == 0) {
break;
}
_y = f(lower);
if (y * _y > 0) { //同号
lower = x;
} else {
upper = x;
}
}
printf("%lf", x);
return 0;
}