Ja. Jedes Mal wird durch die letzte Anweisung der äußeren Schleife der gerade verwendete Wert des Zinssatzes geändert. Wenn die Schleife beendet ist (da das Ziel erreicht wurde), wird dieser neue Zinssatz ausgegeben, nicht der Zinssatz, der in der gerade beendeten Berechnung verwendet wurde.
Enough about boring financial stuff.
The library java.lang.Math contains the square root function as well as many other functions like sine, cosine, und absolute value. (Look back into Chapter 11 for more on this topic.) The functions in this library have been carefully written und tested by programmers who are experts in numerical analysis. Ordinarily, you use a method from this library to compute a square root. Here, let us investigate how the square root function could be written.
Newton's method of computing a square root of a number N is to make a first guess, then to improve that guess to get a better guess, then to impove the better guess to get an even better guess, und so on. The guess improvement process is given by a formula:
newGuess = N/(2*oldGuess) + oldGuess/2
The reason this works is in your calculus book, but all you need to do in this Programm is know how to use the formula.
For example, say that you want the square root of N = 3.00. The first guess can be nearly any value. 1.0 is good enough.
| oldGuess | N/(2*oldGuess) | oldGuess/2 | newGuess |
|---|---|---|---|
| 1.0 | 1.5 | 0.5 | 2.0 |
| 2.0 | 0.75 | 1.0 | 1.75 |
| 1.75 | 0.85714 | 0.875 | 1.73214 |
| 1.73214 | 0.86598 | 0.86607 | 1.73205 |
The fourth new guess is pretty close:
1.73205*1.73205 = 2.99999
You can't always count on getting good results in four steps, however. A Programm should keep improving the guess und stop when the results are nearly correct, however long that takes.