Skip to content

Commit 48e32bc

Browse files
authored
Hide internal method of BigMath into BigDecimal::Internal (#400)
Some internal methods (ex: BigMath._validate_prec) are also used from BigDecimal, so we can't make it private. Moves them to an internal module BigDecimal::Internal.
1 parent beb3e1e commit 48e32bc

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

lib/bigdecimal.rb

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,41 @@
55
end
66

77
class BigDecimal
8+
module Internal # :nodoc:
9+
10+
# Coerce x to BigDecimal with the specified precision.
11+
# TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
12+
def self.coerce_to_bigdecimal(x, prec, method_name) # :nodoc:
13+
case x
14+
when BigDecimal
15+
return x
16+
when Integer, Float
17+
return BigDecimal(x)
18+
when Rational
19+
return BigDecimal(x, [prec, 2 * BigDecimal.double_fig].max)
20+
end
21+
raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal"
22+
end
23+
24+
def self.validate_prec(prec, method_name) # :nodoc:
25+
raise ArgumentError, 'precision must be an Integer' unless Integer === prec
26+
raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
27+
end
28+
29+
def self.infinity_computation_result # :nodoc:
30+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_INFINITY)
31+
raise FloatDomainError, "Computation results in 'Infinity'"
32+
end
33+
BigDecimal::INFINITY
34+
end
35+
36+
def self.nan_computation_result # :nodoc:
37+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN)
38+
raise FloatDomainError, "Computation results to 'NaN'"
39+
end
40+
BigDecimal::NAN
41+
end
42+
end
843

944
# call-seq:
1045
# self ** other -> bigdecimal
@@ -39,11 +74,11 @@ def **(y)
3974
# Also available as the operator **.
4075
#
4176
def power(y, prec = nil)
42-
BigMath._validate_prec(prec, :power) if prec
77+
Internal.validate_prec(prec, :power) if prec
4378
x = self
44-
y = BigMath._coerce_to_bigdecimal(y, prec || n_significant_digits, :power)
79+
y = Internal.coerce_to_bigdecimal(y, prec || n_significant_digits, :power)
4580

46-
return BigMath._nan_computation_result if x.nan? || y.nan?
81+
return Internal.nan_computation_result if x.nan? || y.nan?
4782
return BigDecimal(1) if y.zero?
4883

4984
if y.infinite?
@@ -52,11 +87,11 @@ def power(y, prec = nil)
5287
return BigDecimal(0) if x > -1 && y.positive?
5388
raise Math::DomainError, 'Result undefined for negative base raised to infinite power'
5489
elsif x < 1
55-
return y.positive? ? BigDecimal(0) : BigMath._infinity_computation_result
90+
return y.positive? ? BigDecimal(0) : BigDecimal::Internal.infinity_computation_result
5691
elsif x == 1
5792
return BigDecimal(1)
5893
else
59-
return y.positive? ? BigMath._infinity_computation_result : BigDecimal(0)
94+
return y.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)
6095
end
6196
end
6297

@@ -70,9 +105,9 @@ def power(y, prec = nil)
70105
return BigDecimal(1) if y.zero?
71106
return BigDecimal(0) if y > 0
72107
if y.frac.zero? && y % 2 == 1 && x.sign == -1
73-
return -BigMath._infinity_computation_result
108+
return -BigDecimal::Internal.infinity_computation_result
74109
else
75-
return BigMath._infinity_computation_result
110+
return BigDecimal::Internal.infinity_computation_result
76111
end
77112
elsif x < 0
78113
if y.frac.zero?
@@ -105,7 +140,7 @@ def power(y, prec = nil)
105140
xn *= xn
106141
# Detect overflow/underflow before consuming infinite memory
107142
if (xn.exponent.abs - 1) * int_part / n >= 0x7FFFFFFFFFFFFFFF
108-
return ((xn.exponent > 0) ^ neg ? BigMath._infinity_computation_result : BigDecimal(0)) * (int_part.even? || x > 0 ? 1 : -1)
143+
return ((xn.exponent > 0) ^ neg ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)) * (int_part.even? || x > 0 ? 1 : -1)
109144
end
110145
end
111146
return neg ? BigDecimal(1) / ans : ans
@@ -116,7 +151,7 @@ def power(y, prec = nil)
116151
if y < 0
117152
inv = x.power(-y, prec)
118153
return BigDecimal(0) if inv.infinite?
119-
return BigMath._infinity_computation_result if inv.zero?
154+
return BigDecimal::Internal.infinity_computation_result if inv.zero?
120155
return BigDecimal(1).div(inv, prec)
121156
end
122157

@@ -143,43 +178,6 @@ def power(y, prec = nil)
143178
# Other methods (sin, cos, atan) are defined in 'bigdecimal/math.rb'.
144179
module BigMath
145180

146-
# Coerce x to BigDecimal with the specified precision.
147-
# TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
148-
def self._coerce_to_bigdecimal(x, prec, method_name, complex_domain_error = false) # :nodoc:
149-
case x
150-
when BigDecimal
151-
return x
152-
when Integer, Float
153-
return BigDecimal(x)
154-
when Rational
155-
return BigDecimal(x, [prec, 2 * BigDecimal.double_fig].max)
156-
when Complex
157-
if complex_domain_error
158-
raise Math::DomainError, "Complex argument for BigMath.#{method_name}"
159-
end
160-
end
161-
raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal"
162-
end
163-
164-
def self._validate_prec(prec, method_name) # :nodoc:
165-
raise ArgumentError, 'precision must be an Integer' unless Integer === prec
166-
raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
167-
end
168-
169-
def self._infinity_computation_result # :nodoc:
170-
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_INFINITY)
171-
raise FloatDomainError, "Computation results in 'Infinity'"
172-
end
173-
BigDecimal::INFINITY
174-
end
175-
176-
def self._nan_computation_result # :nodoc:
177-
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN)
178-
raise FloatDomainError, "Computation results to 'NaN'"
179-
end
180-
BigDecimal::NAN
181-
end
182-
183181
# call-seq:
184182
# BigMath.log(decimal, numeric) -> BigDecimal
185183
#
@@ -193,11 +191,13 @@ def self._nan_computation_result # :nodoc:
193191
# If +decimal+ is NaN, returns NaN.
194192
#
195193
def self.log(x, prec)
196-
_validate_prec(prec, :log)
197-
x = _coerce_to_bigdecimal(x, prec, :log, true)
198-
return _nan_computation_result if x.nan?
194+
BigDecimal::Internal.validate_prec(prec, :log)
195+
raise Math::DomainError, 'Complex argument for BigMath.log' if Complex === x
196+
197+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :log)
198+
return BigDecimal::Internal.nan_computation_result if x.nan?
199199
raise Math::DomainError, 'Zero or negative argument for log' if x <= 0
200-
return _infinity_computation_result if x.infinite?
200+
return BigDecimal::Internal.infinity_computation_result if x.infinite?
201201
return BigDecimal(0) if x == 1
202202

203203
if x > 10 || x < 0.1
@@ -259,10 +259,10 @@ def self.log(x, prec)
259259
# If +decimal+ is NaN, returns NaN.
260260
#
261261
def self.exp(x, prec)
262-
_validate_prec(prec, :exp)
263-
x = _coerce_to_bigdecimal(x, prec, :exp)
264-
return _nan_computation_result if x.nan?
265-
return x.positive? ? _infinity_computation_result : BigDecimal(0) if x.infinite?
262+
BigDecimal::Internal.validate_prec(prec, :exp)
263+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
264+
return BigDecimal::Internal.nan_computation_result if x.nan?
265+
return x.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0) if x.infinite?
266266
return BigDecimal(1) if x.zero?
267267
return BigDecimal(1).div(exp(-x, prec), prec) if x < 0
268268

0 commit comments

Comments
 (0)