ruby-changes:64339
From: Kenta <ko1@a...>
Date: Sat, 19 Dec 2020 22:14:41 +0900 (JST)
Subject: [ruby-changes:64339] e1424c3501 (master): [bigdecimal] Fix the default precision of Float#to_d
https://git.ruby-lang.org/ruby.git/commit/?id=e1424c3501 From e1424c35012ccf1f3822c9046081b225aef4142e Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@m...> Date: Fri, 18 Dec 2020 23:46:26 +0900 Subject: [bigdecimal] Fix the default precision of Float#to_d Fix https://github.com/ruby/bigdecimal/issues/70 [Bug #13331] https://github.com/ruby/bigdecimal/commit/aa536cd4b5 diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 2b3c0bb..8875682 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -1278,7 +1278,7 @@ BigDecimal_mult(VALUE self, VALUE r) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L1278 GUARD_OBJ(a, GetVpValue(self, 1)); if (RB_TYPE_P(r, T_FLOAT)) { - b = GetVpValueWithPrec(r, DBL_DIG+1, 1); + b = GetVpValueWithPrec(r, DBLE_FIG, 1); } else if (RB_TYPE_P(r, T_RATIONAL)) { b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1); diff --git a/ext/bigdecimal/lib/bigdecimal/util.rb b/ext/bigdecimal/lib/bigdecimal/util.rb index 66fff78..00a3e96 100644 --- a/ext/bigdecimal/lib/bigdecimal/util.rb +++ b/ext/bigdecimal/lib/bigdecimal/util.rb @@ -43,7 +43,7 @@ class Float < Numeric https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/lib/bigdecimal/util.rb#L43 # # See also BigDecimal::new. # - def to_d(precision=Float::DIG) + def to_d(precision=Float::DIG+1) BigDecimal(self, precision) end end diff --git a/test/bigdecimal/test_bigdecimal_util.rb b/test/bigdecimal/test_bigdecimal_util.rb index 7c0830e..b855fd5 100644 --- a/test/bigdecimal/test_bigdecimal_util.rb +++ b/test/bigdecimal/test_bigdecimal_util.rb @@ -17,10 +17,12 @@ class TestBigDecimalUtil < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal_util.rb#L17 end def test_Float_to_d_without_precision - delta = 1.0/10**(Float::DIG) - assert_in_delta(BigDecimal(0.5, Float::DIG), 0.5.to_d, delta) - assert_in_delta(BigDecimal(355.0/113.0, Float::DIG), (355.0/113.0).to_d, delta) - assert_equal(9.05.to_d.to_s('F'), "9.05") + 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) + + assert_equal(9.05, 9.05.to_d.to_f) + assert_equal("9.050000000000001", 9.05.to_d.to_s('F')) bug9214 = '[ruby-core:58858]' assert_equal((-0.0).to_d.sign, -1, bug9214) @@ -43,6 +45,12 @@ class TestBigDecimalUtil < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal_util.rb#L45 assert(1.1.to_d(digits).frozen?) end + def test_Float_to_d_bug13331 + assert_equal(64.4.to_d, + 1.to_d * 64.4, + "[ruby-core:80234] [Bug #13331]") + end + def test_Rational_to_d digits = 100 delta = 1.0/10**(digits) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/