bigdecimal 3.2.2 → 3.3.0
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/ext/bigdecimal/bigdecimal.c +744 -2268
- data/ext/bigdecimal/bigdecimal.h +4 -25
- data/ext/bigdecimal/bits.h +3 -0
- data/ext/bigdecimal/extconf.rb +3 -7
- data/ext/bigdecimal/missing.h +1 -93
- data/lib/bigdecimal/math.rb +88 -70
- data/lib/bigdecimal/util.rb +15 -14
- data/lib/bigdecimal.rb +340 -0
- metadata +1 -1
data/ext/bigdecimal/bigdecimal.h
CHANGED
@@ -17,7 +17,7 @@
|
|
17
17
|
# include <float.h>
|
18
18
|
#endif
|
19
19
|
|
20
|
-
#
|
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
|
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(
|
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 */
|
data/ext/bigdecimal/bits.h
CHANGED
@@ -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) \
|
data/ext/bigdecimal/extconf.rb
CHANGED
@@ -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
|
}
|
data/ext/bigdecimal/missing.h
CHANGED
@@ -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
|
-
#
|
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
|
data/lib/bigdecimal/math.rb
CHANGED
@@ -7,6 +7,7 @@ require 'bigdecimal'
|
|
7
7
|
# sqrt(x, prec)
|
8
8
|
# sin (x, prec)
|
9
9
|
# cos (x, prec)
|
10
|
+
# tan (x, prec)
|
10
11
|
# atan(x, prec)
|
11
12
|
# PI (prec)
|
12
13
|
# E (prec) == exp(1.0,prec)
|
@@ -24,8 +25,8 @@ require 'bigdecimal'
|
|
24
25
|
#
|
25
26
|
# include BigMath
|
26
27
|
#
|
27
|
-
# a = BigDecimal((PI(
|
28
|
-
# puts sin(a,100) # => 0.
|
28
|
+
# a = BigDecimal((PI(49)/2).to_s)
|
29
|
+
# puts sin(a,100) # => 0.9999999999...9999999986e0
|
29
30
|
#
|
30
31
|
module BigMath
|
31
32
|
module_function
|
@@ -36,13 +37,42 @@ module BigMath
|
|
36
37
|
# Computes the square root of +decimal+ to the specified number of digits of
|
37
38
|
# precision, +numeric+.
|
38
39
|
#
|
39
|
-
# BigMath.sqrt(BigDecimal('2'),
|
40
|
-
# #=> "0.
|
40
|
+
# BigMath.sqrt(BigDecimal('2'), 32).to_s
|
41
|
+
# #=> "0.14142135623730950488016887242097e1"
|
41
42
|
#
|
42
43
|
def sqrt(x, prec)
|
44
|
+
BigDecimal::Internal.validate_prec(prec, :sqrt)
|
45
|
+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :sqrt)
|
43
46
|
x.sqrt(prec)
|
44
47
|
end
|
45
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
|
+
|
46
76
|
# call-seq:
|
47
77
|
# sin(decimal, numeric) -> BigDecimal
|
48
78
|
#
|
@@ -51,40 +81,33 @@ module BigMath
|
|
51
81
|
#
|
52
82
|
# If +decimal+ is Infinity or NaN, returns NaN.
|
53
83
|
#
|
54
|
-
# BigMath.sin(BigMath.PI(5)/4,
|
55
|
-
# #=> "0.
|
84
|
+
# BigMath.sin(BigMath.PI(5)/4, 32).to_s
|
85
|
+
# #=> "0.70710807985947359435812921837984e0"
|
56
86
|
#
|
57
87
|
def sin(x, prec)
|
58
|
-
|
59
|
-
|
88
|
+
BigDecimal::Internal.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?
|
60
91
|
n = prec + BigDecimal.double_fig
|
61
92
|
one = BigDecimal("1")
|
62
93
|
two = BigDecimal("2")
|
63
|
-
|
64
|
-
if x > (twopi = two * BigMath.PI(prec))
|
65
|
-
if x > 30
|
66
|
-
x %= twopi
|
67
|
-
else
|
68
|
-
x -= twopi while x > twopi
|
69
|
-
end
|
70
|
-
end
|
94
|
+
sign, x = _sin_periodic_reduction(x, n)
|
71
95
|
x1 = x
|
72
96
|
x2 = x.mult(x,n)
|
73
|
-
sign = 1
|
74
97
|
y = x
|
75
98
|
d = y
|
76
99
|
i = one
|
77
100
|
z = one
|
78
101
|
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
79
102
|
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
80
|
-
|
81
|
-
x1 = x2.mult(x1,n)
|
103
|
+
x1 = -x2.mult(x1,n)
|
82
104
|
i += two
|
83
105
|
z *= (i-one) * i
|
84
|
-
d =
|
106
|
+
d = x1.div(z,m)
|
85
107
|
y += d
|
86
108
|
end
|
87
|
-
|
109
|
+
y = BigDecimal("1") if y > 1
|
110
|
+
y.mult(sign, prec)
|
88
111
|
end
|
89
112
|
|
90
113
|
# call-seq:
|
@@ -95,40 +118,34 @@ module BigMath
|
|
95
118
|
#
|
96
119
|
# If +decimal+ is Infinity or NaN, returns NaN.
|
97
120
|
#
|
98
|
-
# BigMath.cos(BigMath.PI(
|
99
|
-
# #=> "-0.
|
121
|
+
# BigMath.cos(BigMath.PI(16), 32).to_s
|
122
|
+
# #=> "-0.99999999999999999999999999999997e0"
|
100
123
|
#
|
101
124
|
def cos(x, prec)
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
i += two
|
127
|
-
z *= (i-one) * i
|
128
|
-
d = sign * x1.div(z,m)
|
129
|
-
y += d
|
130
|
-
end
|
131
|
-
y
|
125
|
+
BigDecimal::Internal.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
|
+
BigDecimal::Internal.validate_prec(prec, :tan)
|
148
|
+
sin(x, prec + BigDecimal.double_fig).div(cos(x, prec + BigDecimal.double_fig), prec)
|
132
149
|
end
|
133
150
|
|
134
151
|
# call-seq:
|
@@ -139,19 +156,20 @@ module BigMath
|
|
139
156
|
#
|
140
157
|
# If +decimal+ is NaN, returns NaN.
|
141
158
|
#
|
142
|
-
# BigMath.atan(BigDecimal('-1'),
|
143
|
-
# #=> "-0.
|
159
|
+
# BigMath.atan(BigDecimal('-1'), 32).to_s
|
160
|
+
# #=> "-0.78539816339744830961566084581988e0"
|
144
161
|
#
|
145
162
|
def atan(x, prec)
|
146
|
-
|
147
|
-
|
148
|
-
|
163
|
+
BigDecimal::Internal.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)
|
149
168
|
x = -x if neg = x < 0
|
150
169
|
return pi.div(neg ? -2 : 2, prec) if x.infinite?
|
151
|
-
return pi
|
152
|
-
x = BigDecimal("1").div(x,
|
153
|
-
x = (-1 + sqrt(1 + x
|
154
|
-
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
|
155
173
|
y = x
|
156
174
|
d = y
|
157
175
|
t = x
|
@@ -167,7 +185,7 @@ module BigMath
|
|
167
185
|
y *= 2 if dbl
|
168
186
|
y = pi / 2 - y if inv
|
169
187
|
y = -y if neg
|
170
|
-
y
|
188
|
+
y.mult(1, prec)
|
171
189
|
end
|
172
190
|
|
173
191
|
# call-seq:
|
@@ -176,11 +194,11 @@ module BigMath
|
|
176
194
|
# Computes the value of pi to the specified number of digits of precision,
|
177
195
|
# +numeric+.
|
178
196
|
#
|
179
|
-
# BigMath.PI(
|
180
|
-
# #=> "0.
|
197
|
+
# BigMath.PI(32).to_s
|
198
|
+
# #=> "0.31415926535897932384626433832795e1"
|
181
199
|
#
|
182
200
|
def PI(prec)
|
183
|
-
|
201
|
+
BigDecimal::Internal.validate_prec(prec, :PI)
|
184
202
|
n = prec + BigDecimal.double_fig
|
185
203
|
zero = BigDecimal("0")
|
186
204
|
one = BigDecimal("1")
|
@@ -212,7 +230,7 @@ module BigMath
|
|
212
230
|
pi = pi + d
|
213
231
|
k = k+two
|
214
232
|
end
|
215
|
-
pi
|
233
|
+
pi.mult(1, prec)
|
216
234
|
end
|
217
235
|
|
218
236
|
# call-seq:
|
@@ -221,11 +239,11 @@ module BigMath
|
|
221
239
|
# Computes e (the base of natural logarithms) to the specified number of
|
222
240
|
# digits of precision, +numeric+.
|
223
241
|
#
|
224
|
-
# BigMath.E(
|
225
|
-
# #=> "0.
|
242
|
+
# BigMath.E(32).to_s
|
243
|
+
# #=> "0.27182818284590452353602874713527e1"
|
226
244
|
#
|
227
245
|
def E(prec)
|
228
|
-
|
246
|
+
BigDecimal::Internal.validate_prec(prec, :E)
|
229
247
|
BigMath.exp(1, prec)
|
230
248
|
end
|
231
249
|
end
|
data/lib/bigdecimal/util.rb
CHANGED
@@ -119,8 +119,11 @@ class Rational < Numeric
|
|
119
119
|
#
|
120
120
|
# Returns the value as a BigDecimal.
|
121
121
|
#
|
122
|
-
# The
|
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
|
146
|
-
#
|
147
|
-
#
|
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(
|
164
|
+
def to_d(precision=0)
|
158
165
|
BigDecimal(self) unless self.imag.zero? # to raise error
|
159
166
|
|
160
|
-
|
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
|
|