Skip to content

Commit ae07391

Browse files
committed
10823 fix, fixed sign of beta(x,y) when x+y large (dlang#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 92dd893 commit ae07391

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
@@ -157,14 +157,23 @@ real sgnGamma(real x)
157157
*/
158158
real beta(real x, real y)
159159
{
160-
if ((x+y)> MAXGAMMA)
160+
if (x > MAXGAMMA || y > MAXGAMMA || (x+y)> MAXGAMMA)
161161
{
162-
return exp(logGamma(x) + logGamma(y) - logGamma(x+y));
162+
const sgnB = sgnGamma(x) * sgnGamma(y) / sgnGamma(x+y);
163+
return sgnB * exp(logGamma(x) + logGamma(y) - logGamma(x+y));
163164
} else return gamma(x) * gamma(y) / gamma(x+y);
164165
}
165166

166167
@safe unittest
167168
{
169+
assert(beta(0.6*MAXGAMMA, 0.5*MAXGAMMA) > 0);
170+
assert(beta(2*MAXGAMMA, -0.5) < 0);
171+
assert(beta(-0.1, 2*MAXGAMMA) < 0);
172+
assert(beta(-1.6, 2*MAXGAMMA) > 0);
173+
assert(beta(+0., 2*MAXGAMMA) == real.infinity);
174+
assert(beta(-0., 2*MAXGAMMA) == -real.infinity);
175+
assert(beta(-MAXGAMMA-1.5, MAXGAMMA+1) < 0);
176+
assert(isNaN(beta(-1, 2*MAXGAMMA)));
168177
assert(isIdentical(beta(NaN(0xABC), 4), NaN(0xABC)));
169178
assert(isIdentical(beta(2, NaN(0xABC)), NaN(0xABC)));
170179
}

0 commit comments

Comments
 (0)