          Subroutine Extrem (rmax,rmin,rup,rlo,ntic,tico,
     &                       ticp,ticlen,ialgm)
c
      Dimension iaxdig(10),iindig(10)
      Dimension idax(10),idin(10)
c
d     type *,'$$$$$$$$$$$$$$$$$$$---EXTREM---$$$$$$$$$$$$$$$$$$$'
      rup=0.
      rlo=0.
      ntic=0
      tico=0.
      ticp=0.
      ticlen=0.
      if (rmax.gt.rmin) go to 10
      if (rmax.lt.rmin) type *,' Max. less than Min.!! '
      if (rmax.eq.0..and.rmin.eq.0.) go to 5
      go to 5050
5     continue
d     type *,' .....Both are zeroes... !'
      return
c------------------------------------------------------------
10    call digit (rmax,raxbas,iaxdig)
      call digit (rmin,rinbas,iindig)
      if (iindig(10).eq.1.and.iaxdig(10).eq.1) go to 2000
      if (iindig(10).eq.1)                     go to 3000
      if (ialgm.eq.0) ialgm=2
c-------------------------------------------------------------
d     type *,' ....Both max. and min. are positive.... '
      call rounf (rmax,rmin,iaxdig,iindig,raxbas,rinbas,
     &            rup,rlo,ntic,tico,ticp,ticlen,ialgm)
      iticn1=ifix (ticlen)
      dticn1=float(iticn1)
      diftn1=ticlen-dticn1
      if (diftn1.lt.0.001) go to 90     !i.e. ticlen is a no.
      difpo=ticp-tico                   !w/o decimal figures
      if (difpo.gt.40) go to 90
      if (difpo.lt.0.001) go to 95      !i.e. ticp=tico
c      tico=tico+1
      ticlen=(ticp-tico)/ntic
d     type *,' difpo=',difpo,' diftn1=',diftn1
d     type *,' ticp=',ticp,' tico=',tico
d     type *,' ticlen=',ticlen,' dticn1=',dticn1
      go to 5010
90    if (ticp.ne.tico) go to 5010
95    continue
d     type *,' ticp=tico.....adjusting '
      if (ticlen.eq.0.)   type *,' ticlen=0.!!!!! '
      ticp=tico+ticlen
      if (rup.lt.ticp) rup=ticp      
100   diflow=tico-rlo
      ntic=2
      ticlen=ticlen/2.
      iticln=ifix (ticlen)
      dticln=float(ticlen)
      diftln=ticlen-dticln
      if (diftln.lt.0.001) go to 200   !i.e. ticlen=dticln
      tico=tico+1
      ticlen=(ticp-tico)/ntic
200   continue
d     type *,' <ntic=2>... '
d     type *,' tico=',tico,' diftln=',diftln
d     type *,' ticlen=',ticlen,' iticln=',iticln
d     type *,' diflow=',diflow
      if (diflow.lt.ticlen) go to 5010
      ntic=3
      tico=tico-ticlen
      go to 5010
c-------------------------------------------------------------
2000  continue
d     type *,' ....Both max. and min. are negative.... '
      dax=abs( rmin )
      din=abs( rmax )
      do 2100 i=1,10
      idax(i)=iindig(i)
2100  idin(i)=iaxdig(i)
      drax=rinbas
      drin=raxbas
      call rounf (dax,din,idax,idin,drax,drin,drup,drlo,
     &            ntic,dtico,dticp,ticlen,ialgm)
      rup=-drlo
      rlo=-drup
      ticp=-dtico
      tico=-dticp
      if (ticp.ne.tico) go to 5010
d     type *,' ticp=tico.....adjusting '
      if (ticlen.eq.0.)   type *,' ticlen=0.!!!!! '
      tico=tico-ticlen
      if (rlo.gt.tico) rlo=tico      
      difup=rup-ticp
      ntic=2
      ticlen=ticlen/2.
      if (difup.lt.ticlen) go to 5010
      ntic=3
      ticp=ticp+ticlen
      go to 5010
c-------------------------------------------------------------
3000  continue
d     type *,' ....max. is positive & min. is negative.... '
      do 3100 ix=1,10
      idin(ix)=0.
3100  idax(ix)=iindig(ix)
c-----parameters for max.
      if (rmax.eq.0.) go to 3101
      call rounf (rmax,0.,iaxdig,idin,raxbas,1.,rup,drlo,
     &            nticx,tico,ticpx,ticlex,ialgm)
      go to 3102
3101  rup=0.
      ticpx=0.
3102  continue
d     type *,' <3102>...parameters for max. '
d     type *,' rup=',rup,' ticpx=',ticpx
d     type *,' nticx=',nticx,' ticlex=',ticlex
c-----parameters for min.
      dax=abs (rmin)
      din=0.
      if (rmin.eq.0.) go to 3103
      call rounf (dax,din,idax,idin,rinbas,1.,drup,drlo,
     &            nticn,tico,dticp,ticlem,ialgm)
      rlo=-drup
      ticpn=-dticp
      go to 3104
3103  rlo=0.
      ticpn=0.
3104  continue
d     type *,' <3104>...parameters for min. '
d     type *,' rlo=',rlo,' ticpn=',ticpn
d     type *,' nticn=',nticn,' ticlem=',ticlem
      call ticks (rmax,rmin,rup,rlo,ticpx,ticpn,nticx,nticn,
     &            ticlex,ticlem,raxbas,rinbas,ticp,tico,
     &            recrup,range,ntic,ticlen)
      difrec=abs(recrup-range)
d     type *,' difrec=',difrec
      if (recrup.le.range) go to 5000
      ticp=recrup+tico
      rup=ticp
      if (rup.lt.rmax) rup=rmax
d     type *,' go to <5050>.... '
d     type *,' difrec=',difrec
d     type *,' ticp=',ticp,' rup=',rup
      go to 5050
c--------------------------------------------------------------      
c-----print out results if debug on.
5000  if (difrec.lt.0.001) go to 5020   !ie. recrup=range
5010  updef=rup-ticp
      if (updef.lt.ticlen) go to 5050
      ticp=ticp+ticlen
      ntic=ntic+1
d     type *,' <5000>.... '
d     type *,' updef=',updef,' ticp=',ticp,' ntic=',ntic
      go to 5050
5020  rup=ticpx
      rlo=ticpn
      ticp=ticpx
      tico=ticpn
      if (rmax.gt.ticpx) rup=rmax
      if (rmin.lt.ticpn) rlo=rmin
d     type *,' <5020>.... '
d     type *,' ticpx=',ticpx,' ticpn=',ticpn
d     type *,' difrec=',difrec
5050  continue
d     type *,' <5050>...................................... '
d     type 5100
5100  format ( 2x,' rmax  ',7x,' rmin  ',7x,' recmax  ',7x,
     &            ' recmin  ')
d     type 5200, rmax,rmin,recmax,recmin
5200  format ( 2x,f10.3,3x,f10.3,4x,f10.3,6x,f10.3)
d     type 5300
5300  format ( 2x,'  rup  ',7x,'  rlo  ',7x,'  ticp   ',7x,
     &            ' tico    ')
d     type 5200, rup,rlo,ticp,tico
d     type 5400
5400  format ( 2x,' ntic  ',7x,' ticlen  ' )
d      type 5500, ntic,ticlen
5500  format (2x,i3,10x,f10.3 )
d     type *,'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'
c
      return
      end
