UPP  V11.0.0
 All Data Structures Files Functions Pages
FGAMMA.f
1  REAL FUNCTION fgamma(X)
2 !D DOUBLE PRECISION FUNCTION fGAMMA(X)
3 !----------------------------------------------------------------------
4 !
5 ! This routine calculates the GAMMA function for a real argument X.
6 ! Computation is based on an algorithm outlined in reference 1.
7 ! The program uses rational functions that approximate the GAMMA
8 ! function to at least 20 significant decimal digits. Coefficients
9 ! for the approximation over the interval (1,2) are unpublished.
10 ! Those for the approximation for X >= 12 are from reference 2.
11 ! The accuracy achieved depends on the arithmetic system, the
12 ! compiler, the intrinsic functions, and proper selection of the
13 ! machine-dependent constants.
14 !
15 !
16 !*******************************************************************
17 !*******************************************************************
18 !
19 ! Explanation of machine-dependent constants
20 !
21 ! beta - radix for the floating-point representation
22 ! maxexp - the smallest positive power of beta that overflows
23 ! XBIG - the largest argument for which GAMMA(X) is representable
24 ! in the machine, i.e., the solution to the equation
25 ! GAMMA(XBIG) = beta**maxexp
26 ! XINF - the largest machine representable floating-point number;
27 ! approximately beta**maxexp
28 ! EPS - the smallest positive floating-point number such that
29 ! 1.0+EPS > 1.0
30 ! XMININ - the smallest positive floating-point number such that
31 ! 1/XMININ is machine representable
32 !
33 ! Approximate values for some important machines are:
34 !
35 ! beta maxexp XBIG
36 !
37 ! CRAY-1 (S.P.) 2 8191 966.961
38 ! Cyber 180/855
39 ! under NOS (S.P.) 2 1070 177.803
40 ! IEEE (IBM/XT,
41 ! SUN, etc.) (S.P.) 2 128 35.040
42 ! IEEE (IBM/XT,
43 ! SUN, etc.) (D.P.) 2 1024 171.624
44 ! IBM 3033 (D.P.) 16 63 57.574
45 ! VAX D-Format (D.P.) 2 127 34.844
46 ! VAX G-Format (D.P.) 2 1023 171.489
47 !
48 ! XINF EPS XMININ
49 !
50 ! CRAY-1 (S.P.) 5.45E+2465 7.11E-15 1.84E-2466
51 ! Cyber 180/855
52 ! under NOS (S.P.) 1.26E+322 3.55E-15 3.14E-294
53 ! IEEE (IBM/XT,
54 ! SUN, etc.) (S.P.) 3.40E+38 1.19E-7 1.18E-38
55 ! IEEE (IBM/XT,
56 ! SUN, etc.) (D.P.) 1.79D+308 2.22D-16 2.23D-308
57 ! IBM 3033 (D.P.) 7.23D+75 2.22D-16 1.39D-76
58 ! VAX D-Format (D.P.) 1.70D+38 1.39D-17 5.88D-39
59 ! VAX G-Format (D.P.) 8.98D+307 1.11D-16 1.12D-308
60 !
61 !*******************************************************************
62 !*******************************************************************
63 !
64 ! Error returns
65 !
66 ! The program returns the value XINF for singularities or
67 ! when overflow would occur. The computation is believed
68 ! to be free of underflow and overflow.
69 !
70 !
71 ! Intrinsic functions required are:
72 !
73 ! INT, DBLE, EXP, LOG, REAL, SIN
74 !
75 !
76 ! References: "An Overview of Software Development for Special
77 ! Functions", W. J. Cody, Lecture Notes in Mathematics,
78 ! 506, Numerical Analysis Dundee, 1975, G. A. Watson
79 ! (ed.), Springer Verlag, Berlin, 1976.
80 !
81 ! Computer Approximations, Hart, Et. Al., Wiley and
82 ! sons, New York, 1968.
83 !
84 ! Latest modification: October 12, 1989
85 !
86 ! Authors: W. J. Cody and L. Stoltz
87 ! Applied Mathematics Division
88 ! Argonne National Laboratory
89 ! Argonne, IL 60439
90 
91 ! 10-30-19 Bo CUI - REMOVE "GOTO" STATEMENT
92 !
93 !----------------------------------------------------------------------
94  implicit none
95 
96  INTEGER i,n
97  LOGICAL parity
98  REAL c,conv,eps,fact,half,one,p,pi,q,res,sqrtpi,sum,twelve, &
99  two,x,xbig,xden,xinf,xminin,xnum,y,y1,ysq,z,zero
100  dimension c(7),p(8),q(8)
101 !----------------------------------------------------------------------
102 ! Mathematical constants
103 !----------------------------------------------------------------------
104  DATA one,half,twelve,two,zero/1.0e0,0.5e0,12.0e0,2.0e0,0.0e0/, &
105  sqrtpi/0.9189385332046727417803297e0/, &
106  pi/3.1415926535897932384626434e0/
107 !D DATA ONE,HALF,TWELVE,TWO,ZERO/1.0D0,0.5D0,12.0D0,2.0D0,0.0D0/,
108 !D 1 SQRTPI/0.9189385332046727417803297D0/,
109 !D 2 PI/3.1415926535897932384626434D0/
110 !----------------------------------------------------------------------
111 ! Machine dependent parameters
112 !----------------------------------------------------------------------
113  DATA xbig,xminin,eps/35.040e0,1.18e-38,1.19e-7/, &
114  xinf/3.4e38/
115 !D DATA XBIG,XMININ,EPS/171.624D0,2.23D-308,2.22D-16/,
116 !D 1 XINF/1.79D308/
117 !----------------------------------------------------------------------
118 ! Numerator and denominator coefficients for rational minimax
119 ! approximation over (1,2).
120 !----------------------------------------------------------------------
121  DATA p/-1.71618513886549492533811e+0,2.47656508055759199108314e+1,&
122  -3.79804256470945635097577e+2,6.29331155312818442661052e+2,&
123  8.66966202790413211295064e+2,-3.14512729688483675254357e+4,&
124  -3.61444134186911729807069e+4,6.64561438202405440627855e+4/
125  DATA q/-3.08402300119738975254353e+1,3.15350626979604161529144e+2,&
126  -1.01515636749021914166146e+3,-3.10777167157231109440444e+3,&
127  2.25381184209801510330112e+4,4.75584627752788110767815e+3,&
128  -1.34659959864969306392456e+5,-1.15132259675553483497211e+5/
129 !D DATA P/-1.71618513886549492533811D+0,2.47656508055759199108314D+1,
130 !D 1 -3.79804256470945635097577D+2,6.29331155312818442661052D+2,
131 !D 2 8.66966202790413211295064D+2,-3.14512729688483675254357D+4,
132 !D 3 -3.61444134186911729807069D+4,6.64561438202405440627855D+4/
133 !D DATA Q/-3.08402300119738975254353D+1,3.15350626979604161529144D+2,
134 !D 1 -1.01515636749021914166146D+3,-3.10777167157231109440444D+3,
135 !D 2 2.25381184209801510330112D+4,4.75584627752788110767815D+3,
136 !D 3 -1.34659959864969306392456D+5,-1.15132259675553483497211D+5/
137 !----------------------------------------------------------------------
138 ! Coefficients for minimax approximation over (12, INF).
139 !----------------------------------------------------------------------
140  DATA c/-1.910444077728e-03,8.4171387781295e-04, &
141  -5.952379913043012e-04,7.93650793500350248e-04, &
142  -2.777777777777681622553e-03,8.333333333333333331554247e-02, &
143  5.7083835261e-03/
144 !D DATA C/-1.910444077728D-03,8.4171387781295D-04,
145 !D 1 -5.952379913043012D-04,7.93650793500350248D-04,
146 !D 2 -2.777777777777681622553D-03,8.333333333333333331554247D-02,
147 !D 3 5.7083835261D-03/
148 !----------------------------------------------------------------------
149 ! Statement functions for conversion between integer and float
150 !----------------------------------------------------------------------
151  conv(i) = REAL(i)
152 !D CONV(I) = DBLE(I)
153  parity = .false.
154  fact = one
155  n = 0
156  y = x
157  IF (y <= zero) THEN
158 !----------------------------------------------------------------------
159 ! Argument is negative
160 !----------------------------------------------------------------------
161  y = -x
162  y1 = aint(y)
163  res = y - y1
164  IF (res /= zero) THEN
165  IF (y1 /= aint(y1*half)*two) parity = .true.
166  fact = -pi / sin(pi*res)
167  y = y + one
168  ELSE
169  res = xinf
170  fgamma = res
171  RETURN
172  END IF
173  END IF
174 !----------------------------------------------------------------------
175 ! Argument is positive
176 !----------------------------------------------------------------------
177  IF (y < eps) THEN
178 !----------------------------------------------------------------------
179 ! Argument < EPS
180 !----------------------------------------------------------------------
181  IF (y >= xminin) THEN
182  res = one / y
183  ELSE
184  res = xinf
185  fgamma = res
186  RETURN
187  END IF
188  ELSE IF (y < twelve) THEN
189  y1 = y
190  IF (y < one) THEN
191 !----------------------------------------------------------------------
192 ! 0.0 < argument < 1.0
193 !----------------------------------------------------------------------
194  z = y
195  y = y + one
196  ELSE
197 !----------------------------------------------------------------------
198 ! 1.0 < argument < 12.0, reduce argument if necessary
199 !----------------------------------------------------------------------
200  n = int(y) - 1
201  y = y - conv(n)
202  z = y - one
203  END IF
204 !----------------------------------------------------------------------
205 ! Evaluate approximation for 1.0 < argument < 2.0
206 !----------------------------------------------------------------------
207  xnum = zero
208  xden = one
209  DO 260 i = 1, 8
210  xnum = (xnum + p(i)) * z
211  xden = xden * z + q(i)
212  260 CONTINUE
213  res = xnum / xden + one
214  IF (y1 < y) THEN
215 !----------------------------------------------------------------------
216 ! Adjust result for case 0.0 < argument < 1.0
217 !----------------------------------------------------------------------
218  res = res / y1
219  ELSE IF (y1 > y) THEN
220 !----------------------------------------------------------------------
221 ! Adjust result for case 2.0 < argument < 12.0
222 !----------------------------------------------------------------------
223  DO 290 i = 1, n
224  res = res * y
225  y = y + one
226  290 CONTINUE
227  END IF
228  ELSE
229 !----------------------------------------------------------------------
230 ! Evaluate for argument >= 12.0,
231 !----------------------------------------------------------------------
232  IF (y <= xbig) THEN
233  ysq = y * y
234  sum = c(7)
235  DO 350 i = 1, 6
236  sum = sum / ysq + c(i)
237  350 CONTINUE
238  sum = sum/y - y + sqrtpi
239  sum = sum + (y-half)*log(y)
240  res = exp(sum)
241  ELSE
242  res = xinf
243  fgamma = res
244  RETURN
245  END IF
246  END IF
247 !----------------------------------------------------------------------
248 ! Final adjustments and return
249 !----------------------------------------------------------------------
250  IF (parity) res = -res
251  IF (fact /= one) res = fact / res
252  fgamma = res
253  RETURN
254 ! ---------- Last line of fGAMMA ----------
255  END
256