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

ruby-changes:35207

From: akr <ko1@a...>
Date: Tue, 26 Aug 2014 20:48:34 +0900 (JST)
Subject: [ruby-changes:35207] akr:r47289 (trunk): * time.c (rb_time_unmagnify_to_float): Avoid double rounding.

akr	2014-08-26 20:48:21 +0900 (Tue, 26 Aug 2014)

  New Revision: 47289

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

  Log:
    * time.c (rb_time_unmagnify_to_float): Avoid double rounding.
      Reported by Tsuyoshi Sawada.
      https://bugs.ruby-lang.org/issues/10135#note-1

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_time.rb
    trunk/time.c
Index: time.c
===================================================================
--- time.c	(revision 47288)
+++ time.c	(revision 47289)
@@ -697,7 +697,10 @@ rb_time_unmagnify_to_float(wideval_t w) https://github.com/ruby/ruby/blob/trunk/time.c#L697
     }
 #endif
     v = w2v(w);
-    return quo(v, DBL2NUM(TIME_SCALE));
+    if (RB_TYPE_P(v, T_RATIONAL))
+        return rb_Float(quo(v, INT2FIX(TIME_SCALE)));
+    else
+        return quo(v, DBL2NUM(TIME_SCALE));
 }
 
 static void
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47288)
+++ ChangeLog	(revision 47289)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Aug 26 20:46:55 2014  Tanaka Akira  <akr@f...>
+
+	* time.c (rb_time_unmagnify_to_float): Avoid double rounding.
+	  Reported by Tsuyoshi Sawada.
+	  https://bugs.ruby-lang.org/issues/10135#note-1
+
 Tue Aug 26 17:12:47 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* io.c (io_close): ignore only "closed stream" IOError and
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 47288)
+++ test/ruby/test_time.rb	(revision 47289)
@@ -423,6 +423,12 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L423
     assert_equal(946684800.0, t2000.to_f)
   end
 
+  def test_to_f_accuracy
+    # https://bugs.ruby-lang.org/issues/10135#note-1
+    f = 1381089302.195
+    assert_equal(f, Time.at(f).to_f, "[ruby-core:64373] [Bug #10135] note-1")
+  end
+
   def test_cmp
     t2000 = get_t2000
     assert_equal(-1, t2000 <=> Time.gm(2001))

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

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