@@ -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
135138end
@@ -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
168169end
169170
0 commit comments