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

ruby-changes:20639

From: mrkn <ko1@a...>
Date: Wed, 27 Jul 2011 01:17:18 +0900 (JST)
Subject: [ruby-changes:20639] mrkn:r32687 (ruby_1_9_3): Merge revisions 32676, 32677, 32679, 32680:

mrkn	2011-07-27 01:17:04 +0900 (Wed, 27 Jul 2011)

  New Revision: 32687

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

  Log:
    Merge revisions 32676, 32677, 32679, 32680:
        * ext/bigdecimal/lib/bigdecimal/util.rb (Rational#to_d):
          zero or negative precision is error.  fixes #5098.
          [ruby-dev:44210]
        * ext/bigdecimal/lib/bigdecimal/util.rb (Float#to_d): modified for
          specifying precision.  fixes #5098.  [ruby-dev:44210]
        * ext/bigdecimal/lib/bigdecimal/util.rb (Integer#to_d): added
          for symmetry to BigDecimal() function with an Integer.
          fixes #5098.  [ruby-dev:44210]
        * ext/bigdecimal/lib/bigdecimal/util.rb (BigDecimal#to_d): added
          for adapting other Numeric subclasses.  [ruby-dev:44245]
        * test/bigdecimal/test_bigdecimal_util.rb: add tests for the above
          changes.

  Added files:
    branches/ruby_1_9_3/test/bigdecimal/test_bigdecimal_util.rb
  Modified files:
    branches/ruby_1_9_3/ChangeLog
    branches/ruby_1_9_3/ext/bigdecimal/lib/bigdecimal/util.rb

Index: ruby_1_9_3/ChangeLog
===================================================================
--- ruby_1_9_3/ChangeLog	(revision 32686)
+++ ruby_1_9_3/ChangeLog	(revision 32687)
@@ -1,3 +1,31 @@
+Wed Jul 27 01:13:00 2011  Kenta Murata  <mrkn@m...>
+
+	* ext/bigdecimal/lib/bigdecimal/util.rb (Rational#to_d):
+	  zero or negative precision is error.  fixes #5098.
+	  [ruby-dev:44210]
+
+	* ext/bigdecimal/lib/bigdecimal/util.rb (Float#to_d): modified for
+	  specifying precision.  fixes #5098.  [ruby-dev:44210]
+
+	* ext/bigdecimal/lib/bigdecimal/util.rb (Integer#to_d): added
+	  for symmetry to BigDecimal() function with an Integer.
+	  fixes #5098.  [ruby-dev:44210]
+
+	* ext/bigdecimal/lib/bigdecimal/util.rb (BigDecimal#to_d): added
+	  for adapting other Numeric subclasses.  [ruby-dev:44245]
+
+	* test/bigdecimal/test_bigdecimal_util.rb: add tests for the above
+	  changes.
+
+Wed Jul 27 00:54:38 2011  Kenta Murata  <mrkn@m...>
+
+	* bigdecimal/bigdecimal.c (VpDup) a new function for duplicating
+	  a BigDecimal.
+
+	* bigdecimal/bigdecimal.c (BigDecimal_new): support generating a new
+	  BigDecimal from another BigDecimal using BigDecimal global function
+	  or constructor.  [ruby-dev:44245]
+
 Mon Jul 25 22:24:09 2011  Hiroshi Nakamura  <nahi@r...>
 
 	* backport r32666 from trunk.
Index: ruby_1_9_3/ext/bigdecimal/lib/bigdecimal/util.rb
===================================================================
--- ruby_1_9_3/ext/bigdecimal/lib/bigdecimal/util.rb	(revision 32686)
+++ ruby_1_9_3/ext/bigdecimal/lib/bigdecimal/util.rb	(revision 32687)
@@ -1,3 +1,20 @@
+class Integer < Numeric
+  # call-seq:
+  #     int.to_d  -> bigdecimal
+  #
+  # Convert +int+ to a BigDecimal and return it.
+  #
+  #     require 'bigdecimal'
+  #     require 'bigdecimal/util'
+  #
+  #     42.to_d
+  #     # => #<BigDecimal:1008ef070,'0.42E2',9(36)>
+  #
+  def to_d
+    BigDecimal(self)
+  end
+end
+
 class Float < Numeric
   # call-seq:
   #     flt.to_d  -> bigdecimal
@@ -10,8 +27,8 @@
   #     0.5.to_d
   #     # => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
   #
-  def to_d
-    BigDecimal(self.to_s)
+  def to_d(precision=nil)
+    BigDecimal(self, precision || Float::DIG+1)
   end
 end
 
@@ -54,6 +71,14 @@
       i + "." + ("0"*(-z)) + f
     end
   end
+
+  # call-seq:
+  #     a.to_d -> bigdecimal
+  #
+  # Returns self.
+  def to_d
+    self
+  end
 end
 
 class Rational < Numeric
@@ -70,11 +95,11 @@
   #   # => #<BigDecimal:1a52bd8,'0.3142857142 8571427937 0154144999 105E1',45(63)>
   #   r.to_d(3)
   #   # => #<BigDecimal:1a44d08,'0.314E1',18(36)>
-  def to_d(nFig=0)
-    num = self.numerator.to_s
-    if nFig<=0
-      nFig = BigDecimal.double_fig*2+1
+  def to_d(precision)
+    if precision <= 0
+      raise ArgumentError, "negative precision"
     end
-    BigDecimal.new(num).div(self.denominator,nFig)
+    num = self.numerator
+    BigDecimal(num).div(self.denominator, precision)
   end
 end
Index: ruby_1_9_3/test/bigdecimal/test_bigdecimal_util.rb
===================================================================
--- ruby_1_9_3/test/bigdecimal/test_bigdecimal_util.rb	(revision 0)
+++ ruby_1_9_3/test/bigdecimal/test_bigdecimal_util.rb	(revision 32687)
@@ -0,0 +1,43 @@
+require_relative "testbase"
+
+require 'bigdecimal/util'
+
+class TestBigDecimalUtil < Test::Unit::TestCase
+  def test_BigDecimal_to_d
+    x = BigDecimal(1)
+    assert_same(x, x.to_d)
+  end
+
+  def test_Integer_to_d
+    assert_equal(BigDecimal(1), 1.to_d)
+    assert_equal(BigDecimal(2<<100), (2<<100).to_d)
+  end
+
+  def test_Float_to_d_without_precision
+    delta = 1.0/10**(Float::DIG + 1)
+    assert_in_delta(BigDecimal(0.5, Float::DIG+1), 0.5.to_d, delta)
+    assert_in_delta(BigDecimal(355.0/113.0, Float::DIG+1), (355.0/113.0).to_d, delta)
+  end
+
+  def test_Float_to_d_with_precision
+    digits = 5
+    delta = 1.0/10**(digits)
+    assert_in_delta(BigDecimal(0.5, 5), 0.5.to_d(digits), delta)
+    assert_in_delta(BigDecimal(355.0/113.0, 5), (355.0/113.0).to_d(digits), delta)
+  end
+
+  def test_Rational_to_d
+    digits = 100
+    delta = 1.0/10**(digits)
+    assert_in_delta(BigDecimal(1.quo(2), digits), 1.quo(2).to_d(digits), delta)
+    assert_in_delta(BigDecimal(355.quo(113), digits), 355.quo(113).to_d(digits), delta)
+  end
+
+  def test_Rational_to_d_with_zero_precision
+    assert_raise(ArgumentError) { 355.quo(113).to_d(0) }
+  end
+
+  def test_Rational_to_d_with_negative_precision
+    assert_raise(ArgumentError) { 355.quo(113).to_d(-42) }
+  end
+end

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

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