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

ruby-changes:44424

From: nobu <ko1@a...>
Date: Thu, 27 Oct 2016 09:42:19 +0900 (JST)
Subject: [ruby-changes:44424] nobu:r56497 (trunk): object.c: fixable float to fixnum

nobu	2016-10-27 09:42:11 +0900 (Thu, 27 Oct 2016)

  New Revision: 56497

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56497

  Log:
    object.c: fixable float to fixnum
    
    * object.c (rb_convert_to_integer): convert a fixable float to a
      fixnum directly without the convesion method, as well as bignum
      case.

  Modified files:
    trunk/ChangeLog
    trunk/object.c
    trunk/test/ruby/test_integer.rb
Index: object.c
===================================================================
--- object.c	(revision 56496)
+++ object.c	(revision 56497)
@@ -2684,30 +2684,23 @@ rb_convert_to_integer(VALUE val, int bas https://github.com/ruby/ruby/blob/trunk/object.c#L2684
 {
     VALUE tmp;
 
-    switch (TYPE(val)) {
-      case T_FLOAT:
+    if (RB_FLOAT_TYPE_P(val)) {
+	double f;
 	if (base != 0) goto arg_error;
-	if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
-	    && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
-	    break;
-	}
-	return rb_dbl2big(RFLOAT_VALUE(val));
-
-      case T_FIXNUM:
-      case T_BIGNUM:
+	f = RFLOAT_VALUE(val);
+	if (FIXABLE(f)) return LONG2FIX((long)f);
+	return rb_dbl2big(f);
+    }
+    else if (RB_INTEGER_TYPE_P(val)) {
 	if (base != 0) goto arg_error;
 	return val;
-
-      case T_STRING:
+    }
+    else if (RB_TYPE_P(val, T_STRING)) {
 	return rb_str_to_inum(val, base, TRUE);
-
-      case T_NIL:
+    }
+    else if (NIL_P(val)) {
 	if (base != 0) goto arg_error;
 	rb_raise(rb_eTypeError, "can't convert nil into Integer");
-	break;
-
-      default:
-	break;
     }
     if (base != 0) {
 	tmp = rb_check_string_type(val);
Index: test/ruby/test_integer.rb
===================================================================
--- test/ruby/test_integer.rb	(revision 56496)
+++ test/ruby/test_integer.rb	(revision 56497)
@@ -104,6 +104,16 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L104
     obj = Struct.new(:s).new(%w[42 not-an-integer])
     def obj.to_str; s.shift; end
     assert_equal(42, Integer(obj, 10))
+
+    assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}")
+    begin;
+      class Float
+        undef to_int
+        def to_int; raise "conversion failed"; end
+      end
+      assert_equal (1 << 100), Integer((1 << 100).to_f)
+      assert_equal 1, Integer(1.0)
+    end;
   end
 
   def test_int_p
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56496)
+++ ChangeLog	(revision 56497)
@@ -1,4 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Thu Oct 27 09:12:32 2016  Nobuyoshi Nakada  <nobu@r...>
+Thu Oct 27 09:42:09 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* object.c (rb_convert_to_integer): convert a fixable float to a
+	  fixnum directly without the convesion method, as well as bignum
+	  case.
 
 	* object.c (rb_convert_to_integer): should not drop the converted
 	  string.

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

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