зеркало из https://github.com/github/ruby.git
* ext/bigdecimal/lib/bigdecimal/util.rb (Rational#to_d):
zero or negative precision is error. fixes #5098. [ruby-dev:44210] * test/bigdecimal/test_bigdecimal_util.rb: add test for the above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32680 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
079fb8d4c3
Коммит
dca0b41777
|
@ -1,3 +1,12 @@
|
|||
Wed Jul 27 00:50:00 2011 Kenta Murata <mrkn@mrkn.jp>
|
||||
|
||||
* ext/bigdecimal/lib/bigdecimal/util.rb (Rational#to_d):
|
||||
zero or negative precision is error. fixes #5098.
|
||||
[ruby-dev:44210]
|
||||
|
||||
* test/bigdecimal/test_bigdecimal_util.rb: add test for the above
|
||||
change.
|
||||
|
||||
Wed Jul 27 00:48:00 2011 Kenta Murata <mrkn@mrkn.jp>
|
||||
|
||||
* ext/bigdecimal/lib/bigdecimal/util.rb (Float#to_d): modified for
|
||||
|
|
|
@ -95,11 +95,11 @@ class Rational < Numeric
|
|||
# # => #<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
|
||||
|
|
|
@ -25,4 +25,19 @@ class TestBigDecimalUtil < Test::Unit::TestCase
|
|||
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
|
||||
|
|
Загрузка…
Ссылка в новой задаче