Skip to content

Commit 6ecbd49

Browse files
committed
Allow calling Rational#to_d without arguments
Then the precision would be 0, just like with Float. Apply the same change to Complex, which had a different validation
1 parent dd7738c commit 6ecbd49

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

lib/bigdecimal/util.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,11 @@ class Rational < Numeric
119119
#
120120
# Returns the value as a BigDecimal.
121121
#
122-
# The required +precision+ parameter is used to determine the number of
123-
# significant digits for the result.
122+
# The +precision+ parameter is used to determine the number of
123+
# significant digits for the result. When +precision+ is set to +0+,
124+
# the number of digits to represent the float being converted is determined
125+
# automatically.
126+
# The default +precision+ is +0+.
124127
#
125128
# require 'bigdecimal'
126129
# require 'bigdecimal/util'
@@ -129,7 +132,7 @@ class Rational < Numeric
129132
#
130133
# See also Kernel.BigDecimal.
131134
#
132-
def to_d(precision)
135+
def to_d(precision=0)
133136
BigDecimal(self, precision)
134137
end
135138
end
@@ -141,29 +144,27 @@ class Complex < Numeric
141144
# cmp.to_d(precision) -> bigdecimal
142145
#
143146
# Returns the value as a BigDecimal.
147+
# If the imaginary part is not +0+, an error is raised
144148
#
145-
# The +precision+ parameter is required for a rational complex number.
146-
# This parameter is used to determine the number of significant digits
147-
# for the result.
149+
# The +precision+ parameter is used to determine the number of
150+
# significant digits for the result. When +precision+ is set to +0+,
151+
# the number of digits to represent the float being converted is determined
152+
# automatically.
153+
# The default +precision+ is +0+.
148154
#
149155
# require 'bigdecimal'
150156
# require 'bigdecimal/util'
151157
#
152158
# Complex(0.1234567, 0).to_d(4) # => 0.1235e0
153159
# Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
160+
# Complex(1, 1).to_d # raises ArgumentError
154161
#
155162
# See also Kernel.BigDecimal.
156163
#
157-
def to_d(*args)
164+
def to_d(precision=0)
158165
BigDecimal(self) unless self.imag.zero? # to raise error
159166

160-
if args.length == 0
161-
case self.real
162-
when Rational
163-
BigDecimal(self.real) # to raise error
164-
end
165-
end
166-
self.real.to_d(*args)
167+
BigDecimal(self.real, precision)
167168
end
168169
end
169170

test/bigdecimal/test_bigdecimal_util.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ def test_Rational_to_d
8484
assert(355.quo(113).to_d(digits).frozen?)
8585
end
8686

87+
def test_Rational_to_d_without_precision
88+
assert_equal(BigDecimal("1.25"), Rational(5, 4).to_d)
89+
assert_equal(BigDecimal(355.quo(113), 0), 355.quo(113).to_d)
90+
end
91+
8792
def test_Rational_to_d_with_zero_precision
8893
assert_equal(BigDecimal(355.quo(113), 0), 355.quo(113).to_d(0))
8994
end
@@ -102,7 +107,7 @@ def test_Complex_to_d
102107
assert_equal(BigDecimal("0.1234567"), Complex(0.1234567, 0).to_d)
103108
assert_equal(BigDecimal("0.1235"), Complex(0.1234567, 0).to_d(4))
104109

105-
assert_raise_with_message(ArgumentError, "can't omit precision for a Rational.") { Complex(1.quo(3), 0).to_d }
110+
assert_equal(BigDecimal("0.5"), Complex(1.quo(2), 0).to_d)
106111

107112
assert_raise_with_message(ArgumentError, "Unable to make a BigDecimal from non-zero imaginary number") { Complex(1, 1).to_d }
108113
end

0 commit comments

Comments
 (0)