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

ruby-changes:19916

From: tadf <ko1@a...>
Date: Thu, 9 Jun 2011 19:46:38 +0900 (JST)
Subject: [ruby-changes:19916] tadf:r31963 (ruby_1_8): * lib/date.rb (time_to_day_fraction): accepts flonum without Float#to_r.

tadf	2011-06-09 19:46:20 +0900 (Thu, 09 Jun 2011)

  New Revision: 31963

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

  Log:
    * lib/date.rb (time_to_day_fraction): accepts flonum without Float#to_r.

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/lib/date.rb

Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 31962)
+++ ruby_1_8/ChangeLog	(revision 31963)
@@ -1,3 +1,7 @@
+Thu Jun  9 19:43:57 2011  Tadayoshi Funaba  <tadf@d...>
+
+	* lib/date.rb (time_to_day_fraction): accepts flonum without Float#to_r.
+
 Wed May 25 18:40:47 2011  URABE Shyouhei  <shyouhei@r...>
 
 	* variable.c (rb_const_get_0):  Fix  previous change.   There were
Index: ruby_1_8/lib/date.rb
===================================================================
--- ruby_1_8/lib/date.rb	(revision 31962)
+++ ruby_1_8/lib/date.rb	(revision 31963)
@@ -511,6 +511,20 @@
     return h, min, s, fr
   end
 
+  def self.decode(num)
+    f, n = Math.frexp(num)
+    f = Math.ldexp(f, Float::MANT_DIG).to_i
+    n -= Float::MANT_DIG
+    return f, n
+  end
+
+  def self.f_to_r(num)
+    f, n = decode(num)
+    Rational(f * Float::RADIX ** n)
+  end
+
+  private_class_method :decode, :f_to_r
+
   # Convert an +h+ hour, +min+ minutes, +s+ seconds period
   # to a fractional day.
   begin
@@ -521,11 +535,15 @@
     end
   rescue
     def self.time_to_day_fraction(h, min, s)
-	if Integer === h && Integer === min && Integer === s
-	  Rational(h * 3600 + min * 60 + s, 86400) # 4p
-	else
-	  (h * 3600 + min * 60 + s).to_r/86400 # 4p
+      x = h * 3600 + min * 60 + s
+      if Integer === x
+	Rational(x, 86400) # 4p
+      else
+	if Float === x
+	  x = f_to_r(x)
 	end
+	x / 86400 # 4p
+      end
     end
   end
 

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

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