bigdecimal 3.3.1 → 4.1.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.
- checksums.yaml +4 -4
- data/bigdecimal.gemspec +8 -1
- data/ext/bigdecimal/bigdecimal.c +288 -303
- data/ext/bigdecimal/bigdecimal.h +48 -42
- data/ext/bigdecimal/div.h +219 -0
- data/ext/bigdecimal/extconf.rb +5 -2
- data/ext/bigdecimal/missing/dtoa.c +184 -137
- data/ext/bigdecimal/missing.h +4 -2
- data/ext/bigdecimal/ntt.h +191 -0
- data/lib/bigdecimal/jacobian.rb +2 -0
- data/lib/bigdecimal/ludcmp.rb +2 -0
- data/lib/bigdecimal/math/erf.rb +291 -0
- data/lib/bigdecimal/math/gamma.rb +513 -0
- data/lib/bigdecimal/math.rb +548 -81
- data/lib/bigdecimal/newton.rb +2 -0
- data/lib/bigdecimal/util.rb +1 -1
- data/lib/bigdecimal.rb +121 -73
- data/sample/linear.rb +73 -37
- data/sample/nlsolve.rb +47 -30
- data/sample/pi.rb +2 -7
- data/sig/big_decimal.rbs +1504 -0
- data/sig/big_decimal_util.rbs +158 -0
- data/sig/big_math.rbs +423 -0
- metadata +10 -3
data/ext/bigdecimal/bigdecimal.h
CHANGED
|
@@ -17,24 +17,15 @@
|
|
|
17
17
|
# include <float.h>
|
|
18
18
|
#endif
|
|
19
19
|
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
# define PRI_DECDIG_DBL_PREFIX PRI_LL_PREFIX
|
|
28
|
-
# else
|
|
29
|
-
# define PRI_DECDIG_DBL_PREFIX "l"
|
|
30
|
-
# endif
|
|
20
|
+
#define DECDIG uint32_t
|
|
21
|
+
#define DECDIG_DBL uint64_t
|
|
22
|
+
#define DECDIG_DBL_SIGNED int64_t
|
|
23
|
+
#define SIZEOF_DECDIG 4
|
|
24
|
+
#define PRI_DECDIG_PREFIX ""
|
|
25
|
+
#ifdef PRI_LL_PREFIX
|
|
26
|
+
# define PRI_DECDIG_DBL_PREFIX PRI_LL_PREFIX
|
|
31
27
|
#else
|
|
32
|
-
# define
|
|
33
|
-
# define DECDIG_DBL uint32_t
|
|
34
|
-
# define DECDIG_DBL_SIGNED int32_t
|
|
35
|
-
# define SIZEOF_DECDIG 2
|
|
36
|
-
# define PRI_DECDIG_PREFIX "h"
|
|
37
|
-
# define PRI_DECDIG_DBL_PREFIX ""
|
|
28
|
+
# define PRI_DECDIG_DBL_PREFIX "l"
|
|
38
29
|
#endif
|
|
39
30
|
|
|
40
31
|
#define PRIdDECDIG PRI_DECDIG_PREFIX"d"
|
|
@@ -51,31 +42,15 @@
|
|
|
51
42
|
#define PRIxDECDIG_DBL PRI_DECDIG_DBL_PREFIX"x"
|
|
52
43
|
#define PRIXDECDIG_DBL PRI_DECDIG_DBL_PREFIX"X"
|
|
53
44
|
|
|
54
|
-
#
|
|
55
|
-
#
|
|
56
|
-
# define BIGDECIMAL_COMPONENT_FIGURES 9
|
|
45
|
+
#define BIGDECIMAL_BASE ((DECDIG)1000000000U)
|
|
46
|
+
#define BIGDECIMAL_COMPONENT_FIGURES 9
|
|
57
47
|
/*
|
|
58
48
|
* The number of components required for a 64-bit integer.
|
|
59
49
|
*
|
|
60
50
|
* INT64_MAX: 9_223372036_854775807
|
|
61
51
|
* UINT64_MAX: 18_446744073_709551615
|
|
62
52
|
*/
|
|
63
|
-
#
|
|
64
|
-
|
|
65
|
-
#elif SIZEOF_DECDIG == 2
|
|
66
|
-
# define BIGDECIMAL_BASE ((DECDIG)10000U)
|
|
67
|
-
# define BIGDECIMAL_COMPONENT_FIGURES 4
|
|
68
|
-
/*
|
|
69
|
-
* The number of components required for a 64-bit integer.
|
|
70
|
-
*
|
|
71
|
-
* INT64_MAX: 922_3372_0368_5477_5807
|
|
72
|
-
* UINT64_MAX: 1844_6744_0737_0955_1615
|
|
73
|
-
*/
|
|
74
|
-
# define BIGDECIMAL_INT64_MAX_LENGTH 5
|
|
75
|
-
|
|
76
|
-
#else
|
|
77
|
-
# error Unknown size of DECDIG
|
|
78
|
-
#endif
|
|
53
|
+
#define BIGDECIMAL_INT64_MAX_LENGTH 3
|
|
79
54
|
|
|
80
55
|
#define BIGDECIMAL_DOUBLE_FIGURES (1+DBL_DIG)
|
|
81
56
|
|
|
@@ -188,6 +163,16 @@ typedef struct {
|
|
|
188
163
|
DECDIG frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
|
|
189
164
|
} Real;
|
|
190
165
|
|
|
166
|
+
typedef struct {
|
|
167
|
+
VALUE bigdecimal;
|
|
168
|
+
Real *real;
|
|
169
|
+
} BDVALUE;
|
|
170
|
+
|
|
171
|
+
typedef struct {
|
|
172
|
+
VALUE bigdecimal_or_nil;
|
|
173
|
+
Real *real_or_null;
|
|
174
|
+
} NULLABLE_BDVALUE;
|
|
175
|
+
|
|
191
176
|
/*
|
|
192
177
|
* ------------------
|
|
193
178
|
* EXPORTables.
|
|
@@ -214,7 +199,7 @@ VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
|
|
|
214
199
|
VP_EXPORT int VpException(unsigned short f,const char *str,int always);
|
|
215
200
|
VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
|
|
216
201
|
VP_EXPORT size_t VpInit(DECDIG BaseVal);
|
|
217
|
-
VP_EXPORT
|
|
202
|
+
VP_EXPORT NULLABLE_BDVALUE VpAlloc(const char *szVal, int strict_p, int exc);
|
|
218
203
|
VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
|
|
219
204
|
VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
|
|
220
205
|
VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
|
|
@@ -232,10 +217,31 @@ VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
|
|
|
232
217
|
VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
|
|
233
218
|
VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
|
|
234
219
|
VP_EXPORT void VpFrac(Real *y, Real *x);
|
|
220
|
+
VP_EXPORT int AddExponent(Real *a, SIGNED_VALUE n);
|
|
235
221
|
|
|
236
222
|
/* VP constants */
|
|
237
223
|
VP_EXPORT Real *VpOne(void);
|
|
238
224
|
|
|
225
|
+
/*
|
|
226
|
+
* **** BigDecimal part ****
|
|
227
|
+
*/
|
|
228
|
+
VP_EXPORT VALUE BigDecimal_lt(VALUE self, VALUE r);
|
|
229
|
+
VP_EXPORT VALUE BigDecimal_ge(VALUE self, VALUE r);
|
|
230
|
+
VP_EXPORT VALUE BigDecimal_exponent(VALUE self);
|
|
231
|
+
VP_EXPORT VALUE BigDecimal_fix(VALUE self);
|
|
232
|
+
VP_EXPORT VALUE BigDecimal_frac(VALUE self);
|
|
233
|
+
VP_EXPORT VALUE BigDecimal_add(VALUE self, VALUE b);
|
|
234
|
+
VP_EXPORT VALUE BigDecimal_sub(VALUE self, VALUE b);
|
|
235
|
+
VP_EXPORT VALUE BigDecimal_mult(VALUE self, VALUE b);
|
|
236
|
+
VP_EXPORT VALUE BigDecimal_add2(VALUE self, VALUE b, VALUE n);
|
|
237
|
+
VP_EXPORT VALUE BigDecimal_sub2(VALUE self, VALUE b, VALUE n);
|
|
238
|
+
VP_EXPORT VALUE BigDecimal_mult2(VALUE self, VALUE b, VALUE n);
|
|
239
|
+
VP_EXPORT VALUE BigDecimal_split(VALUE self);
|
|
240
|
+
VP_EXPORT VALUE BigDecimal_decimal_shift(VALUE self, VALUE v);
|
|
241
|
+
VP_EXPORT inline BDVALUE GetBDValueMust(VALUE v);
|
|
242
|
+
VP_EXPORT inline BDVALUE rbd_allocate_struct_zero_wrap(int sign, size_t const digits);
|
|
243
|
+
#define NewZeroWrap rbd_allocate_struct_zero_wrap
|
|
244
|
+
|
|
239
245
|
/*
|
|
240
246
|
* ------------------
|
|
241
247
|
* MACRO definitions.
|
|
@@ -261,21 +267,21 @@ VP_EXPORT Real *VpOne(void);
|
|
|
261
267
|
#define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
|
|
262
268
|
#define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
|
|
263
269
|
#define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
|
|
264
|
-
#define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
|
|
265
|
-
#define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
|
|
270
|
+
#define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_POSITIVE_ZERO)
|
|
271
|
+
#define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
|
|
266
272
|
#define VpSetZero(a,s) (void)(((s)>0)?VpSetPosZero(a):VpSetNegZero(a))
|
|
267
273
|
|
|
268
274
|
/* NaN */
|
|
269
275
|
#define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
|
|
270
|
-
#define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
|
|
276
|
+
#define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_NaN)
|
|
271
277
|
|
|
272
278
|
/* Infinity */
|
|
273
279
|
#define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
|
|
274
280
|
#define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
|
|
275
281
|
#define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
|
|
276
282
|
#define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
|
|
277
|
-
#define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
|
|
278
|
-
#define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
|
|
283
|
+
#define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
|
|
284
|
+
#define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->exponent=0,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
|
|
279
285
|
#define VpSetInf(a,s) (void)(((s)>0)?VpSetPosInf(a):VpSetNegInf(a))
|
|
280
286
|
#define VpHasVal(a) (a->frac[0])
|
|
281
287
|
#define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
struct vp_settings {
|
|
2
|
+
size_t prec_limit;
|
|
3
|
+
unsigned short rounding_mode;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
static VALUE
|
|
7
|
+
restore_vp_settings(VALUE saved_ptr)
|
|
8
|
+
{
|
|
9
|
+
struct vp_settings *saved = (struct vp_settings *)saved_ptr;
|
|
10
|
+
VpSetPrecLimit(saved->prec_limit);
|
|
11
|
+
VpSetRoundMode(saved->rounding_mode);
|
|
12
|
+
return Qnil;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Calculate the inverse of x using the Newton-Raphson method.
|
|
16
|
+
// Assumes no precision limit and ROUND_HALF_UP. See VpDivdNewton.
|
|
17
|
+
static VALUE
|
|
18
|
+
newton_raphson_inverse(VALUE x, size_t prec) {
|
|
19
|
+
BDVALUE bdone = NewZeroWrap(1, 1);
|
|
20
|
+
VpSetOne(bdone.real);
|
|
21
|
+
VALUE one = bdone.bigdecimal;
|
|
22
|
+
|
|
23
|
+
// Initial approximation: 10^18 / (leading 9 digits of x), calculated in 64-bit integer.
|
|
24
|
+
// Truncating x to 9 digits and the quotient to an integer keep the relative error below 1.2e-8,
|
|
25
|
+
// so it has at least 7 correct digits.
|
|
26
|
+
const size_t initial_digits = 7;
|
|
27
|
+
const DECDIG_DBL base_sq = (DECDIG_DBL)BIGDECIMAL_BASE * BIGDECIMAL_BASE;
|
|
28
|
+
BDVALUE bdx = GetBDValueMust(x);
|
|
29
|
+
DECDIG_DBL x_lead = (DECDIG_DBL)bdx.real->frac[0] * BIGDECIMAL_BASE + (bdx.real->Prec >= 2 ? bdx.real->frac[1] : 0);
|
|
30
|
+
int shift = 0;
|
|
31
|
+
while (x_lead < base_sq / 10) {
|
|
32
|
+
x_lead *= 10;
|
|
33
|
+
shift++;
|
|
34
|
+
}
|
|
35
|
+
// Dividing base_sq - 1 instead of base_sq keeps the quotient below 10 * BASE,
|
|
36
|
+
// so that frac[0] of inv0 never reaches BASE.
|
|
37
|
+
DECDIG_DBL inv_lead = (base_sq - 1) / (x_lead / BIGDECIMAL_BASE);
|
|
38
|
+
for (int i = 0; i < shift; i++) inv_lead *= 10;
|
|
39
|
+
BDVALUE inv0 = NewZeroWrap(1, 2 * BIGDECIMAL_COMPONENT_FIGURES);
|
|
40
|
+
VpSetOne(inv0.real);
|
|
41
|
+
inv0.real->frac[0] = (DECDIG)(inv_lead / BIGDECIMAL_BASE);
|
|
42
|
+
inv0.real->frac[1] = (DECDIG)(inv_lead % BIGDECIMAL_BASE);
|
|
43
|
+
inv0.real->Prec = 2;
|
|
44
|
+
inv0.real->exponent = 1 - bdx.real->exponent;
|
|
45
|
+
VpNmlz(inv0.real);
|
|
46
|
+
RB_GC_GUARD(bdx.bigdecimal);
|
|
47
|
+
VALUE inv = inv0.bigdecimal;
|
|
48
|
+
|
|
49
|
+
int bl = 1;
|
|
50
|
+
while (((size_t)1 << bl) < prec) bl++;
|
|
51
|
+
|
|
52
|
+
// Each iteration doubles the number of correct digits, and the rounding error of
|
|
53
|
+
// the previous iteration is squared into the new digits. Margin digits keep the
|
|
54
|
+
// squared error below the last digit, otherwise it can grow over iterations.
|
|
55
|
+
const size_t margin = 4;
|
|
56
|
+
for (int i = bl; i >= 0; i--) {
|
|
57
|
+
size_t n = (prec >> i) + margin;
|
|
58
|
+
if (n > prec) n = prec;
|
|
59
|
+
// inv0 already has this precision. The last iteration is kept to round inv to prec digits.
|
|
60
|
+
if (n <= initial_digits && i > 0) continue;
|
|
61
|
+
// Newton-Raphson iteration: inv_next = inv + inv * (1 - x * inv)
|
|
62
|
+
// (1 - x * inv) is about 10^(-n/2), so calculating it in n/2 digits is enough for inv_next in n digits.
|
|
63
|
+
VALUE one_minus_x_inv = BigDecimal_sub2(
|
|
64
|
+
one,
|
|
65
|
+
BigDecimal_mult(BigDecimal_mult2(x, one, SIZET2NUM(n + 1)), inv),
|
|
66
|
+
SIZET2NUM(n / 2 + margin)
|
|
67
|
+
);
|
|
68
|
+
inv = BigDecimal_add2(
|
|
69
|
+
inv,
|
|
70
|
+
BigDecimal_mult(inv, one_minus_x_inv),
|
|
71
|
+
SIZET2NUM(n)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return inv;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Calculates divmod by multiplying approximate reciprocal of y
|
|
78
|
+
static void
|
|
79
|
+
divmod_by_inv_mul(VALUE x, VALUE y, VALUE inv, VALUE *res_div, VALUE *res_mod) {
|
|
80
|
+
VALUE div = BigDecimal_fix(BigDecimal_mult(x, inv));
|
|
81
|
+
VALUE mod = BigDecimal_sub(x, BigDecimal_mult(div, y));
|
|
82
|
+
while (RTEST(BigDecimal_lt(mod, INT2FIX(0)))) {
|
|
83
|
+
mod = BigDecimal_add(mod, y);
|
|
84
|
+
div = BigDecimal_sub(div, INT2FIX(1));
|
|
85
|
+
}
|
|
86
|
+
while (RTEST(BigDecimal_ge(mod, y))) {
|
|
87
|
+
mod = BigDecimal_sub(mod, y);
|
|
88
|
+
div = BigDecimal_add(div, INT2FIX(1));
|
|
89
|
+
}
|
|
90
|
+
*res_div = div;
|
|
91
|
+
*res_mod = mod;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static void
|
|
95
|
+
slice_copy(DECDIG *dest, Real *src, size_t rshift, size_t length) {
|
|
96
|
+
ssize_t start = src->exponent - (ssize_t)rshift - (ssize_t)length;
|
|
97
|
+
ssize_t from = Max(start, 0);
|
|
98
|
+
ssize_t to = Min(start + (ssize_t)length, (ssize_t)src->Prec);
|
|
99
|
+
if (from >= to) return;
|
|
100
|
+
memcpy(dest + (from - start), src->frac + from, (size_t)(to - from) * sizeof(DECDIG));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* Calculates divmod using Newton-Raphson method.
|
|
104
|
+
* x and y must be a BigDecimal representing an integer value.
|
|
105
|
+
*
|
|
106
|
+
* To calculate with low cost, we need to split x into blocks and perform divmod for each block.
|
|
107
|
+
* x_digits = remaining_digits(<= y_digits) + block_digits * num_blocks
|
|
108
|
+
*
|
|
109
|
+
* Example:
|
|
110
|
+
* xxx_xxxxx_xxxxx_xxxxx(18 digits) / yyyyy(5 digits)
|
|
111
|
+
* remaining_digits = 3, block_digits = 5, num_blocks = 3
|
|
112
|
+
* repeating xxxxx_xxxxxx.divmod(yyyyy) calculation 3 times.
|
|
113
|
+
*
|
|
114
|
+
* In each divmod step, dividend is at most (y_digits + block_digits) digits and divisor is y_digits digits.
|
|
115
|
+
* Reciprocal of y needs block_digits + 1 precision.
|
|
116
|
+
*/
|
|
117
|
+
static void
|
|
118
|
+
divmod_newton(VALUE x, VALUE y, VALUE *div_out, VALUE *mod_out) {
|
|
119
|
+
size_t x_digits = NUM2SIZET(BigDecimal_exponent(x));
|
|
120
|
+
size_t y_digits = NUM2SIZET(BigDecimal_exponent(y));
|
|
121
|
+
if (x_digits <= y_digits) x_digits = y_digits + 1;
|
|
122
|
+
|
|
123
|
+
size_t n = x_digits / y_digits;
|
|
124
|
+
size_t block_figs = (x_digits - y_digits) / n / BIGDECIMAL_COMPONENT_FIGURES + 1;
|
|
125
|
+
size_t block_digits = block_figs * BIGDECIMAL_COMPONENT_FIGURES;
|
|
126
|
+
size_t num_blocks = (x_digits - y_digits + block_digits - 1) / block_digits;
|
|
127
|
+
size_t y_figs = (y_digits - 1) / BIGDECIMAL_COMPONENT_FIGURES + 1;
|
|
128
|
+
VALUE yinv = newton_raphson_inverse(y, block_digits + 1);
|
|
129
|
+
|
|
130
|
+
BDVALUE divident = NewZeroWrap(1, BIGDECIMAL_COMPONENT_FIGURES * (y_figs + block_figs));
|
|
131
|
+
BDVALUE div_result = NewZeroWrap(1, BIGDECIMAL_COMPONENT_FIGURES * (num_blocks * block_figs + 1));
|
|
132
|
+
BDVALUE bdx = GetBDValueMust(x);
|
|
133
|
+
|
|
134
|
+
VALUE mod = BigDecimal_fix(BigDecimal_decimal_shift(x, SSIZET2NUM(-(ssize_t)(num_blocks * block_digits))));
|
|
135
|
+
for (ssize_t i = (ssize_t)(num_blocks - 1); i >= 0; i--) {
|
|
136
|
+
memset(divident.real->frac, 0, (y_figs + block_figs) * sizeof(DECDIG));
|
|
137
|
+
|
|
138
|
+
BDVALUE bdmod = GetBDValueMust(mod);
|
|
139
|
+
slice_copy(divident.real->frac, bdmod.real, 0, y_figs);
|
|
140
|
+
slice_copy(divident.real->frac + y_figs, bdx.real, (size_t)i * block_figs, block_figs);
|
|
141
|
+
RB_GC_GUARD(bdmod.bigdecimal);
|
|
142
|
+
|
|
143
|
+
VpSetSign(divident.real, 1);
|
|
144
|
+
divident.real->exponent = (ssize_t)(y_figs + block_figs);
|
|
145
|
+
divident.real->Prec = y_figs + block_figs;
|
|
146
|
+
VpNmlz(divident.real);
|
|
147
|
+
|
|
148
|
+
VALUE div;
|
|
149
|
+
divmod_by_inv_mul(divident.bigdecimal, y, yinv, &div, &mod);
|
|
150
|
+
BDVALUE bddiv = GetBDValueMust(div);
|
|
151
|
+
slice_copy(div_result.real->frac + (num_blocks - (size_t)i - 1) * block_figs, bddiv.real, 0, block_figs + 1);
|
|
152
|
+
RB_GC_GUARD(bddiv.bigdecimal);
|
|
153
|
+
}
|
|
154
|
+
VpSetSign(div_result.real, 1);
|
|
155
|
+
div_result.real->exponent = (ssize_t)(num_blocks * block_figs + 1);
|
|
156
|
+
div_result.real->Prec = num_blocks * block_figs + 1;
|
|
157
|
+
VpNmlz(div_result.real);
|
|
158
|
+
RB_GC_GUARD(bdx.bigdecimal);
|
|
159
|
+
RB_GC_GUARD(divident.bigdecimal);
|
|
160
|
+
RB_GC_GUARD(div_result.bigdecimal);
|
|
161
|
+
*div_out = div_result.bigdecimal;
|
|
162
|
+
*mod_out = mod;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
struct vp_divd_newton_args {
|
|
166
|
+
Real *c, *r, *a, *b;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
static VALUE
|
|
170
|
+
VpDivdNewtonInner(VALUE args_ptr)
|
|
171
|
+
{
|
|
172
|
+
struct vp_divd_newton_args *args = (struct vp_divd_newton_args *)args_ptr;
|
|
173
|
+
Real *c = args->c, *r = args->r, *a = args->a, *b = args->b;
|
|
174
|
+
BDVALUE a2, b2, c2, r2;
|
|
175
|
+
VALUE div, mod, a2_frac = Qnil;
|
|
176
|
+
size_t div_prec = c->MaxPrec - 1;
|
|
177
|
+
size_t base_prec = b->Prec;
|
|
178
|
+
|
|
179
|
+
a2 = NewZeroWrap(1, a->Prec * BIGDECIMAL_COMPONENT_FIGURES);
|
|
180
|
+
b2 = NewZeroWrap(1, b->Prec * BIGDECIMAL_COMPONENT_FIGURES);
|
|
181
|
+
VpAsgn(a2.real, a, 1);
|
|
182
|
+
VpAsgn(b2.real, b, 1);
|
|
183
|
+
VpSetSign(a2.real, 1);
|
|
184
|
+
VpSetSign(b2.real, 1);
|
|
185
|
+
a2.real->exponent = (ssize_t)(base_prec + div_prec);
|
|
186
|
+
b2.real->exponent = (ssize_t)base_prec;
|
|
187
|
+
|
|
188
|
+
if ((ssize_t)a2.real->Prec > a2.real->exponent) {
|
|
189
|
+
a2_frac = BigDecimal_frac(a2.bigdecimal);
|
|
190
|
+
VpMidRound(a2.real, VP_ROUND_DOWN, 0);
|
|
191
|
+
}
|
|
192
|
+
divmod_newton(a2.bigdecimal, b2.bigdecimal, &div, &mod);
|
|
193
|
+
if (a2_frac != Qnil) mod = BigDecimal_add(mod, a2_frac);
|
|
194
|
+
|
|
195
|
+
c2 = GetBDValueMust(div);
|
|
196
|
+
r2 = GetBDValueMust(mod);
|
|
197
|
+
VpAsgn(c, c2.real, VpGetSign(a) * VpGetSign(b));
|
|
198
|
+
VpAsgn(r, r2.real, VpGetSign(a));
|
|
199
|
+
if (!VpIsZero(c)) AddExponent(c, a->exponent - b->exponent - (ssize_t)div_prec);
|
|
200
|
+
if (!VpIsZero(r)) AddExponent(r, a->exponent - (ssize_t)(base_prec + div_prec));
|
|
201
|
+
RB_GC_GUARD(a2.bigdecimal);
|
|
202
|
+
RB_GC_GUARD(b2.bigdecimal);
|
|
203
|
+
RB_GC_GUARD(c2.bigdecimal);
|
|
204
|
+
RB_GC_GUARD(r2.bigdecimal);
|
|
205
|
+
return Qnil;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Newton-Raphson iteration converges from below, so a rounding mode that rounds toward zero
|
|
209
|
+
// (ROUND_DOWN, ROUND_FLOOR) adds error in the same direction at every iteration.
|
|
210
|
+
// Calculate with ROUND_HALF_UP and without precision limit, and restore them even if an exception is raised.
|
|
211
|
+
static void
|
|
212
|
+
VpDivdNewton(Real *c, Real *r, Real *a, Real *b)
|
|
213
|
+
{
|
|
214
|
+
struct vp_divd_newton_args args = {c, r, a, b};
|
|
215
|
+
struct vp_settings saved = {VpGetPrecLimit(), VpGetRoundMode()};
|
|
216
|
+
VpSetPrecLimit(0);
|
|
217
|
+
VpSetRoundMode(VP_ROUND_HALF_UP);
|
|
218
|
+
rb_ensure(VpDivdNewtonInner, (VALUE)&args, restore_vp_settings, (VALUE)&saved);
|
|
219
|
+
}
|
data/ext/bigdecimal/extconf.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# frozen_string_literal:
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
require 'mkmf'
|
|
3
3
|
|
|
4
4
|
def have_builtin_func(name, check_expr, opt = "", &b)
|
|
@@ -46,13 +46,16 @@ have_func("rb_opts_exception_p", "ruby.h")
|
|
|
46
46
|
have_func("rb_category_warn", "ruby.h")
|
|
47
47
|
have_const("RB_WARN_CATEGORY_DEPRECATED", "ruby.h")
|
|
48
48
|
|
|
49
|
+
if RUBY_ENGINE == "ruby"
|
|
50
|
+
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
|
|
51
|
+
end
|
|
52
|
+
|
|
49
53
|
if File.file?(File.expand_path('../lib/bigdecimal.rb', __FILE__))
|
|
50
54
|
bigdecimal_rb = "$(srcdir)/lib/bigdecimal.rb"
|
|
51
55
|
else
|
|
52
56
|
bigdecimal_rb = "$(srcdir)/../../lib/bigdecimal.rb"
|
|
53
57
|
end
|
|
54
58
|
|
|
55
|
-
$defs.push '-DBIGDECIMAL_USE_DECDIG_UINT16_T' if ENV['BIGDECIMAL_USE_DECDIG_UINT16_T'] == 'true'
|
|
56
59
|
$defs.push '-DBIGDECIMAL_USE_VP_TEST_METHODS' if ENV['BIGDECIMAL_USE_VP_TEST_METHODS'] == 'true'
|
|
57
60
|
|
|
58
61
|
create_makefile('bigdecimal') {|mf|
|