Skip to content

Commit 5f899f6

Browse files
committed
less promotion of types
1 parent 96d6245 commit 5f899f6

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

stan/math/prim/fun/log_gamma_q_dgamma.hpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stan/math/prim/fun/gamma_p.hpp>
99
#include <stan/math/prim/fun/gamma_q.hpp>
1010
#include <stan/math/prim/fun/grad_reg_inc_gamma.hpp>
11+
#include <stan/math/prim/fun/inv.hpp>
1112
#include <stan/math/prim/fun/lgamma.hpp>
1213
#include <stan/math/prim/fun/log.hpp>
1314
#include <stan/math/prim/fun/log1m.hpp>
@@ -41,41 +42,40 @@ namespace internal {
4142
* @param z Value at which to evaluate
4243
* @param precision Convergence threshold
4344
* @param max_steps Maximum number of continued fraction iterations
44-
* @return log(Q(a,z)) with same type as T_a and T_z
45+
* @return log(Q(a,z)) with the return type of T_a and T_z
4546
*/
4647
template <typename T_a, typename T_z>
47-
inline auto log_q_gamma_cf(const T_a& a, const T_z& z, double precision = 1e-16,
48-
int max_steps = 250) {
48+
inline return_type_t<T_a, T_z> log_q_gamma_cf(const T_a& a, const T_z& z,
49+
double precision = 1e-16,
50+
int max_steps = 250) {
4951
using stan::math::lgamma;
5052
using stan::math::log;
5153
using stan::math::value_of;
5254
using std::fabs;
5355
using T_return = return_type_t<T_a, T_z>;
5456

55-
const T_return a_ret = a;
56-
const T_return z_ret = z;
57-
const auto log_prefactor = a_ret * log(z_ret) - z_ret - lgamma(a_ret);
57+
const T_return log_prefactor = a * log(z) - z - lgamma(a);
5858

59-
auto b = z_ret + 1.0 - a_ret;
60-
auto C = (fabs(value_of(b)) >= EPSILON) ? b : T_return(EPSILON);
61-
auto D = T_return(0.0);
62-
auto f = C;
59+
T_return b = z + 1.0 - a;
60+
T_return C = (fabs(value_of(b)) >= EPSILON) ? b : T_return(EPSILON);
61+
T_return D = 0.0;
62+
T_return f = C;
6363

6464
for (int i = 1; i <= max_steps; ++i) {
65-
auto an = -i * (i - a_ret);
65+
T_a an = -i * (i - a);
6666
b += 2.0;
6767

6868
D = b + an * D;
69-
if (fabs(value_of(D)) < EPSILON) {
70-
D = T_return(EPSILON);
69+
if (fabs(D) < EPSILON) {
70+
D = EPSILON;
7171
}
7272
C = b + an / C;
73-
if (fabs(value_of(C)) < EPSILON) {
74-
C = T_return(EPSILON);
73+
if (fabs(C) < EPSILON) {
74+
C = EPSILON;
7575
}
7676

77-
D = 1.0 / D;
78-
auto delta = C * D;
77+
D = inv(D);
78+
T_return delta = C * D;
7979
f *= delta;
8080

8181
const double delta_m1 = value_of(fabs(value_of(delta) - 1.0));

0 commit comments

Comments
 (0)