Skip to content

Commit 2066c20

Browse files
authored
BigMath methods common interface: coerce x, validate prec, check nan error (#415)
1 parent 92a9d7e commit 2066c20

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

lib/bigdecimal/math.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ module BigMath
4141
# #=> "0.1414213562373095048801688724e1"
4242
#
4343
def sqrt(x, prec)
44+
BigDecimal::Internal.validate_prec(prec, :sqrt)
45+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :sqrt)
4446
x.sqrt(prec)
4547
end
4648

@@ -83,8 +85,9 @@ def sqrt(x, prec)
8385
# #=> "0.70710678118654752440082036563292800375e0"
8486
#
8587
def sin(x, prec)
86-
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
87-
return BigDecimal("NaN") if x.infinite? || x.nan?
88+
BigDecimal::Internal.validate_prec(prec, :sin)
89+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :sin)
90+
return BigDecimal::Internal.nan_computation_result if x.infinite? || x.nan?
8891
n = prec + BigDecimal.double_fig
8992
one = BigDecimal("1")
9093
two = BigDecimal("2")
@@ -119,8 +122,9 @@ def sin(x, prec)
119122
# #=> "-0.999999999999999999999999999999856613163740061349e0"
120123
#
121124
def cos(x, prec)
122-
raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
123-
return BigDecimal("NaN") if x.infinite? || x.nan?
125+
BigDecimal::Internal.validate_prec(prec, :cos)
126+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :cos)
127+
return BigDecimal::Internal.nan_computation_result if x.infinite? || x.nan?
124128
sign, x = _sin_periodic_reduction(x, prec + BigDecimal.double_fig, add_half_pi: true)
125129
sign * sin(x, prec)
126130
end
@@ -140,6 +144,7 @@ def cos(x, prec)
140144
# #=> "0.99999999999999999999419869652481995799388629632650769e0"
141145
#
142146
def tan(x, prec)
147+
BigDecimal::Internal.validate_prec(prec, :tan)
143148
sin(x, prec) / cos(x, prec)
144149
end
145150

@@ -155,8 +160,9 @@ def tan(x, prec)
155160
# #=> "-0.785398163397448309615660845819878471907514682065e0"
156161
#
157162
def atan(x, prec)
158-
raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
159-
return BigDecimal("NaN") if x.nan?
163+
BigDecimal::Internal.validate_prec(prec, :atan)
164+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :atan)
165+
return BigDecimal::Internal.nan_computation_result if x.nan?
160166
pi = PI(prec)
161167
x = -x if neg = x < 0
162168
return pi.div(neg ? -2 : 2, prec) if x.infinite?
@@ -192,7 +198,7 @@ def atan(x, prec)
192198
# #=> "0.3141592653589793238462643388813853786957412e1"
193199
#
194200
def PI(prec)
195-
raise ArgumentError, "Zero or negative precision for PI" if prec <= 0
201+
BigDecimal::Internal.validate_prec(prec, :PI)
196202
n = prec + BigDecimal.double_fig
197203
zero = BigDecimal("0")
198204
one = BigDecimal("1")
@@ -237,7 +243,7 @@ def PI(prec)
237243
# #=> "0.271828182845904523536028752390026306410273e1"
238244
#
239245
def E(prec)
240-
raise ArgumentError, "Zero or negative precision for E" if prec <= 0
246+
BigDecimal::Internal.validate_prec(prec, :E)
241247
BigMath.exp(1, prec)
242248
end
243249
end

test/bigdecimal/test_bigdecimal.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2484,7 +2484,8 @@ def test_sqrt_exp_log_pow_with_limit
24842484
BigDecimal.save_limit do
24852485
BigDecimal.limit(limit)
24862486
assert_equal(sqrt, BigMath.sqrt(x, prec))
2487-
assert_equal(sqrt_lim, BigMath.sqrt(x, 0))
2487+
assert_equal(sqrt, x.sqrt(prec))
2488+
assert_equal(sqrt_lim, x.sqrt(0))
24882489
assert_equal(exp, BigMath.exp(x, prec))
24892490
assert_equal(log, BigMath.log(x, prec))
24902491
assert_equal(pow, x.power(y, prec))

0 commit comments

Comments
 (0)