bigdecimal 3.2.2 → 4.0.1

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.
@@ -2,6 +2,8 @@
2
2
  require "bigdecimal/ludcmp"
3
3
  require "bigdecimal/jacobian"
4
4
 
5
+ warn "'bigdecimal/newton' is deprecated and will be removed in a future release."
6
+
5
7
  #
6
8
  # newton.rb
7
9
  #
@@ -119,8 +119,11 @@ class Rational < Numeric
119
119
  #
120
120
  # Returns the value as a BigDecimal.
121
121
  #
122
- # The required +precision+ parameter is used to determine the number of
123
- # significant digits for the result.
122
+ # The +precision+ parameter is used to determine the number of
123
+ # significant digits for the result. When +precision+ is set to +0+,
124
+ # the number of digits to represent the float being converted is determined
125
+ # automatically.
126
+ # The default +precision+ is +0+.
124
127
  #
125
128
  # require 'bigdecimal'
126
129
  # require 'bigdecimal/util'
@@ -129,7 +132,7 @@ class Rational < Numeric
129
132
  #
130
133
  # See also Kernel.BigDecimal.
131
134
  #
132
- def to_d(precision)
135
+ def to_d(precision=0)
133
136
  BigDecimal(self, precision)
134
137
  end
135
138
  end
@@ -141,29 +144,27 @@ class Complex < Numeric
141
144
  # cmp.to_d(precision) -> bigdecimal
142
145
  #
143
146
  # Returns the value as a BigDecimal.
147
+ # If the imaginary part is not +0+, an error is raised
144
148
  #
145
- # The +precision+ parameter is required for a rational complex number.
146
- # This parameter is used to determine the number of significant digits
147
- # for the result.
149
+ # The +precision+ parameter is used to determine the number of
150
+ # significant digits for the result. When +precision+ is set to +0+,
151
+ # the number of digits to represent the float being converted is determined
152
+ # automatically.
153
+ # The default +precision+ is +0+.
148
154
  #
149
155
  # require 'bigdecimal'
150
156
  # require 'bigdecimal/util'
151
157
  #
152
158
  # Complex(0.1234567, 0).to_d(4) # => 0.1235e0
153
159
  # Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
160
+ # Complex(1, 1).to_d # raises ArgumentError
154
161
  #
155
162
  # See also Kernel.BigDecimal.
156
163
  #
157
- def to_d(*args)
164
+ def to_d(precision=0)
158
165
  BigDecimal(self) unless self.imag.zero? # to raise error
159
166
 
160
- if args.length == 0
161
- case self.real
162
- when Rational
163
- BigDecimal(self.real) # to raise error
164
- end
165
- end
166
- self.real.to_d(*args)
167
+ BigDecimal(self.real, precision)
167
168
  end
168
169
  end
169
170
 
data/lib/bigdecimal.rb CHANGED
@@ -1,5 +1,360 @@
1
1
  if RUBY_ENGINE == 'jruby'
2
2
  JRuby::Util.load_ext("org.jruby.ext.bigdecimal.BigDecimalLibrary")
3
+
4
+ class BigDecimal
5
+ def _decimal_shift(i) # :nodoc:
6
+ to_java.move_point_right(i).to_d
7
+ end
8
+ end
3
9
  else
4
10
  require 'bigdecimal.so'
5
11
  end
12
+
13
+ class BigDecimal
14
+ module Internal # :nodoc:
15
+
16
+ # Coerce x to BigDecimal with the specified precision.
17
+ # TODO: some methods (example: BigMath.exp) require more precision than specified to coerce.
18
+ def self.coerce_to_bigdecimal(x, prec, method_name) # :nodoc:
19
+ case x
20
+ when BigDecimal
21
+ return x
22
+ when Integer, Float
23
+ return BigDecimal(x, 0)
24
+ when Rational
25
+ return BigDecimal(x, [prec, 2 * BigDecimal.double_fig].max)
26
+ end
27
+ raise ArgumentError, "#{x.inspect} can't be coerced into BigDecimal"
28
+ end
29
+
30
+ def self.coerce_validate_prec(prec, method_name, accept_zero: false) # :nodoc:
31
+ unless Integer === prec
32
+ original = prec
33
+ # Emulate Integer.try_convert for ruby < 3.1
34
+ if prec.respond_to?(:to_int)
35
+ prec = prec.to_int
36
+ else
37
+ raise TypeError, "no implicit conversion of #{original.class} into Integer"
38
+ end
39
+ raise TypeError, "can't convert #{original.class} to Integer" unless Integer === prec
40
+ end
41
+
42
+ if accept_zero
43
+ raise ArgumentError, "Negative precision for #{method_name}" if prec < 0
44
+ else
45
+ raise ArgumentError, "Zero or negative precision for #{method_name}" if prec <= 0
46
+ end
47
+ prec
48
+ end
49
+
50
+ def self.infinity_computation_result # :nodoc:
51
+ if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_INFINITY)
52
+ raise FloatDomainError, "Computation results in 'Infinity'"
53
+ end
54
+ BigDecimal::INFINITY
55
+ end
56
+
57
+ def self.nan_computation_result # :nodoc:
58
+ if BigDecimal.mode(BigDecimal::EXCEPTION_ALL).anybits?(BigDecimal::EXCEPTION_NaN)
59
+ raise FloatDomainError, "Computation results to 'NaN'"
60
+ end
61
+ BigDecimal::NAN
62
+ end
63
+ end
64
+
65
+ # call-seq:
66
+ # self ** other -> bigdecimal
67
+ #
68
+ # Returns the \BigDecimal value of +self+ raised to power +other+:
69
+ #
70
+ # b = BigDecimal('3.14')
71
+ # b ** 2 # => 0.98596e1
72
+ # b ** 2.0 # => 0.98596e1
73
+ # b ** Rational(2, 1) # => 0.98596e1
74
+ #
75
+ # Related: BigDecimal#power.
76
+ #
77
+ def **(y)
78
+ case y
79
+ when BigDecimal, Integer, Float, Rational
80
+ power(y)
81
+ when nil
82
+ raise TypeError, 'wrong argument type NilClass'
83
+ else
84
+ x, y = y.coerce(self)
85
+ x**y
86
+ end
87
+ end
88
+
89
+ # call-seq:
90
+ # power(n)
91
+ # power(n, prec)
92
+ #
93
+ # Returns the value raised to the power of n.
94
+ #
95
+ # Also available as the operator **.
96
+ #
97
+ def power(y, prec = 0)
98
+ prec = Internal.coerce_validate_prec(prec, :power, accept_zero: true)
99
+ x = self
100
+ y = Internal.coerce_to_bigdecimal(y, prec.nonzero? || n_significant_digits, :power)
101
+
102
+ return Internal.nan_computation_result if x.nan? || y.nan?
103
+ return BigDecimal(1) if y.zero?
104
+
105
+ if y.infinite?
106
+ if x < 0
107
+ return BigDecimal(0) if x < -1 && y.negative?
108
+ return BigDecimal(0) if x > -1 && y.positive?
109
+ raise Math::DomainError, 'Result undefined for negative base raised to infinite power'
110
+ elsif x < 1
111
+ return y.positive? ? BigDecimal(0) : BigDecimal::Internal.infinity_computation_result
112
+ elsif x == 1
113
+ return BigDecimal(1)
114
+ else
115
+ return y.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)
116
+ end
117
+ end
118
+
119
+ if x.infinite? && y < 0
120
+ # Computation result will be +0 or -0. Avoid overflow.
121
+ neg = x < 0 && y.frac.zero? && y % 2 == 1
122
+ return neg ? -BigDecimal(0) : BigDecimal(0)
123
+ end
124
+
125
+ if x.zero?
126
+ return BigDecimal(1) if y.zero?
127
+ return BigDecimal(0) if y > 0
128
+ if y.frac.zero? && y % 2 == 1 && x.sign == -1
129
+ return -BigDecimal::Internal.infinity_computation_result
130
+ else
131
+ return BigDecimal::Internal.infinity_computation_result
132
+ end
133
+ elsif x < 0
134
+ if y.frac.zero?
135
+ if y % 2 == 0
136
+ return (-x).power(y, prec)
137
+ else
138
+ return -(-x).power(y, prec)
139
+ end
140
+ else
141
+ raise Math::DomainError, 'Computation results in complex number'
142
+ end
143
+ elsif x == 1
144
+ return BigDecimal(1)
145
+ end
146
+
147
+ limit = BigDecimal.limit
148
+ frac_part = y.frac
149
+
150
+ if frac_part.zero? && prec.zero? && limit.zero?
151
+ # Infinite precision calculation for `x ** int` and `x.power(int)`
152
+ int_part = y.fix.to_i
153
+ int_part = -int_part if (neg = int_part < 0)
154
+ ans = BigDecimal(1)
155
+ n = 1
156
+ xn = x
157
+ while true
158
+ ans *= xn if int_part.allbits?(n)
159
+ n <<= 1
160
+ break if n > int_part
161
+ xn *= xn
162
+ # Detect overflow/underflow before consuming infinite memory
163
+ if (xn.exponent.abs - 1) * int_part / n >= 0x7FFFFFFFFFFFFFFF
164
+ return ((xn.exponent > 0) ^ neg ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0)) * (int_part.even? || x > 0 ? 1 : -1)
165
+ end
166
+ end
167
+ return neg ? BigDecimal(1) / ans : ans
168
+ end
169
+
170
+ result_prec = prec.nonzero? || [x.n_significant_digits, y.n_significant_digits, BigDecimal.double_fig].max + BigDecimal.double_fig
171
+ result_prec = [result_prec, limit].min if prec.zero? && limit.nonzero?
172
+
173
+ prec2 = result_prec + BigDecimal.double_fig
174
+
175
+ if y < 0
176
+ inv = x.power(-y, prec2)
177
+ return BigDecimal(0) if inv.infinite?
178
+ return BigDecimal::Internal.infinity_computation_result if inv.zero?
179
+ return BigDecimal(1).div(inv, result_prec)
180
+ end
181
+
182
+ if frac_part.zero? && y.exponent < Math.log(result_prec) * 5 + 20
183
+ # Use exponentiation by squaring if y is an integer and not too large
184
+ pow_prec = prec2 + y.exponent
185
+ n = 1
186
+ xn = x
187
+ ans = BigDecimal(1)
188
+ int_part = y.fix.to_i
189
+ while true
190
+ ans = ans.mult(xn, pow_prec) if int_part.allbits?(n)
191
+ n <<= 1
192
+ break if n > int_part
193
+ xn = xn.mult(xn, pow_prec)
194
+ end
195
+ ans.mult(1, result_prec)
196
+ else
197
+ if x > 1 && x.finite?
198
+ # To calculate exp(z, prec), z needs prec+max(z.exponent, 0) precision if z > 0.
199
+ # Estimate (y*log(x)).exponent
200
+ logx_exponent = x < 2 ? (x - 1).exponent : Math.log10(x.exponent).round
201
+ ylogx_exponent = y.exponent + logx_exponent
202
+ prec2 += [ylogx_exponent, 0].max
203
+ end
204
+ BigMath.exp(BigMath.log(x, prec2).mult(y, prec2), result_prec)
205
+ end
206
+ end
207
+
208
+ # Returns the square root of the value.
209
+ #
210
+ # Result has at least prec significant digits.
211
+ #
212
+ def sqrt(prec)
213
+ prec = Internal.coerce_validate_prec(prec, :sqrt, accept_zero: true)
214
+ return Internal.infinity_computation_result if infinite? == 1
215
+
216
+ raise FloatDomainError, 'sqrt of negative value' if self < 0
217
+ raise FloatDomainError, "sqrt of 'NaN'(Not a Number)" if nan?
218
+ return self if zero?
219
+
220
+ if prec == 0
221
+ limit = BigDecimal.limit
222
+ prec = n_significant_digits + BigDecimal.double_fig
223
+ prec = [limit, prec].min if limit.nonzero?
224
+ end
225
+
226
+ ex = exponent / 2
227
+ x = _decimal_shift(-2 * ex)
228
+ y = BigDecimal(Math.sqrt(x.to_f), 0)
229
+ precs = [prec + BigDecimal.double_fig]
230
+ precs << 2 + precs.last / 2 while precs.last > BigDecimal.double_fig
231
+ precs.reverse_each do |p|
232
+ y = y.add(x.div(y, p), p).div(2, p)
233
+ end
234
+ y._decimal_shift(ex).mult(1, prec)
235
+ end
236
+ end
237
+
238
+ # Core BigMath methods for BigDecimal (log, exp) are defined here.
239
+ # Other methods (sin, cos, atan) are defined in 'bigdecimal/math.rb'.
240
+ module BigMath
241
+ module_function
242
+
243
+ # call-seq:
244
+ # BigMath.log(decimal, numeric) -> BigDecimal
245
+ #
246
+ # Computes the natural logarithm of +decimal+ to the specified number of
247
+ # digits of precision, +numeric+.
248
+ #
249
+ # If +decimal+ is zero or negative, raises Math::DomainError.
250
+ #
251
+ # If +decimal+ is positive infinity, returns Infinity.
252
+ #
253
+ # If +decimal+ is NaN, returns NaN.
254
+ #
255
+ def log(x, prec)
256
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :log)
257
+ raise Math::DomainError, 'Complex argument for BigMath.log' if Complex === x
258
+
259
+ x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :log)
260
+ return BigDecimal::Internal.nan_computation_result if x.nan?
261
+ raise Math::DomainError, 'Negative argument for log' if x < 0
262
+ return -BigDecimal::Internal.infinity_computation_result if x.zero?
263
+ return BigDecimal::Internal.infinity_computation_result if x.infinite?
264
+ return BigDecimal(0) if x == 1
265
+
266
+ prec2 = prec + BigDecimal.double_fig
267
+ BigDecimal.save_limit do
268
+ BigDecimal.limit(0)
269
+ if x > 10 || x < 0.1
270
+ log10 = log(BigDecimal(10), prec2)
271
+ exponent = x.exponent
272
+ x = x._decimal_shift(-exponent)
273
+ if x < 0.3
274
+ x *= 10
275
+ exponent -= 1
276
+ end
277
+ return (log10 * exponent).add(log(x, prec2), prec)
278
+ end
279
+
280
+ x_minus_one_exponent = (x - 1).exponent
281
+
282
+ # log(x) = log(sqrt(sqrt(sqrt(sqrt(x))))) * 2**sqrt_steps
283
+ sqrt_steps = [Integer.sqrt(prec2) + 3 * x_minus_one_exponent, 0].max
284
+
285
+ lg2 = 0.3010299956639812
286
+ sqrt_prec = prec2 + [-x_minus_one_exponent, 0].max + (sqrt_steps * lg2).ceil
287
+
288
+ sqrt_steps.times do
289
+ x = x.sqrt(sqrt_prec)
290
+ end
291
+
292
+ # Taylor series for log(x) around 1
293
+ # log(x) = -log((1 + X) / (1 - X)) where X = (x - 1) / (x + 1)
294
+ # log(x) = 2 * (X + X**3 / 3 + X**5 / 5 + X**7 / 7 + ...)
295
+ x = (x - 1).div(x + 1, sqrt_prec)
296
+ y = x
297
+ x2 = x.mult(x, prec2)
298
+ 1.step do |i|
299
+ n = prec2 + x.exponent - y.exponent + x2.exponent
300
+ break if n <= 0 || x.zero?
301
+ x = x.mult(x2.round(n - x2.exponent), n)
302
+ y = y.add(x.div(2 * i + 1, n), prec2)
303
+ end
304
+
305
+ y.mult(2 ** (sqrt_steps + 1), prec)
306
+ end
307
+ end
308
+
309
+ # Taylor series for exp(x) around 0
310
+ private_class_method def _exp_taylor(x, prec) # :nodoc:
311
+ xn = BigDecimal(1)
312
+ y = BigDecimal(1)
313
+ 1.step do |i|
314
+ n = prec + xn.exponent
315
+ break if n <= 0 || xn.zero?
316
+ xn = xn.mult(x, n).div(i, n)
317
+ y = y.add(xn, prec)
318
+ end
319
+ y
320
+ end
321
+
322
+ # call-seq:
323
+ # BigMath.exp(decimal, numeric) -> BigDecimal
324
+ #
325
+ # Computes the value of e (the base of natural logarithms) raised to the
326
+ # power of +decimal+, to the specified number of digits of precision.
327
+ #
328
+ # If +decimal+ is infinity, returns Infinity.
329
+ #
330
+ # If +decimal+ is NaN, returns NaN.
331
+ #
332
+ def exp(x, prec)
333
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :exp)
334
+ x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :exp)
335
+ return BigDecimal::Internal.nan_computation_result if x.nan?
336
+ return x.positive? ? BigDecimal::Internal.infinity_computation_result : BigDecimal(0) if x.infinite?
337
+ return BigDecimal(1) if x.zero?
338
+
339
+ # exp(x * 10**cnt) = exp(x)**(10**cnt)
340
+ cnt = x < -1 || x > 1 ? x.exponent : 0
341
+ prec2 = prec + BigDecimal.double_fig + cnt
342
+ x = x._decimal_shift(-cnt)
343
+
344
+ # Calculation of exp(small_prec) is fast because calculation of x**n is fast
345
+ # Calculation of exp(small_abs) converges fast.
346
+ # exp(x) = exp(small_prec_part + small_abs_part) = exp(small_prec_part) * exp(small_abs_part)
347
+ x_small_prec = x.round(Integer.sqrt(prec2))
348
+ y = _exp_taylor(x_small_prec, prec2).mult(_exp_taylor(x.sub(x_small_prec, prec2), prec2), prec2)
349
+
350
+ # calculate exp(x * 10**cnt) from exp(x)
351
+ # exp(x * 10**k) = exp(x * 10**(k - 1)) ** 10
352
+ cnt.times do
353
+ y2 = y.mult(y, prec2)
354
+ y5 = y2.mult(y2, prec2).mult(y, prec2)
355
+ y = y5.mult(y5, prec2)
356
+ end
357
+
358
+ y.mult(1, prec)
359
+ end
360
+ 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.2
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenta Murata
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  requirements: []
63
- rubygems_version: 3.6.7
63
+ rubygems_version: 3.6.9
64
64
  specification_version: 4
65
65
  summary: Arbitrary-precision decimal floating-point number library.
66
66
  test_files: []