#include /* The same as the guessing game, except that the computer really doesn't know the answer this time. For the guessing game to work, it needs to be able to tell whether the guess is correct, too big, or too small. It wants to know if guess > sqrt(secret) without being able to use sqrt(). Fortunately, this test will always have the same result guess * guess > secret But if the number's square root can't be represented exactly inside the computer's hardware, it can't possible get exactly the right answer. What are we to do? */ double squareroot(const double secret) { cout << "The secret number is " << secret << "\n"; double min = 0, max = secret+1; int count = 0; while (true) { const double guess = (min + max) / 2; count = count + 1; cout << count << " min=" << setprecision(17) << min << ", max=" << setprecision(17) << max << ", I guess " << setprecision(17) << guess << "\n"; if (guess * guess == secret) return guess; else if (guess * guess > secret) max = guess; else if (guess * guess < secret) min = guess; } cout << "game over.\n"; } void main() { cout << "number? "; const double num = read_double(); const double result = squareroot(num); cout << "Result: " << result << "\n"; }