bigdecimal 3.2.2 → 3.2.3

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.
@@ -17,7 +17,7 @@
17
17
  # include <float.h>
18
18
  #endif
19
19
 
20
- #ifdef HAVE_INT64_T
20
+ #if defined(HAVE_INT64_T) && !defined(BIGDECIMAL_USE_DECDIG_UINT16_T)
21
21
  # define DECDIG uint32_t
22
22
  # define DECDIG_DBL uint64_t
23
23
  # define DECDIG_DBL_SIGNED int64_t
@@ -167,7 +167,6 @@ enum rbd_rounding_mode {
167
167
  * r = 0.xxxxxxxxx *BASE**exponent
168
168
  */
169
169
  typedef struct {
170
- VALUE obj; /* Back pointer(VALUE) for Ruby object. */
171
170
  size_t MaxPrec; /* Maximum precision size */
172
171
  /* This is the actual size of frac[] */
173
172
  /*(frac[0] to frac[MaxPrec] are available). */
@@ -195,13 +194,7 @@ typedef struct {
195
194
  * ------------------
196
195
  */
197
196
 
198
- VP_EXPORT Real *VpNewRbClass(size_t mx, char const *str, VALUE klass, bool strict_p, bool raise_exception);
199
-
200
- VP_EXPORT Real *VpCreateRbObject(size_t mx, const char *str, bool raise_exception);
201
-
202
197
  #define VpBaseFig() BIGDECIMAL_COMPONENT_FIGURES
203
- #define VpDblFig() BIGDECIMAL_DOUBLE_FIGURES
204
- #define VpBaseVal() BIGDECIMAL_BASE
205
198
 
206
199
  /* Zero,Inf,NaN (isinf(),isnan() used to check) */
207
200
  VP_EXPORT double VpGetDoubleNaN(void);
@@ -211,7 +204,7 @@ VP_EXPORT double VpGetDoubleNegZero(void);
211
204
 
212
205
  /* These 2 functions added at v1.1.7 */
213
206
  VP_EXPORT size_t VpGetPrecLimit(void);
214
- VP_EXPORT size_t VpSetPrecLimit(size_t n);
207
+ VP_EXPORT void VpSetPrecLimit(size_t n);
215
208
 
216
209
  /* Round mode */
217
210
  VP_EXPORT int VpIsRoundMode(unsigned short n);
@@ -219,16 +212,14 @@ VP_EXPORT unsigned short VpGetRoundMode(void);
219
212
  VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
220
213
 
221
214
  VP_EXPORT int VpException(unsigned short f,const char *str,int always);
222
- #if 0 /* unused */
223
- VP_EXPORT int VpIsNegDoubleZero(double v);
224
- #endif
225
215
  VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
226
216
  VP_EXPORT size_t VpInit(DECDIG BaseVal);
227
- VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal, int strict_p, int exc);
217
+ VP_EXPORT Real *VpAlloc(const char *szVal, int strict_p, int exc);
228
218
  VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
229
219
  VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
230
220
  VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
231
221
  VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
222
+ VP_EXPORT int VpNmlz(Real *a);
232
223
  VP_EXPORT int VpComp(Real *a,Real *b);
233
224
  VP_EXPORT ssize_t VpExponent10(Real *a);
234
225
  VP_EXPORT void VpSzMantissa(Real *a, char *buf, size_t bufsize);
@@ -237,17 +228,10 @@ VP_EXPORT void VpToString(Real *a, char *buf, size_t bufsize, size_t fFmt, int f
237
228
  VP_EXPORT void VpToFString(Real *a, char *buf, size_t bufsize, size_t fFmt, int fPlus);
238
229
  VP_EXPORT int VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, const char *exp_chr, size_t ne);
239
230
  VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
240
- VP_EXPORT void VpDtoV(Real *m,double d);
241
- #if 0 /* unused */
242
- VP_EXPORT void VpItoV(Real *m,S_INT ival);
243
- #endif
244
- VP_EXPORT int VpSqrt(Real *y,Real *x);
245
231
  VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
246
232
  VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
247
233
  VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
248
234
  VP_EXPORT void VpFrac(Real *y, Real *x);
249
- VP_EXPORT int VpPowerByInt(Real *y, Real *x, SIGNED_VALUE n);
250
- #define VpPower VpPowerByInt
251
235
 
252
236
  /* VP constants */
253
237
  VP_EXPORT Real *VpOne(void);
@@ -261,10 +245,6 @@ VP_EXPORT Real *VpOne(void);
261
245
  #define Max(a, b) (((a)>(b))?(a):(b))
262
246
  #define Min(a, b) (((a)>(b))?(b):(a))
263
247
 
264
- #define VpMaxPrec(a) ((a)->MaxPrec)
265
- #define VpPrec(a) ((a)->Prec)
266
- #define VpGetFlag(a) ((a)->flag)
267
-
268
248
  /* Sign */
269
249
 
270
250
  /* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
@@ -299,7 +279,6 @@ VP_EXPORT Real *VpOne(void);
299
279
  #define VpSetInf(a,s) (void)(((s)>0)?VpSetPosInf(a):VpSetNegInf(a))
300
280
  #define VpHasVal(a) (a->frac[0])
301
281
  #define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
302
- #define VpExponent(a) (a->exponent)
303
282
  #ifdef BIGDECIMAL_DEBUG
304
283
  int VpVarCheck(Real * v);
305
284
  #endif /* BIGDECIMAL_DEBUG */
@@ -26,6 +26,9 @@
26
26
  ((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \
27
27
  ((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b)))
28
28
 
29
+ #define ADD_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
30
+ ((a) > 0) == ((b) > 0) && ((a) > 0 ? (max) - (a) < (b) : (min) - (a) > (b)))
31
+
29
32
  #ifdef HAVE_UINT128_T
30
33
  # define bit_length(x) \
31
34
  (unsigned int) \
@@ -36,17 +36,10 @@ if have_header("intrin.h")
36
36
  have_func("_BitScanReverse64", "intrin.h")
37
37
  end
38
38
 
39
- have_func("labs", "stdlib.h")
40
- have_func("llabs", "stdlib.h")
41
- have_func("finite", "math.h")
42
- have_func("isfinite", "math.h")
43
-
44
39
  have_header("ruby/atomic.h")
45
40
  have_header("ruby/internal/has/builtin.h")
46
41
  have_header("ruby/internal/static_assert.h")
47
42
 
48
- have_func("rb_rational_num", "ruby.h")
49
- have_func("rb_rational_den", "ruby.h")
50
43
  have_func("rb_complex_real", "ruby.h")
51
44
  have_func("rb_complex_imag", "ruby.h")
52
45
  have_func("rb_opts_exception_p", "ruby.h")
@@ -59,6 +52,9 @@ else
59
52
  bigdecimal_rb = "$(srcdir)/../../lib/bigdecimal.rb"
60
53
  end
61
54
 
55
+ $defs.push '-DBIGDECIMAL_USE_DECDIG_UINT16_T' if ENV['BIGDECIMAL_USE_DECDIG_UINT16_T'] == 'true'
56
+ $defs.push '-DBIGDECIMAL_USE_VP_TEST_METHODS' if ENV['BIGDECIMAL_USE_VP_TEST_METHODS'] == 'true'
57
+
62
58
  create_makefile('bigdecimal') {|mf|
63
59
  mf << "BIGDECIMAL_RB = #{bigdecimal_rb}\n"
64
60
  }
@@ -8,14 +8,6 @@ extern "C" {
8
8
  #endif
9
9
  #endif
10
10
 
11
- #ifdef HAVE_STDLIB_H
12
- # include <stdlib.h>
13
- #endif
14
-
15
- #ifdef HAVE_MATH_H
16
- # include <math.h>
17
- #endif
18
-
19
11
  #ifndef RB_UNUSED_VAR
20
12
  # if defined(_MSC_VER) && _MSC_VER >= 1911
21
13
  # define RB_UNUSED_VAR(x) x [[maybe_unused]]
@@ -55,97 +47,13 @@ extern "C" {
55
47
 
56
48
  /* bool */
57
49
 
58
- #if defined(__bool_true_false_are_defined)
59
- # /* Take that. */
60
-
61
- #elif defined(HAVE_STDBOOL_H)
50
+ #ifndef __bool_true_false_are_defined
62
51
  # include <stdbool.h>
63
-
64
- #else
65
- typedef unsigned char _Bool;
66
- # define bool _Bool
67
- # define true ((_Bool)+1)
68
- # define false ((_Bool)-1)
69
- # define __bool_true_false_are_defined
70
- #endif
71
-
72
- /* abs */
73
-
74
- #ifndef HAVE_LABS
75
- static inline long
76
- labs(long const x)
77
- {
78
- if (x < 0) return -x;
79
- return x;
80
- }
81
- #endif
82
-
83
- #ifndef HAVE_LLABS
84
- static inline LONG_LONG
85
- llabs(LONG_LONG const x)
86
- {
87
- if (x < 0) return -x;
88
- return x;
89
- }
90
- #endif
91
-
92
- #ifdef vabs
93
- # undef vabs
94
- #endif
95
- #if SIZEOF_VALUE <= SIZEOF_INT
96
- # define vabs abs
97
- #elif SIZEOF_VALUE <= SIZEOF_LONG
98
- # define vabs labs
99
- #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
100
- # define vabs llabs
101
- #endif
102
-
103
- /* finite */
104
-
105
- #ifndef HAVE_FINITE
106
- static int
107
- finite(double)
108
- {
109
- return !isnan(n) && !isinf(n);
110
- }
111
- #endif
112
-
113
- #ifndef isfinite
114
- # ifndef HAVE_ISFINITE
115
- # define HAVE_ISFINITE 1
116
- # define isfinite(x) finite(x)
117
- # endif
118
52
  #endif
119
53
 
120
54
  /* dtoa */
121
55
  char *BigDecimal_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve);
122
56
 
123
- /* rational */
124
-
125
- #ifndef HAVE_RB_RATIONAL_NUM
126
- static inline VALUE
127
- rb_rational_num(VALUE rat)
128
- {
129
- #ifdef RRATIONAL
130
- return RRATIONAL(rat)->num;
131
- #else
132
- return rb_funcall(rat, rb_intern("numerator"), 0);
133
- #endif
134
- }
135
- #endif
136
-
137
- #ifndef HAVE_RB_RATIONAL_DEN
138
- static inline VALUE
139
- rb_rational_den(VALUE rat)
140
- {
141
- #ifdef RRATIONAL
142
- return RRATIONAL(rat)->den;
143
- #else
144
- return rb_funcall(rat, rb_intern("denominator"), 0);
145
- #endif
146
- }
147
- #endif
148
-
149
57
  /* complex */
150
58
 
151
59
  #ifndef HAVE_RB_COMPLEX_REAL
@@ -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 > (twopi = two * BigMath.PI(prec))
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 > (twopi = two * BigMath.PI(prec))
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
- x = BigDecimal("1").div(x, prec) if inv = x > 1
153
- x = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5
154
- n = prec + BigDecimal.double_fig
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.2
4
+ version: 3.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenta Murata