A Program to Find a Square Root

If you want to find the square root of a positive REAL number in a FORTRAN program, the simplest method is to use the SQRT function. It is quite reasonable, however, to ask how it is done.

Here is a program to find a square root. It uses a method known as "Newton's Algorithm".


         PROGRAM SQROOT

         IMPLICIT NONE
         REAL, PARAMETER :: E = 0.0001
         REAL :: NUM, X1, X2, RESULT
          
         PRINT *, 'We will find the square root of a number.'
         PRINT *, 'Pick a real number.'

         READ *, NUM

         IF (NUM < 0.0) THEN
           PRINT *, 'Your number is negative:  no real root.'
         ELSE
           IF (NUM == 0.0) THEN
             RESULT = 0.0
           ELSE
             X1 = NUM
             X2 = 1.0
             DO WHILE (ABS(X1 - X2) > E)
               X1 = (X1 + X2) / 2.0
               X2 = NUM / X1
               PRINT *, 'Our current values are:  ', X1, ' and ', X2
             END DO
           END IF
           PRINT *, 'The square root of ', NUM, ' is:  ', X1
         END IF

         END PROGRAM SQROOT


A few notes


Output from the SQROOT program

 We will find the square root of a number.
 Pick a real number.
 Our current values are:     1.5000000      and    1.3333334    
 Our current values are:     1.4166667      and    1.4117646    
 Our current values are:     1.4142157      and    1.4142114    
 The square root of    2.0000000      is:     1.4142157    


 We will find the square root of a number.
 Pick a real number.
 Our current values are:    0.50500000      and   1.98019799E-02
 Our current values are:    0.26240098      and   3.81096117E-02
 Our current values are:    0.15025529      and   6.65533915E-02
 Our current values are:    0.10840434      and   9.22472253E-02
 Our current values are:    0.10032578      and   9.96752754E-02
 Our current values are:    0.10000053      and   9.99994650E-02
 The square root of   9.99999978E-03  is:    0.10000053    


 We will find the square root of a number.
 Pick a real number.
 The square root of    0.0000000      is:    3.21393408E-39


 We will find the square root of a number.
 Pick a real number.
 The square root of    1.0000000      is:     1.0000000    


 We will find the square root of a number.
 Pick a real number.
 Your number,   -1.0000000      is negative:  no real root.

 
 We will find the square root of a number.
 Pick a real number.
 Our current values are:     3200.5000      and    1.9996876    
 Our current values are:     1601.2499      and    3.9968777    
 Our current values are:     802.62335      and    7.9738522    
 Our current values are:     405.29861      and    15.790826    
 Our current values are:     210.54472      and    30.397343    
 Our current values are:     120.47103      and    53.124805    
 Our current values are:     86.797920      and    73.734482    
 Our current values are:     80.266205      and    79.734680    
 Our current values are:     80.000443      and    79.999557    
 Our current values are:     80.000000      and    80.000000    
 The square root of    6400.0000      is:     80.000000