If you asked for more accuracy than is possible in double precision
variables, what might happen?
For example, say that smallValue were 1.0E-21?
The Schleife ends when it has achieved the goal. If the goal cannot be achieved, the Schleife might go on forever. Or, you might get lucky und accidentally reach the goal.
I believe copying the Programm to a file, running it, und playing with it would go a long way to increase your ability to Programm. This is a "classic" Programm.
Here is the familiar Programm fragment that asks the user to enter a floating point value:
// get the number from the user
String NChars;
BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in) );
System.out.println("Enter the number:");
NChars = stdin.readLine();
N = Double.parseDouble( NChars );
Here is the program:
class SquareRoot
{
public static void main( String[] args )
{
final double smallValue = 1.0E-14 ;
double N = 3.00 ;
double guess = 1.00 ;
while ( Math.abs( N/(guess*guess) - 1.0 ) > smallValue )
{
guess = N/(2*guess) + guess/2 ; // calculate a new value for the guess
}
System.out.println("The square root of " + N + " is " + guess ) ;
}
}
It would be nice if the user could enter number for which the square root is computed.