Skip to content

Commit dcb6609

Browse files
committed
Use common prec validation also in BigDecimal#sqrt
1 parent a5a99a7 commit dcb6609

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/bigdecimal.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ def power(y, prec = nil)
146146
# Result has at least prec significant digits.
147147
#
148148
def sqrt(prec)
149+
BigMath._validate_prec(prec, :sqrt, accept_zero: true)
149150
if infinite? == 1
150151
exception_mode = BigDecimal.mode(BigDecimal::EXCEPTION_ALL)
151152
raise FloatDomainError, "Computation results in 'Infinity'" if exception_mode.anybits?(BigDecimal::EXCEPTION_INFINITY)
152153
return INFINITY
153154
end
154-
raise ArgumentError, 'negative precision' if prec < 0
155155
raise FloatDomainError, 'sqrt of negative value' if self < 0
156156
raise FloatDomainError, "sqrt of 'NaN'(Not a Number)" if nan?
157157
return self if zero?
@@ -190,9 +190,13 @@ def self._coerce_to_bigdecimal(x, method_name, complex_domain_error = false) # :
190190
raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal"
191191
end
192192

193-
def self._validate_prec(prec, method_name) # :nodoc:
193+
def self._validate_prec(prec, method_name, accept_zero: false) # :nodoc:
194194
raise ArgumentError, 'precision must be an Integer' unless Integer === prec
195-
raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
195+
if accept_zero
196+
raise ArgumentError, "Negative precision for #{method_name}" if prec < 0
197+
else
198+
raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
199+
end
196200
end
197201

198202
def self._infinity_computation_result # :nodoc:

0 commit comments

Comments
 (0)