C       Program to calculate the zero of a function in a given interval
C       Written by Irvin J. Levy, Gordon College
C
C       P.S. Your comments had better be considerably better than this!
C
        PROGRAM Zero

        DOUBLE PRECISION mini,maxi,step,x,oldval,parm,xin,comp,
     2       thresh,value,test1,test2,calc
 10     FORMAT (' Precision desired in result: '$)
        TYPE 10
        READ (5,*) thresh
        IF (thresh.LT.1E-6) TYPE 15
        IF (thresh.LT.1E-6) thresh=1E-6
 15     FORMAT('+>> Threshold too small.  Auto-set to 1 ppm <<')
 20     FORMAT(/' Find first zero in range (0,0 = done): ',$)
 23     TYPE 20
        READ (5,*) mini,maxi
 25     FORMAT(1X)
        TYPE 25
        IF (mini-maxi) 31,220,31
 31     step=(maxi-mini)/100.
 50     oldval=CALC(mini+0.1*step)
        DO 100 parm=mini,maxi,step
                xin=parm
                value=CALC(xin)
                test1=oldval/ABS(oldval)
                test2=value/ABS(value)
                IF (test1.NE.test2) GOTO 150
 100    CONTINUE
 150    mini=parm-step
        step=step/10.
 175    FORMAT (' Sign change.  X = ',G,' Value = ',G)
        TYPE 175,parm,value
        comp=value/parm
        IF (ABS(comp).LE.thresh) GOTO 200
        GOTO 50
 200    prec=value/parm*1E6
        TYPE 210,parm,value,prec
 210    FORMAT (////' RESULT!!!   X = ',G,' Value = ',G,
     2          /' Precision = ',G,' ppm')
        GOTO 23
 220    STOP 'PROGRAM TERMINATED - RANGE OF ZERO'
        END

C * * * * * * * * * * *  FUNCTION SECTION * * * * * * * * * * *

        FUNCTION CALC(x)
        calc=((2.7182818**(-2.*x))*(2.*x**2+2.*x+1.))-0.100
        RETURN
        END
