summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgogotanaka <gogotanaka@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-03-14 11:07:49 (GMT)
committergogotanaka <gogotanaka@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-03-14 11:07:49 (GMT)
commit0e749ddedc100e88dad9fb367ade2e56bbf8f785 (patch)
tree2aa5804c3ea6585943b28993627e1b30a5a3dddb
parentffe896c3cb0872baf301a5c21b90059f33d3d7a0 (diff)
* 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
-rw-r--r--ChangeLog4
-rw-r--r--math.c13
-rw-r--r--test/ruby/test_math.rb2
3 files changed, 12 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index c63e19d..3bf5a1e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/math.c b/math.c
index f66f9ff..6590121 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 be86579..a4237ec 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|