[前][次][番号順一覧][スレッド一覧]

ruby-changes:30791

From: akr <ko1@a...>
Date: Sat, 7 Sep 2013 20:26:58 +0900 (JST)
Subject: [ruby-changes:30791] akr:r42870 (trunk): * math.c (math_log): Support bignums bigger than 2**1024.

akr	2013-09-07 20:26:52 +0900 (Sat, 07 Sep 2013)

  New Revision: 42870

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42870

  Log:
    * math.c (math_log): Support bignums bigger than 2**1024.
      (math_log2): Ditto.
      (math_log10): Ditto.

  Modified files:
    trunk/ChangeLog
    trunk/math.c
Index: math.c
===================================================================
--- math.c	(revision 42869)
+++ math.c	(revision 42870)
@@ -11,6 +11,7 @@ https://github.com/ruby/ruby/blob/trunk/math.c#L11
 
 #include "ruby/ruby.h"
 #include "internal.h"
+#include <float.h>
 #include <math.h>
 #include <errno.h>
 
@@ -439,8 +440,16 @@ math_log(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/math.c#L440
 {
     VALUE x, base;
     double d0, d;
+    size_t numbits = 0;
 
     rb_scan_args(argc, argv, "11", &x, &base);
+
+    if (TYPE(x) == T_BIGNUM &&
+            DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
+        numbits -= DBL_MANT_DIG;
+        x = rb_big_rshift(x, SIZET2NUM(numbits));
+    }
+
     Need_Float(x);
     d0 = RFLOAT_VALUE(x);
     /* check for domain error */
@@ -448,6 +457,8 @@ math_log(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/math.c#L457
     /* check for pole error */
     if (d0 == 0.0) return DBL2NUM(-INFINITY);
     d = log(d0);
+    if (numbits)
+        d += numbits * log(2); /* log(2**numbits) */
     if (argc == 2) {
 	Need_Float(base);
 	d /= log(RFLOAT_VALUE(base));
@@ -488,6 +499,13 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/math.c#L499
 math_log2(VALUE obj, VALUE x)
 {
     double d0, d;
+    size_t numbits = 0;
+
+    if (TYPE(x) == T_BIGNUM &&
+            DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
+        numbits -= DBL_MANT_DIG;
+        x = rb_big_rshift(x, SIZET2NUM(numbits));
+    }
 
     Need_Float(x);
     d0 = RFLOAT_VALUE(x);
@@ -496,6 +514,7 @@ math_log2(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L514
     /* check for pole error */
     if (d0 == 0.0) return DBL2NUM(-INFINITY);
     d = log2(d0);
+    d += numbits;
     return DBL2NUM(d);
 }
 
@@ -519,6 +538,13 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/math.c#L538
 math_log10(VALUE obj, VALUE x)
 {
     double d0, d;
+    size_t numbits = 0;
+
+    if (TYPE(x) == T_BIGNUM &&
+            DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
+        numbits -= DBL_MANT_DIG;
+        x = rb_big_rshift(x, SIZET2NUM(numbits));
+    }
 
     Need_Float(x);
     d0 = RFLOAT_VALUE(x);
@@ -527,6 +553,8 @@ math_log10(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L553
     /* check for pole error */
     if (d0 == 0.0) return DBL2NUM(-INFINITY);
     d = log10(d0);
+    if (numbits)
+        d += numbits * log10(2); /* log10(2**numbits) */
     return DBL2NUM(d);
 }
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42869)
+++ ChangeLog	(revision 42870)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Sep  7 20:25:47 2013  Tanaka Akira  <akr@f...>
+
+	* math.c (math_log): Support bignums bigger than 2**1024.
+	  (math_log2): Ditto.
+	  (math_log10): Ditto.
+
 Sat Sep  7 15:36:00 2013  Charlie Somerville  <charliesome@r...>
 
 	* vm_eval.c (vm_call0): fix prototype, the id parameter should be of

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]