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

ruby-changes:21134

From: marcandre <ko1@a...>
Date: Mon, 5 Sep 2011 04:29:04 +0900 (JST)
Subject: [ruby-changes:21134] marcandRe: r33183 (trunk): * numeric.c (int_round): Integer#round always returns an Integer [Bug #5271]

marcandre	2011-09-05 04:28:54 +0900 (Mon, 05 Sep 2011)

  New Revision: 33183

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

  Log:
    * numeric.c (int_round): Integer#round always returns an Integer [Bug #5271]

  Modified files:
    trunk/ChangeLog
    trunk/numeric.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 33182)
+++ ChangeLog	(revision 33183)
@@ -1,3 +1,8 @@
+Mon Sep  5 04:28:25 2011  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* numeric.c (int_round): Integer#round always returns an Integer [Bug
+	  #5271]
+
 Sun Sep  4 22:28:50 2011  Shugo Maeda  <shugo@r...>
 
 	* lib/net/imap.rb (default_port, default_imap_port,
Index: numeric.c
===================================================================
--- numeric.c	(revision 33182)
+++ numeric.c	(revision 33183)
@@ -3320,6 +3320,7 @@
 {
     VALUE n, f, h, r;
     int ndigits;
+    long bytes;
     ID op;
 
     if (argc == 0) return num;
@@ -3331,11 +3332,15 @@
     if (ndigits == 0) {
 	return num;
     }
-    ndigits = -ndigits;
-    if (ndigits < 0) {
-	rb_raise(rb_eArgError, "ndigits out of range");
+
+    /* If 10**N / 2 > num, then return 0 */
+    /* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */
+    bytes = FIXNUM_P(num) ? sizeof(long) : rb_funcall(num, rb_intern("size"), 0);
+    if (-0.415241 * ndigits - 0.125 > bytes ) {
+	return INT2FIX(0);
     }
-    f = int_pow(10, ndigits);
+
+    f = int_pow(10, -ndigits);
     if (FIXNUM_P(num) && FIXNUM_P(f)) {
 	SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
 	int neg = x < 0;
@@ -3344,6 +3349,10 @@
 	if (neg) x = -x;
 	return LONG2NUM(x);
     }
+    if (TYPE(f) == T_FLOAT) {
+	/* then int_pow overflow */
+	return INT2FIX(0);
+    }
     h = rb_funcall(f, '/', 1, INT2FIX(2));
     r = rb_funcall(num, '%', 1, f);
     n = rb_funcall(num, '-', 1, r);

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

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