1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* math.c (math_gamma): optimization for passed small integer.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
gogotanaka 2015-03-14 11:07:49 +00:00
parent ffe896c3cb
commit 0e749ddedc
3 changed files with 12 additions and 7 deletions

View file

@ -1,3 +1,7 @@
Sat Mar 14 20:05:23 2015 Kazuki Tanaka <gogotanaka@ruby-lang.org>
* math.c (math_gamma): optimization for passed small integer.
Sat Mar 14 18:07:23 2015 Kazuki Tanaka <gogotanaka@ruby-lang.org>
* enum.c: [DOC] Fixes Enumerable#member? documentation

13
math.c
View file

@ -836,6 +836,8 @@ mingw_tgamma(const double d)
*
*/
#define NGAMMA_TABLE 23
static VALUE
math_gamma(VALUE obj, VALUE x)
{
@ -868,16 +870,13 @@ math_gamma(VALUE obj, VALUE x)
* 53bit mantissa. */
};
double d;
double intpart, fracpart;
d = Get_Double(x);
/* check for domain error */
if (isinf(d) && signbit(d)) domain_error("gamma");
fracpart = modf(d, &intpart);
if (fracpart == 0.0) {
if (intpart < 0) domain_error("gamma");
if (0 < intpart &&
intpart - 1 < (double)numberof(fact_table)) {
return DBL2NUM(fact_table[(int)intpart - 1]);
if (d == floor(d)) {
if (d < 0.0) domain_error("gamma");
if (1.0 <= d && d <= NGAMMA_TABLE) {
return DBL2NUM(fact_table[(int)d - 1]);
}
}
return DBL2NUM(tgamma(d));

View file

@ -240,6 +240,8 @@ class TestMath < Test::Unit::TestCase
check(2, Math.gamma(3))
check(15 * sqrt_pi / 8, Math.gamma(3.5))
check(6, Math.gamma(4))
check(1.1240007277776077e+21, Math.gamma(23))
check(2.5852016738885062e+22, Math.gamma(24))
# no SEGV [ruby-core:25257]
31.upto(65) do |i|