#include <iostream>
using namespace std;
int main() {
double a = 1, b = 2, c, eps = 1e-9;
while (b - a > eps) {
// (1) find middle point
c = (a + b) / 2;
// (2) calculate the function values
double fa = a * a * a - a - 2;
double fb = b * b * b - b - 2;
double fc = c * c * c - c - 2;
// (3) determine which way to go
// if ((fa > 0 && fc < 0 ) || (fa < 0 && fc > 0))
if (fa * fc < 0) {
b = c;
} else {
a = c;
}
}
cout << "root ~ " << c << endl;
cout << "error ~ " << c * c * c - c - 2 << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CgogICAgZG91YmxlIGEgPSAxLCBiID0gMiwgYywgZXBzID0gMWUtOTsKCiAgICB3aGlsZSAoYiAtIGEgPiBlcHMpIHsKICAgICAgICAvLyAoMSkgZmluZCBtaWRkbGUgcG9pbnQKICAgICAgICBjID0gKGEgKyBiKSAvIDI7CgogICAgICAgIC8vICgyKSBjYWxjdWxhdGUgdGhlIGZ1bmN0aW9uIHZhbHVlcwogICAgICAgIGRvdWJsZSBmYSA9IGEgKiBhICogYSAtIGEgLSAyOwogICAgICAgIGRvdWJsZSBmYiA9IGIgKiBiICogYiAtIGIgLSAyOwogICAgICAgIGRvdWJsZSBmYyA9IGMgKiBjICogYyAtIGMgLSAyOwoKICAgICAgICAvLyAoMykgZGV0ZXJtaW5lIHdoaWNoIHdheSB0byBnbwogICAgICAgIC8vIGlmICgoZmEgPiAwICYmIGZjIDwgMCApIHx8IChmYSA8IDAgJiYgZmMgPiAwKSkKICAgICAgICBpZiAoZmEgKiBmYyA8IDApIHsKICAgICAgICAgICAgYiA9IGM7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgYSA9IGM7CiAgICAgICAgfQogICAgfQoKICAgIGNvdXQgPDwgInJvb3QgfiAiIDw8IGMgPDwgZW5kbDsKICAgIGNvdXQgPDwgImVycm9yIH4gIiA8PCBjICogYyAqIGMgLSBjIC0gMiA8PCBlbmRsOwoKICAgIHJldHVybiAwOwp9