      function gamma_ln(x) ! bernd berg, june 24 1999.
c ln of gamma function ala lanczos, siam num. anal. b1 (1964) 1.
      include '../../libs/fortran/implicit.sta'
      include '../../libs/fortran/constants.par'
      if(x.le.zero) then
        print*,"gamma_ln: argument x =",x
        stop 'gamma_ln: illegal argument.'
      end if
      if(x.gt.one) then ! full accuracy of lanczos formula.
        y=x
      else ! use gamma(z+1)=z*gamma(z).
        y=one+x
      endif
      ser=((one+c1_l/y)+c2_l/(y+one))+c3_l/(y+two)
      ser=((ser+c4_l/(y+three))+c5_l/(y+four))+c6_l/(y+five)
      gamma_ln=(y-half)*log(y+fnine_half)-(y+fnine_half)+log(stp_l*ser)
      if(x.gt.one) return
      gamma_ln=gamma_ln-log(x)
      return
      end
