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

ruby-changes:31994

From: mrkn <ko1@a...>
Date: Sun, 8 Dec 2013 20:20:58 +0900 (JST)
Subject: [ruby-changes:31994] mrkn:r44073 (trunk): * bigdecimal.c (BigDecimal_coerce): convert a Float to a BigDecimal instead

mrkn	2013-12-08 20:20:51 +0900 (Sun, 08 Dec 2013)

  New Revision: 44073

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

  Log:
    * bigdecimal.c (BigDecimal_coerce): convert a Float to a BigDecimal instead
      of converting the receiver to a Float.
      [ruby-core:58756] [Bug #9192]
    
    * test/bigdecimal/test_bigdecimal.rb: add tests for the above change.

  Modified files:
    trunk/ChangeLog
    trunk/ext/bigdecimal/bigdecimal.c
    trunk/test/bigdecimal/test_bigdecimal.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44072)
+++ ChangeLog	(revision 44073)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Dec  8 20:21:00 2013  Kenta Murata  <mrkn@m...>
+
+	* bigdecimal.c (BigDecimal_coerce): convert a Float to a BigDecimal instead
+	  of converting the receiver to a Float.
+	  [ruby-core:58756] [Bug #9192]
+
+	* test/bigdecimal/test_bigdecimal.rb: add tests for the above change.
+
 Sun Dec  8 18:28:20 2013  Kazuki Tsujimoto  <kazuki@c...>
 
 	* NEWS: [DOC] update NEWS about GC.
Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 44072)
+++ ext/bigdecimal/bigdecimal.c	(revision 44073)
@@ -797,7 +797,8 @@ BigDecimal_coerce(VALUE self, VALUE othe https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L797
     Real *b;
 
     if (RB_TYPE_P(other, T_FLOAT)) {
-	obj = rb_assoc_new(other, BigDecimal_to_f(self));
+	GUARD_OBJ(b, GetVpValueWithPrec(other, DBL_DIG+1, 1));
+	obj = rb_assoc_new(ToValue(b), self);
     }
     else {
 	if (RB_TYPE_P(other, T_RATIONAL)) {
Index: test/bigdecimal/test_bigdecimal.rb
===================================================================
--- test/bigdecimal/test_bigdecimal.rb	(revision 44072)
+++ test/bigdecimal/test_bigdecimal.rb	(revision 44073)
@@ -438,6 +438,20 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L438
     assert_equal(0, BigDecimal("1E-1") <=> 10**(-1), '#4825')
   end
 
+  def test_cmp_issue9192
+    bug9192 = '[ruby-core:58756] [#9192]'
+    operators = { :== => :==, :< => :>, :> => :<, :<= => :>=, :>= => :<= }
+    5.upto(8) do |i|
+      s = "706.0#{i}"
+      d = BigDecimal(s)
+      f = s.to_f
+      operators.each do |op, inv|
+        assert_equal(d.send(op, f), f.send(inv, d),
+                     "(BigDecimal(#{s.inspect}) #{op} #{s}) and (#{s} #{inv} BigDecimal(#{s.inspect})) is different #{bug9192}")
+      end
+    end
+  end
+
   def test_cmp_nan
     n1 = BigDecimal.new("1")
     BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
@@ -608,8 +622,8 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L622
 
   def test_coerce
     a, b = BigDecimal.new("1").coerce(1.0)
-    assert_instance_of(Float, a)
-    assert_instance_of(Float, b)
+    assert_instance_of(BigDecimal, a)
+    assert_instance_of(BigDecimal, b)
     assert_equal(2, 1 + BigDecimal.new("1"), '[ruby-core:25697]')
 
     a, b = BigDecimal("1").coerce(1.quo(10))
@@ -1462,7 +1476,7 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1476
 
   def test_BigMath_log_with_101
     # this is mainly a performance test (should be very fast, not the 0.3 s)
-    assert_in_delta(Math.log(101), BigMath.log(101, 20), 1E-20)
+    assert_in_delta(Math.log(101), BigMath.log(101, 20), 1E-15)
   end
 
   def test_BigMath_log_with_reciprocal_of_42

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

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