Skip to content

Commit c348c30

Browse files
authored
10823 fix, fixed sign of beta(x,y) when x+y large (#10824)
Fix the large value method of computing std.mathspecial.beta so that it recovers the signs of gamma(x), gamma(y), and gamma(x+y).
1 parent 4fc3fac commit c348c30

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

std/mathspecial.d

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,23 @@ real sgnGamma(real x)
154154
*/
155155
real beta(real x, real y)
156156
{
157-
if ((x+y)> MAXGAMMA)
157+
if (x > MAXGAMMA || y > MAXGAMMA || (x+y)> MAXGAMMA)
158158
{
159-
return exp(logGamma(x) + logGamma(y) - logGamma(x+y));
159+
const sgnB = sgnGamma(x) * sgnGamma(y) / sgnGamma(x+y);
160+
return sgnB * exp(logGamma(x) + logGamma(y) - logGamma(x+y));
160161
} else return gamma(x) * gamma(y) / gamma(x+y);
161162
}
162163

163164
@safe unittest
164165
{
166+
assert(beta(0.6*MAXGAMMA, 0.5*MAXGAMMA) > 0);
167+
assert(beta(2*MAXGAMMA, -0.5) < 0);
168+
assert(beta(-0.1, 2*MAXGAMMA) < 0);
169+
assert(beta(-1.6, 2*MAXGAMMA) > 0);
170+
assert(beta(+0., 2*MAXGAMMA) == real.infinity);
171+
assert(beta(-0., 2*MAXGAMMA) == -real.infinity);
172+
assert(beta(-MAXGAMMA-1.5, MAXGAMMA+1) < 0);
173+
assert(isNaN(beta(-1, 2*MAXGAMMA)));
165174
assert(isIdentical(beta(NaN(0xABC), 4), NaN(0xABC)));
166175
assert(isIdentical(beta(2, NaN(0xABC)), NaN(0xABC)));
167176
}

0 commit comments

Comments
 (0)