#include const bool debug = true; double find_sqrt(const double k, const double min_pos, const double max_pos) { if (debug) cout << setprecision(20) << min_pos << " " << max_pos << "\n"; const double error = max_pos - min_pos; const double guess = (min_pos + max_pos) / 2; if (debug) cout << "err=" << error << ", err/guess=" << error/guess << "\n"; if (error/guess < 1.0e-14) return guess; if (debug) cout << "g=" << guess << " " << guess*guess << "\n\n"; if (guess*guess == k) return guess; if (guess*guess > k) return find_sqrt(k, min_pos, guess); else return find_sqrt(k, guess, max_pos); } void main() { while (true) { cout << "search for: "; const double w = read_double(); const double p = find_sqrt(w, 0, w + 1); cout << "the square root of " << w << " is " << p << "\n"; } }