diff --git a/ChangeLog b/ChangeLog index c63e19d63f..3bf5a1ebef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sat Mar 14 20:05:23 2015 Kazuki Tanaka + + * math.c (math_gamma): optimization for passed small integer. + Sat Mar 14 18:07:23 2015 Kazuki Tanaka * enum.c: [DOC] Fixes Enumerable#member? documentation diff --git a/math.c b/math.c index f66f9ff802..659012148a 100644 --- a/math.c +++ b/math.c @@ -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)); diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb index be86579c55..a4237ecff2 100644 --- a/test/ruby/test_math.rb +++ b/test/ruby/test_math.rb @@ -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|