@@ -44,7 +44,7 @@ def **(y)
4444 def power ( y , prec = nil )
4545 BigMath . _validate_prec ( prec , :power ) if prec
4646 x = self
47- y = BigMath . _coerce_to_bigdecimal ( y , :power )
47+ y = BigMath . _coerce_to_bigdecimal ( y , prec || 0 , :power )
4848
4949 return BigMath . _nan_computation_result if x . nan? || y . nan?
5050 return BigDecimal ( 1 ) if y . zero?
@@ -145,12 +145,17 @@ def power(y, prec = nil)
145145# Core BigMath methods for BigDecimal (log, exp) are defined here.
146146# Other methods (sin, cos, atan) are defined in 'bigdecimal/math.rb'.
147147module BigMath
148- def self . _coerce_to_bigdecimal ( x , method_name , complex_domain_error = false ) # :nodoc:
148+
149+ # Coerce x to BigDecimal with the specified precision.
150+ # TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
151+ def self . _coerce_to_bigdecimal ( x , prec , method_name , complex_domain_error = false ) # :nodoc:
149152 case x
150153 when BigDecimal
151154 return x
152- when Integer , Float , Rational
153- return BigDecimal ( x , 0 )
155+ when Integer , Float
156+ return BigDecimal ( x )
157+ when Rational
158+ return BigDecimal ( x , [ prec , 2 * BigDecimal . double_fig ] . max )
154159 when Complex
155160 if complex_domain_error
156161 raise Math ::DomainError , "Complex argument for BigMath.#{ method_name } "
@@ -192,7 +197,7 @@ def self._nan_computation_result # :nodoc:
192197 #
193198 def self . log ( x , prec )
194199 _validate_prec ( prec , :log )
195- x = _coerce_to_bigdecimal ( x , :log , true )
200+ x = _coerce_to_bigdecimal ( x , prec , :log , true )
196201 return _nan_computation_result if x . nan?
197202 raise Math ::DomainError , 'Zero or negative argument for log' if x <= 0
198203 return _infinity_computation_result if x . infinite?
@@ -258,7 +263,7 @@ def self.log(x, prec)
258263 #
259264 def self . exp ( x , prec )
260265 _validate_prec ( prec , :exp )
261- x = _coerce_to_bigdecimal ( x , :exp )
266+ x = _coerce_to_bigdecimal ( x , prec , :exp )
262267 return _nan_computation_result if x . nan?
263268 return x . positive? ? _infinity_computation_result : BigDecimal ( 0 ) if x . infinite?
264269 return BigDecimal ( 1 ) if x . zero?
0 commit comments