bigdecimal 3.2.2-java → 3.2.3-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bigdecimal/math.rb +9 -6
- data/lib/bigdecimal.rb +324 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72ea230b182767a2c32bc9dafe2cb3b39165960ca8683b5afaab7dd9cfcae7fb
|
4
|
+
data.tar.gz: f34cbd33b43a2ca0441d1f9b070fee1d27dc3ad9bba3e0df1f6774d688d18b68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55cb0f29873b8685d9cfeb14627fe9ecfed5e735a08cd82730da1d736d83b0f630f48e4ee0df164ba799ac0e54dbfbea629712b131d5e9355d07b4ed755edabb
|
7
|
+
data.tar.gz: 8d0ac09e425579924920109a14290858b03bc70cd66ec3acae334593df0b786e922220a683f20bf2bd35900d1e5ebca1aa9cd3ed0463b20edc34ec3fbce2f01c
|
data/lib/bigdecimal/math.rb
CHANGED
@@ -61,7 +61,8 @@ module BigMath
|
|
61
61
|
one = BigDecimal("1")
|
62
62
|
two = BigDecimal("2")
|
63
63
|
x = -x if neg = x < 0
|
64
|
-
if x >
|
64
|
+
if x > 6
|
65
|
+
twopi = two * BigMath.PI(prec + x.exponent)
|
65
66
|
if x > 30
|
66
67
|
x %= twopi
|
67
68
|
else
|
@@ -84,6 +85,7 @@ module BigMath
|
|
84
85
|
d = sign * x1.div(z,m)
|
85
86
|
y += d
|
86
87
|
end
|
88
|
+
y = BigDecimal("1") if y > 1
|
87
89
|
neg ? -y : y
|
88
90
|
end
|
89
91
|
|
@@ -105,7 +107,8 @@ module BigMath
|
|
105
107
|
one = BigDecimal("1")
|
106
108
|
two = BigDecimal("2")
|
107
109
|
x = -x if x < 0
|
108
|
-
if x >
|
110
|
+
if x > 6
|
111
|
+
twopi = two * BigMath.PI(prec + x.exponent)
|
109
112
|
if x > 30
|
110
113
|
x %= twopi
|
111
114
|
else
|
@@ -128,7 +131,7 @@ module BigMath
|
|
128
131
|
d = sign * x1.div(z,m)
|
129
132
|
y += d
|
130
133
|
end
|
131
|
-
y
|
134
|
+
y < -1 ? BigDecimal("-1") : y > 1 ? BigDecimal("1") : y
|
132
135
|
end
|
133
136
|
|
134
137
|
# call-seq:
|
@@ -149,9 +152,9 @@ module BigMath
|
|
149
152
|
x = -x if neg = x < 0
|
150
153
|
return pi.div(neg ? -2 : 2, prec) if x.infinite?
|
151
154
|
return pi / (neg ? -4 : 4) if x.round(prec) == 1
|
152
|
-
|
153
|
-
x = (
|
154
|
-
|
155
|
+
n = prec + BigDecimal.double_fig
|
156
|
+
x = BigDecimal("1").div(x, n) if inv = x > 1
|
157
|
+
x = (-1 + sqrt(1 + x.mult(x, n), n)).div(x, n) if dbl = x > 0.5
|
155
158
|
y = x
|
156
159
|
d = y
|
157
160
|
t = x
|
data/lib/bigdecimal.rb
CHANGED
@@ -1,5 +1,329 @@
|
|
1
1
|
if RUBY_ENGINE == 'jruby'
|
2
2
|
JRuby::Util.load_ext("org.jruby.ext.bigdecimal.BigDecimalLibrary")
|
3
|
+
return
|
3
4
|
else
|
4
5
|
require 'bigdecimal.so'
|
5
6
|
end
|
7
|
+
|
8
|
+
class BigDecimal
|
9
|
+
module Internal # :nodoc:
|
10
|
+
|
11
|
+
# Coerce x to BigDecimal with the specified precision.
|
12
|
+
# TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
|
13
|
+
def self.coerce_to_bigdecimal(x, prec, method_name) # :nodoc:
|
14
|
+
case x
|
15
|
+
when BigDecimal
|
16
|
+
return x
|
17
|
+
when Integer, Float
|
18
|
+
return BigDecimal(x)
|
19
|
+
when Rational
|
20
|
+
return BigDecimal(x, [prec, 2 * BigDecimal.double_fig].max)
|
21
|
+
end
|
22
|
+
raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal"
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.validate_prec(prec, method_name, accept_zero: false) # :nodoc:
|
26
|
+
raise ArgumentError, 'precision must be an Integer' unless Integer === prec
|
27
|
+
if accept_zero
|
28
|
+
raise ArgumentError, "Negative precision for #{method_name}" if prec < 0
|
29
|
+
else
|
30
|
+
raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.infinity_computation_result # :nodoc:
|
35
|
+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_INFINITY)
|
36
|
+
raise FloatDomainError, "Computation results in 'Infinity'"
|
37
|
+
end
|
38
|
+
BigDecimal::INFINITY
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.nan_computation_result # :nodoc:
|
42
|
+
if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN)
|
43
|
+
raise FloatDomainError, "Computation results to 'NaN'"
|
44
|
+
end
|
45
|
+
BigDecimal::NAN
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# call-seq:
|
50
|
+
# self ** other -> bigdecimal
|
51
|
+
#
|
52
|
+
# Returns the \BigDecimal value of +self+ raised to power +other+:
|
53
|
+
#
|
54
|
+
# b = BigDecimal('3.14')
|
55
|
+
# b ** 2 # => 0.98596e1
|
56
|
+
# b ** 2.0 # => 0.98596e1
|
57
|
+
# b ** Rational(2, 1) # => 0.98596e1
|
58
|
+
#
|
59
|
+
# Related: BigDecimal#power.
|
60
|
+
#
|
61
|
+
def **(y)
|
62
|
+
case y
|
63
|
+
when BigDecimal, Integer, Float, Rational
|
64
|
+
power(y)
|
65
|
+
when nil
|
66
|
+
raise TypeError, 'wrong argument type NilClass'
|
67
|
+
else
|
68
|
+
x, y = y.coerce(self)
|
69
|
+
x**y
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# call-seq:
|
74
|
+
# power(n)
|
75
|
+
# power(n, prec)
|
76
|
+
#
|
77
|
+
# Returns the value raised to the power of n.
|
78
|
+
#
|
79
|
+
# Also available as the operator **.
|
80
|
+
#
|
81
|
+
def power(y, prec = nil)
|
82
|
+
Internal.validate_prec(prec, :power) if prec
|
83
|
+
x = self
|
84
|
+
y = Internal.coerce_to_bigdecimal(y, prec || n_significant_digits, :power)
|
85
|
+
|
86
|
+
return Internal.nan_computation_result if x.nan? || y.nan?
|
87
|
+
return BigDecimal(1) if y.zero?
|
88
|
+
|
89
|
+
if y.infinite?
|
90
|
+
if x < 0
|
91
|
+
return BigDecimal(0) if x < -1 && y.negative?
|
92
|
+
return BigDecimal(0) if x > -1 && y.positive?
|
93
|
+
raise Math::DomainError, 'Result undefined for negative base raised to infinite power'
|
94
|
+
elsif x < 1
|
95
|
+
return y.positive? ? BigDecimal(0) : BigDecimal::Internal.infinity_computation_result
|
96
|
+
elsif x == 1
|
97
|
+
return BigDecimal(1)
|
98
|
+
else
|
99
|
+
return y.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
if x.infinite? && y < 0
|
104
|
+
# Computation result will be +0 or -0. Avoid overflow.
|
105
|
+
neg = x < 0 && y.frac.zero? && y % 2 == 1
|
106
|
+
return neg ? -BigDecimal(0) : BigDecimal(0)
|
107
|
+
end
|
108
|
+
|
109
|
+
if x.zero?
|
110
|
+
return BigDecimal(1) if y.zero?
|
111
|
+
return BigDecimal(0) if y > 0
|
112
|
+
if y.frac.zero? && y % 2 == 1 && x.sign == -1
|
113
|
+
return -BigDecimal::Internal.infinity_computation_result
|
114
|
+
else
|
115
|
+
return BigDecimal::Internal.infinity_computation_result
|
116
|
+
end
|
117
|
+
elsif x < 0
|
118
|
+
if y.frac.zero?
|
119
|
+
if y % 2 == 0
|
120
|
+
return (-x).power(y, prec)
|
121
|
+
else
|
122
|
+
return -(-x).power(y, prec)
|
123
|
+
end
|
124
|
+
else
|
125
|
+
raise Math::DomainError, 'Computation results in complex number'
|
126
|
+
end
|
127
|
+
elsif x == 1
|
128
|
+
return BigDecimal(1)
|
129
|
+
end
|
130
|
+
|
131
|
+
prec ||= BigDecimal.limit.nonzero?
|
132
|
+
frac_part = y.frac
|
133
|
+
|
134
|
+
if frac_part.zero? && !prec
|
135
|
+
# Infinite precision calculation for `x ** int` and `x.power(int)`
|
136
|
+
int_part = y.fix.to_i
|
137
|
+
int_part = -int_part if (neg = int_part < 0)
|
138
|
+
ans = BigDecimal(1)
|
139
|
+
n = 1
|
140
|
+
xn = x
|
141
|
+
while true
|
142
|
+
ans *= xn if int_part.allbits?(n)
|
143
|
+
n <<= 1
|
144
|
+
break if n > int_part
|
145
|
+
xn *= xn
|
146
|
+
# Detect overflow/underflow before consuming infinite memory
|
147
|
+
if (xn.exponent.abs - 1) * int_part / n >= 0x7FFFFFFFFFFFFFFF
|
148
|
+
return ((xn.exponent > 0) ^ neg ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)) * (int_part.even? || x > 0 ? 1 : -1)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
return neg ? BigDecimal(1) / ans : ans
|
152
|
+
end
|
153
|
+
|
154
|
+
prec ||= [x.n_significant_digits, y.n_significant_digits, BigDecimal.double_fig].max + BigDecimal.double_fig
|
155
|
+
|
156
|
+
if y < 0
|
157
|
+
inv = x.power(-y, prec)
|
158
|
+
return BigDecimal(0) if inv.infinite?
|
159
|
+
return BigDecimal::Internal.infinity_computation_result if inv.zero?
|
160
|
+
return BigDecimal(1).div(inv, prec)
|
161
|
+
end
|
162
|
+
|
163
|
+
int_part = y.fix.to_i
|
164
|
+
prec2 = prec + BigDecimal.double_fig
|
165
|
+
pow_prec = prec2 + (int_part > 0 ? y.exponent : 0)
|
166
|
+
ans = BigDecimal(1)
|
167
|
+
n = 1
|
168
|
+
xn = x
|
169
|
+
while true
|
170
|
+
ans = ans.mult(xn, pow_prec) if int_part.allbits?(n)
|
171
|
+
n <<= 1
|
172
|
+
break if n > int_part
|
173
|
+
xn = xn.mult(xn, pow_prec)
|
174
|
+
end
|
175
|
+
unless frac_part.zero?
|
176
|
+
ans = ans.mult(BigMath.exp(BigMath.log(x, prec2).mult(frac_part, prec2), prec2), prec2)
|
177
|
+
end
|
178
|
+
ans.mult(1, prec)
|
179
|
+
end
|
180
|
+
|
181
|
+
# Returns the square root of the value.
|
182
|
+
#
|
183
|
+
# Result has at least prec significant digits.
|
184
|
+
#
|
185
|
+
def sqrt(prec)
|
186
|
+
Internal.validate_prec(prec, :sqrt, accept_zero: true)
|
187
|
+
return Internal.infinity_computation_result if infinite? == 1
|
188
|
+
|
189
|
+
raise FloatDomainError, 'sqrt of negative value' if self < 0
|
190
|
+
raise FloatDomainError, "sqrt of 'NaN'(Not a Number)" if nan?
|
191
|
+
return self if zero?
|
192
|
+
|
193
|
+
limit = BigDecimal.limit.nonzero? if prec == 0
|
194
|
+
|
195
|
+
# BigDecimal#sqrt calculates at least n_significant_digits precision.
|
196
|
+
# This feature maybe problematic for some cases.
|
197
|
+
n_digits = n_significant_digits
|
198
|
+
prec = [prec, n_digits].max
|
199
|
+
|
200
|
+
ex = exponent / 2
|
201
|
+
x = _decimal_shift(-2 * ex)
|
202
|
+
y = BigDecimal(Math.sqrt(x.to_f))
|
203
|
+
precs = [prec + BigDecimal.double_fig]
|
204
|
+
precs << 2 + precs.last / 2 while precs.last > BigDecimal.double_fig
|
205
|
+
precs.reverse_each do |p|
|
206
|
+
y = y.add(x.div(y, p), p).div(2, p)
|
207
|
+
end
|
208
|
+
y = y.mult(1, limit) if limit
|
209
|
+
y._decimal_shift(ex)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Core BigMath methods for BigDecimal (log, exp) are defined here.
|
214
|
+
# Other methods (sin, cos, atan) are defined in 'bigdecimal/math.rb'.
|
215
|
+
module BigMath
|
216
|
+
|
217
|
+
# call-seq:
|
218
|
+
# BigMath.log(decimal, numeric) -> BigDecimal
|
219
|
+
#
|
220
|
+
# Computes the natural logarithm of +decimal+ to the specified number of
|
221
|
+
# digits of precision, +numeric+.
|
222
|
+
#
|
223
|
+
# If +decimal+ is zero or negative, raises Math::DomainError.
|
224
|
+
#
|
225
|
+
# If +decimal+ is positive infinity, returns Infinity.
|
226
|
+
#
|
227
|
+
# If +decimal+ is NaN, returns NaN.
|
228
|
+
#
|
229
|
+
def self.log(x, prec)
|
230
|
+
BigDecimal::Internal.validate_prec(prec, :log)
|
231
|
+
raise Math::DomainError, 'Complex argument for BigMath.log' if Complex === x
|
232
|
+
|
233
|
+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :log)
|
234
|
+
return BigDecimal::Internal.nan_computation_result if x.nan?
|
235
|
+
raise Math::DomainError, 'Zero or negative argument for log' if x <= 0
|
236
|
+
return BigDecimal::Internal.infinity_computation_result if x.infinite?
|
237
|
+
return BigDecimal(0) if x == 1
|
238
|
+
|
239
|
+
BigDecimal.save_limit do
|
240
|
+
BigDecimal.limit(0)
|
241
|
+
if x > 10 || x < 0.1
|
242
|
+
log10 = log(BigDecimal(10), prec)
|
243
|
+
exponent = x.exponent
|
244
|
+
x = x._decimal_shift(-exponent)
|
245
|
+
if x < 0.3
|
246
|
+
x *= 10
|
247
|
+
exponent -= 1
|
248
|
+
end
|
249
|
+
return log10 * exponent + log(x, prec)
|
250
|
+
end
|
251
|
+
|
252
|
+
x_minus_one_exponent = (x - 1).exponent
|
253
|
+
prec += BigDecimal.double_fig
|
254
|
+
|
255
|
+
# log(x) = log(sqrt(sqrt(sqrt(sqrt(x))))) * 2**sqrt_steps
|
256
|
+
sqrt_steps = [Integer.sqrt(prec) + 3 * x_minus_one_exponent, 0].max
|
257
|
+
|
258
|
+
lg2 = 0.3010299956639812
|
259
|
+
prec2 = prec + [-x_minus_one_exponent, 0].max + (sqrt_steps * lg2).ceil
|
260
|
+
|
261
|
+
sqrt_steps.times do
|
262
|
+
x = x.sqrt(prec2)
|
263
|
+
|
264
|
+
# Workaround for https://github.com/ruby/bigdecimal/issues/354
|
265
|
+
x = x.mult(1, prec2 + BigDecimal.double_fig)
|
266
|
+
end
|
267
|
+
|
268
|
+
# Taylor series for log(x) around 1
|
269
|
+
# log(x) = -log((1 + X) / (1 - X)) where X = (x - 1) / (x + 1)
|
270
|
+
# log(x) = 2 * (X + X**3 / 3 + X**5 / 5 + X**7 / 7 + ...)
|
271
|
+
x = (x - 1).div(x + 1, prec2)
|
272
|
+
y = x
|
273
|
+
x2 = x.mult(x, prec)
|
274
|
+
1.step do |i|
|
275
|
+
n = prec + x.exponent - y.exponent + x2.exponent
|
276
|
+
break if n <= 0 || x.zero?
|
277
|
+
x = x.mult(x2.round(n - x2.exponent), n)
|
278
|
+
y = y.add(x.div(2 * i + 1, n), prec)
|
279
|
+
end
|
280
|
+
|
281
|
+
y.mult(2 ** (sqrt_steps + 1), prec)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
# call-seq:
|
286
|
+
# BigMath.exp(decimal, numeric) -> BigDecimal
|
287
|
+
#
|
288
|
+
# Computes the value of e (the base of natural logarithms) raised to the
|
289
|
+
# power of +decimal+, to the specified number of digits of precision.
|
290
|
+
#
|
291
|
+
# If +decimal+ is infinity, returns Infinity.
|
292
|
+
#
|
293
|
+
# If +decimal+ is NaN, returns NaN.
|
294
|
+
#
|
295
|
+
def self.exp(x, prec)
|
296
|
+
BigDecimal::Internal.validate_prec(prec, :exp)
|
297
|
+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
|
298
|
+
return BigDecimal::Internal.nan_computation_result if x.nan?
|
299
|
+
return x.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0) if x.infinite?
|
300
|
+
return BigDecimal(1) if x.zero?
|
301
|
+
return BigDecimal(1).div(exp(-x, prec), prec) if x < 0
|
302
|
+
|
303
|
+
# exp(x * 10**cnt) = exp(x)**(10**cnt)
|
304
|
+
cnt = x > 1 ? x.exponent : 0
|
305
|
+
prec2 = prec + BigDecimal.double_fig + cnt
|
306
|
+
x = x._decimal_shift(-cnt)
|
307
|
+
xn = BigDecimal(1)
|
308
|
+
y = BigDecimal(1)
|
309
|
+
|
310
|
+
# Taylor series for exp(x) around 0
|
311
|
+
1.step do |i|
|
312
|
+
n = prec2 + xn.exponent
|
313
|
+
break if n <= 0 || xn.zero?
|
314
|
+
x = x.mult(1, n)
|
315
|
+
xn = xn.mult(x, n).div(i, n)
|
316
|
+
y = y.add(xn, prec2)
|
317
|
+
end
|
318
|
+
|
319
|
+
# calculate exp(x * 10**cnt) from exp(x)
|
320
|
+
# exp(x * 10**k) = exp(x * 10**(k - 1)) ** 10
|
321
|
+
cnt.times do
|
322
|
+
y2 = y.mult(y, prec2)
|
323
|
+
y5 = y2.mult(y2, prec2).mult(y, prec2)
|
324
|
+
y = y5.mult(y5, prec2)
|
325
|
+
end
|
326
|
+
|
327
|
+
y.mult(1, prec)
|
328
|
+
end
|
329
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bigdecimal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.2.
|
4
|
+
version: 3.2.3
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Kenta Murata
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
- Shigeo Kobayashi
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2025-
|
12
|
+
date: 2025-09-03 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: This library provides arbitrary-precision decimal floating-point number
|
15
15
|
class.
|