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

ruby-changes:41459

From: nobu <ko1@a...>
Date: Thu, 14 Jan 2016 16:12:14 +0900 (JST)
Subject: [ruby-changes:41459] nobu:r53533 (trunk): sprintf.c: exact number

nobu	2016-01-14 16:12:42 +0900 (Thu, 14 Jan 2016)

  New Revision: 53533

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

  Log:
    sprintf.c: exact number
    
    * sprintf.c (rb_str_format): format exact number more exactly.

  Modified files:
    trunk/ChangeLog
    trunk/sprintf.c
    trunk/test/ruby/test_sprintf.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53532)
+++ ChangeLog	(revision 53533)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jan 14 16:12:09 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* sprintf.c (rb_str_format): format exact number more exactly.
+
 Thu Jan 14 15:08:43 2016  Tony Arcieri  <bascule@g...>
 
 	* Remove 512-bit DH group. It's affected by LogJam Attack.
Index: test/ruby/test_sprintf.rb
===================================================================
--- test/ruby/test_sprintf.rb	(revision 53532)
+++ test/ruby/test_sprintf.rb	(revision 53533)
@@ -149,6 +149,11 @@ class TestSprintf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_sprintf.rb#L149
     assert_equal(" Inf", sprintf("% e", inf), '[ruby-dev:34002]')
   end
 
+  def test_bignum
+    assert_match(/\A10{120}\.0+\z/, sprintf("%f", 100**60))
+    assert_match(/\A10{180}\.0+\z/, sprintf("%f", 1000**60))
+  end
+
   def test_rational
     assert_match(/\A0\.10+\z/, sprintf("%.60f", 0.1r))
     assert_match(/\A0\.010+\z/, sprintf("%.60f", 0.01r))
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 53532)
+++ sprintf.c	(revision 53533)
@@ -1036,13 +1036,19 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1036
 		int sign = (flags&FPLUS) ? 1 : 0, zero = 0;
 		long len, done = 0;
 		int prefix = 0;
-		if (!RB_TYPE_P(val, T_RATIONAL)) {
+		if (FIXNUM_P(val) || RB_TYPE_P(val, T_BIGNUM)) {
+		    den = INT2FIX(1);
+		    num = val;
+		}
+		else if (RB_TYPE_P(val, T_RATIONAL)) {
+		    den = rb_rational_den(val);
+		    num = rb_rational_num(val);
+		}
+		else {
 		    nextvalue = val;
 		    goto float_value;
 		}
 		if (!(flags&FPREC)) prec = default_float_precision;
-		den = rb_rational_den(val);
-		num = rb_rational_num(val);
 		if (FIXNUM_P(num)) {
 		    if ((SIGNED_VALUE)num < 0) {
 			long n = -FIX2LONG(num);

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

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