From 069501e66c5936cc951495d9270dfcedcd2f8e4d Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Thu, 6 Mar 2025 11:47:24 +0530 Subject: [PATCH 1/3] fix: special gamma functions --- src/stdlib_specialfunctions_gamma.fypp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/stdlib_specialfunctions_gamma.fypp b/src/stdlib_specialfunctions_gamma.fypp index 88a73063b..4d0c34409 100644 --- a/src/stdlib_specialfunctions_gamma.fypp +++ b/src/stdlib_specialfunctions_gamma.fypp @@ -575,9 +575,9 @@ contains ! Fortran 90 program by Jim-215-Fisher ! ${t1}$, intent(in) :: p, x - integer :: n, m + integer :: n - ${t2}$ :: res, p_lim, a, b, g, c, d, y, ss + ${t2}$ :: res, p_lim, a, b, g, c, d, y ${t2}$, parameter :: zero = 0.0_${k2}$, one = 1.0_${k2}$ ${t2}$, parameter :: dm = tiny(1.0_${k2}$) * 10 ** 6 ${t1}$, parameter :: zero_k1 = 0.0_${k1}$ @@ -603,6 +603,9 @@ contains call error_stop("Error(gpx): Incomplete gamma function with " & //"negative x must come with a whole number p not too small") + if(x < zero_k1) call error_stop("Error(gpx): Incomplete gamma" & + // " function with negative x must have an integer parameter p") + if(p >= p_lim) then !use modified Lentz method of continued fraction !for eq. (15) in the above reference. a = one @@ -670,7 +673,6 @@ contains else !Algorithm 2 in the reference - m = nint(ss) a = - x c = one / a d = p - one @@ -689,9 +691,9 @@ contains end do - if(y >= b * tol_${k2}$ .and. mod(m , 2) /= 0) b = b + d * c / a + if(y >= b * tol_${k2}$ .and. mod(int(p), 2) /= 0) b = b + d * c / a - g = ((-1) ** m * exp(-a + log_gamma(p) - (p - 1) * log(a)) + b) / a + g = ((-1) ** p * exp(-a + log_gamma(p) - (p - 1) * log(a)) + b) / a end if res = g From c0ce8dc6e59b9de77b2e5fd89ee1d744a1359ad1 Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Thu, 6 Mar 2025 12:15:49 +0530 Subject: [PATCH 2/3] remove unreachable code --- src/stdlib_specialfunctions_gamma.fypp | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/stdlib_specialfunctions_gamma.fypp b/src/stdlib_specialfunctions_gamma.fypp index 4d0c34409..6a28c2116 100644 --- a/src/stdlib_specialfunctions_gamma.fypp +++ b/src/stdlib_specialfunctions_gamma.fypp @@ -671,29 +671,6 @@ contains end do - else !Algorithm 2 in the reference - - a = - x - c = one / a - d = p - one - b = c * (a - d) - n = 1 - - do - - c = d * (d - one) / (a * a) - d = d - 2 - y = c * (a - d) - b = b + y - n = n + 1 - - if(n > int((p - 2) / 2) .or. y < b * tol_${k2}$) exit - - end do - - if(y >= b * tol_${k2}$ .and. mod(int(p), 2) /= 0) b = b + d * c / a - - g = ((-1) ** p * exp(-a + log_gamma(p) - (p - 1) * log(a)) + b) / a end if res = g From 499052f2edf0c6b98ab7f0076f16d857a95ebbe1 Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Thu, 6 Mar 2025 22:17:09 +0530 Subject: [PATCH 3/3] add else clause to cover all branches --- src/stdlib_specialfunctions_gamma.fypp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/stdlib_specialfunctions_gamma.fypp b/src/stdlib_specialfunctions_gamma.fypp index 6a28c2116..d051c94df 100644 --- a/src/stdlib_specialfunctions_gamma.fypp +++ b/src/stdlib_specialfunctions_gamma.fypp @@ -4,6 +4,7 @@ #:set CI_KINDS_TYPES = INT_KINDS_TYPES + C_KINDS_TYPES module stdlib_specialfunctions_gamma use iso_fortran_env, only : qp => real128 + use ieee_arithmetic, only: ieee_value, ieee_quiet_nan use stdlib_kinds, only : sp, dp, int8, int16, int32, int64 use stdlib_error, only : error_stop @@ -671,6 +672,9 @@ contains end do + else + g = ieee_value(1._${k1}$, ieee_quiet_nan) + end if res = g