bigdecimal 3.1.8 → 3.3.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.
@@ -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) \
@@ -24,27 +24,22 @@ have_header("math.h")
24
24
  have_header("stdbool.h")
25
25
  have_header("stdlib.h")
26
26
 
27
- have_header("x86intrin.h")
28
- have_func("_lzcnt_u32", "x86intrin.h")
29
- have_func("_lzcnt_u64", "x86intrin.h")
30
-
31
- have_header("intrin.h")
32
- have_func("__lzcnt", "intrin.h")
33
- have_func("__lzcnt64", "intrin.h")
34
- have_func("_BitScanReverse", "intrin.h")
35
- have_func("_BitScanReverse64", "intrin.h")
27
+ if have_header("x86intrin.h")
28
+ have_func("_lzcnt_u32", "x86intrin.h")
29
+ have_func("_lzcnt_u64", "x86intrin.h")
30
+ end
36
31
 
37
- have_func("labs", "stdlib.h")
38
- have_func("llabs", "stdlib.h")
39
- have_func("finite", "math.h")
40
- have_func("isfinite", "math.h")
32
+ if have_header("intrin.h")
33
+ have_func("__lzcnt", "intrin.h")
34
+ have_func("__lzcnt64", "intrin.h")
35
+ have_func("_BitScanReverse", "intrin.h")
36
+ have_func("_BitScanReverse64", "intrin.h")
37
+ end
41
38
 
42
39
  have_header("ruby/atomic.h")
43
40
  have_header("ruby/internal/has/builtin.h")
44
41
  have_header("ruby/internal/static_assert.h")
45
42
 
46
- have_func("rb_rational_num", "ruby.h")
47
- have_func("rb_rational_den", "ruby.h")
48
43
  have_func("rb_complex_real", "ruby.h")
49
44
  have_func("rb_complex_imag", "ruby.h")
50
45
  have_func("rb_opts_exception_p", "ruby.h")
@@ -57,6 +52,9 @@ else
57
52
  bigdecimal_rb = "$(srcdir)/../../lib/bigdecimal.rb"
58
53
  end
59
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
+
60
58
  create_makefile('bigdecimal') {|mf|
61
59
  mf << "BIGDECIMAL_RB = #{bigdecimal_rb}\n"
62
60
  }
@@ -15,7 +15,8 @@
15
15
  _Pragma("GCC diagnostic push") \
16
16
  _Pragma("GCC diagnostic ignored \"-Wattributes\"") \
17
17
  __attribute__((__no_sanitize__(x))) y; \
18
- _Pragma("GCC diagnostic pop")
18
+ _Pragma("GCC diagnostic pop") \
19
+ y
19
20
  #endif
20
21
 
21
22
  #undef strtod
@@ -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
@@ -7,13 +7,13 @@ require 'bigdecimal'
7
7
  # sqrt(x, prec)
8
8
  # sin (x, prec)
9
9
  # cos (x, prec)
10
- # atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
10
+ # tan (x, prec)
11
+ # atan(x, prec)
11
12
  # PI (prec)
12
13
  # E (prec) == exp(1.0,prec)
13
14
  #
14
15
  # where:
15
16
  # x ... BigDecimal number to be computed.
16
- # |x| must be small enough to get convergence.
17
17
  # prec ... Number of digits to be obtained.
18
18
  #++
19
19
  #
@@ -25,8 +25,8 @@ require 'bigdecimal'
25
25
  #
26
26
  # include BigMath
27
27
  #
28
- # a = BigDecimal((PI(100)/2).to_s)
29
- # puts sin(a,100) # => 0.99999999999999999999......e0
28
+ # a = BigDecimal((PI(49)/2).to_s)
29
+ # puts sin(a,100) # => 0.9999999999...9999999986e0
30
30
  #
31
31
  module BigMath
32
32
  module_function
@@ -37,13 +37,42 @@ module BigMath
37
37
  # Computes the square root of +decimal+ to the specified number of digits of
38
38
  # precision, +numeric+.
39
39
  #
40
- # BigMath.sqrt(BigDecimal('2'), 16).to_s
41
- # #=> "0.1414213562373095048801688724e1"
40
+ # BigMath.sqrt(BigDecimal('2'), 32).to_s
41
+ # #=> "0.14142135623730950488016887242097e1"
42
42
  #
43
43
  def sqrt(x, prec)
44
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :sqrt)
45
+ x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :sqrt)
44
46
  x.sqrt(prec)
45
47
  end
46
48
 
49
+
50
+ # Returns [sign, reduced_x] where reduced_x is in -pi/2..pi/2
51
+ # and satisfies sin(x) = sign * sin(reduced_x)
52
+ # If add_half_pi is true, adds pi/2 to x before reduction.
53
+ # Precision of pi is adjusted to ensure reduced_x has the required precision.
54
+ private_class_method def _sin_periodic_reduction(x, prec, add_half_pi: false) # :nodoc:
55
+ return [1, x] if -Math::PI/2 <= x && x <= Math::PI/2 && !add_half_pi
56
+
57
+ mod_prec = prec + BigDecimal.double_fig
58
+ pi_extra_prec = [x.exponent, 0].max + BigDecimal.double_fig
59
+ while true
60
+ pi = PI(mod_prec + pi_extra_prec)
61
+ half_pi = pi / 2
62
+ div, mod = (add_half_pi ? x + pi : x + half_pi).divmod(pi)
63
+ mod -= half_pi
64
+ if mod.zero? || mod_prec + mod.exponent <= 0
65
+ # mod is too small to estimate required pi precision
66
+ mod_prec = mod_prec * 3 / 2 + BigDecimal.double_fig
67
+ elsif mod_prec + mod.exponent < prec
68
+ # Estimate required precision of pi
69
+ mod_prec = prec - mod.exponent + BigDecimal.double_fig
70
+ else
71
+ return [div % 2 == 0 ? 1 : -1, mod.mult(1, prec)]
72
+ end
73
+ end
74
+ end
75
+
47
76
  # call-seq:
48
77
  # sin(decimal, numeric) -> BigDecimal
49
78
  #
@@ -52,40 +81,33 @@ module BigMath
52
81
  #
53
82
  # If +decimal+ is Infinity or NaN, returns NaN.
54
83
  #
55
- # BigMath.sin(BigMath.PI(5)/4, 5).to_s
56
- # #=> "0.70710678118654752440082036563292800375e0"
84
+ # BigMath.sin(BigMath.PI(5)/4, 32).to_s
85
+ # #=> "0.70710807985947359435812921837984e0"
57
86
  #
58
87
  def sin(x, prec)
59
- raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
60
- return BigDecimal("NaN") if x.infinite? || x.nan?
88
+ prec = BigDecimal::Internal.coerce_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?
61
91
  n = prec + BigDecimal.double_fig
62
92
  one = BigDecimal("1")
63
93
  two = BigDecimal("2")
64
- x = -x if neg = x < 0
65
- if x > (twopi = two * BigMath.PI(prec))
66
- if x > 30
67
- x %= twopi
68
- else
69
- x -= twopi while x > twopi
70
- end
71
- end
94
+ sign, x = _sin_periodic_reduction(x, n)
72
95
  x1 = x
73
96
  x2 = x.mult(x,n)
74
- sign = 1
75
97
  y = x
76
98
  d = y
77
99
  i = one
78
100
  z = one
79
101
  while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
80
102
  m = BigDecimal.double_fig if m < BigDecimal.double_fig
81
- sign = -sign
82
- x1 = x2.mult(x1,n)
103
+ x1 = -x2.mult(x1,n)
83
104
  i += two
84
105
  z *= (i-one) * i
85
- d = sign * x1.div(z,m)
106
+ d = x1.div(z,m)
86
107
  y += d
87
108
  end
88
- neg ? -y : y
109
+ y = BigDecimal("1") if y > 1
110
+ y.mult(sign, prec)
89
111
  end
90
112
 
91
113
  # call-seq:
@@ -96,40 +118,34 @@ module BigMath
96
118
  #
97
119
  # If +decimal+ is Infinity or NaN, returns NaN.
98
120
  #
99
- # BigMath.cos(BigMath.PI(4), 16).to_s
100
- # #=> "-0.999999999999999999999999999999856613163740061349e0"
121
+ # BigMath.cos(BigMath.PI(16), 32).to_s
122
+ # #=> "-0.99999999999999999999999999999997e0"
101
123
  #
102
124
  def cos(x, prec)
103
- raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
104
- return BigDecimal("NaN") if x.infinite? || x.nan?
105
- n = prec + BigDecimal.double_fig
106
- one = BigDecimal("1")
107
- two = BigDecimal("2")
108
- x = -x if x < 0
109
- if x > (twopi = two * BigMath.PI(prec))
110
- if x > 30
111
- x %= twopi
112
- else
113
- x -= twopi while x > twopi
114
- end
115
- end
116
- x1 = one
117
- x2 = x.mult(x,n)
118
- sign = 1
119
- y = one
120
- d = y
121
- i = BigDecimal("0")
122
- z = one
123
- while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
124
- m = BigDecimal.double_fig if m < BigDecimal.double_fig
125
- sign = -sign
126
- x1 = x2.mult(x1,n)
127
- i += two
128
- z *= (i-one) * i
129
- d = sign * x1.div(z,m)
130
- y += d
131
- end
132
- y
125
+ prec = BigDecimal::Internal.coerce_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?
128
+ sign, x = _sin_periodic_reduction(x, prec + BigDecimal.double_fig, add_half_pi: true)
129
+ sign * sin(x, prec)
130
+ end
131
+
132
+ # call-seq:
133
+ # tan(decimal, numeric) -> BigDecimal
134
+ #
135
+ # Computes the tangent of +decimal+ to the specified number of digits of
136
+ # precision, +numeric+.
137
+ #
138
+ # If +decimal+ is Infinity or NaN, returns NaN.
139
+ #
140
+ # BigMath.tan(BigDecimal("0.0"), 4).to_s
141
+ # #=> "0.0"
142
+ #
143
+ # BigMath.tan(BigMath.PI(24) / 4, 32).to_s
144
+ # #=> "0.99999999999999999999999830836025e0"
145
+ #
146
+ def tan(x, prec)
147
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :tan)
148
+ sin(x, prec + BigDecimal.double_fig).div(cos(x, prec + BigDecimal.double_fig), prec)
133
149
  end
134
150
 
135
151
  # call-seq:
@@ -140,19 +156,20 @@ module BigMath
140
156
  #
141
157
  # If +decimal+ is NaN, returns NaN.
142
158
  #
143
- # BigMath.atan(BigDecimal('-1'), 16).to_s
144
- # #=> "-0.785398163397448309615660845819878471907514682065e0"
159
+ # BigMath.atan(BigDecimal('-1'), 32).to_s
160
+ # #=> "-0.78539816339744830961566084581988e0"
145
161
  #
146
162
  def atan(x, prec)
147
- raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
148
- return BigDecimal("NaN") if x.nan?
149
- pi = PI(prec)
163
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :atan)
164
+ x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :atan)
165
+ return BigDecimal::Internal.nan_computation_result if x.nan?
166
+ n = prec + BigDecimal.double_fig
167
+ pi = PI(n)
150
168
  x = -x if neg = x < 0
151
169
  return pi.div(neg ? -2 : 2, prec) if x.infinite?
152
- return pi / (neg ? -4 : 4) if x.round(prec) == 1
153
- x = BigDecimal("1").div(x, prec) if inv = x > 1
154
- x = (-1 + sqrt(1 + x**2, prec))/x if dbl = x > 0.5
155
- n = prec + BigDecimal.double_fig
170
+ return pi.div(neg ? -4 : 4, prec) if x.round(prec) == 1
171
+ x = BigDecimal("1").div(x, n) if inv = x > 1
172
+ x = (-1 + sqrt(1 + x.mult(x, n), n)).div(x, n) if dbl = x > 0.5
156
173
  y = x
157
174
  d = y
158
175
  t = x
@@ -168,7 +185,7 @@ module BigMath
168
185
  y *= 2 if dbl
169
186
  y = pi / 2 - y if inv
170
187
  y = -y if neg
171
- y
188
+ y.mult(1, prec)
172
189
  end
173
190
 
174
191
  # call-seq:
@@ -177,11 +194,11 @@ module BigMath
177
194
  # Computes the value of pi to the specified number of digits of precision,
178
195
  # +numeric+.
179
196
  #
180
- # BigMath.PI(10).to_s
181
- # #=> "0.3141592653589793238462643388813853786957412e1"
197
+ # BigMath.PI(32).to_s
198
+ # #=> "0.31415926535897932384626433832795e1"
182
199
  #
183
200
  def PI(prec)
184
- raise ArgumentError, "Zero or negative precision for PI" if prec <= 0
201
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :PI)
185
202
  n = prec + BigDecimal.double_fig
186
203
  zero = BigDecimal("0")
187
204
  one = BigDecimal("1")
@@ -213,7 +230,7 @@ module BigMath
213
230
  pi = pi + d
214
231
  k = k+two
215
232
  end
216
- pi
233
+ pi.mult(1, prec)
217
234
  end
218
235
 
219
236
  # call-seq:
@@ -222,11 +239,11 @@ module BigMath
222
239
  # Computes e (the base of natural logarithms) to the specified number of
223
240
  # digits of precision, +numeric+.
224
241
  #
225
- # BigMath.E(10).to_s
226
- # #=> "0.271828182845904523536028752390026306410273e1"
242
+ # BigMath.E(32).to_s
243
+ # #=> "0.27182818284590452353602874713527e1"
227
244
  #
228
245
  def E(prec)
229
- raise ArgumentError, "Zero or negative precision for E" if prec <= 0
246
+ prec = BigDecimal::Internal.coerce_validate_prec(prec, :E)
230
247
  BigMath.exp(1, prec)
231
248
  end
232
249
  end
@@ -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)
158
- BigDecimal(self) unless self.imag.zero? # to raise eerror
164
+ def to_d(precision=0)
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