bigdecimal 4.0.1 → 4.1.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/bigdecimal.gemspec +6 -1
- data/ext/bigdecimal/bigdecimal.c +85 -27
- data/ext/bigdecimal/bigdecimal.h +37 -36
- data/ext/bigdecimal/div.h +192 -0
- data/ext/bigdecimal/extconf.rb +1 -2
- data/ext/bigdecimal/missing.h +4 -2
- data/ext/bigdecimal/ntt.h +191 -0
- data/lib/bigdecimal/math.rb +101 -122
- data/lib/bigdecimal/util.rb +1 -1
- data/lib/bigdecimal.rb +86 -59
- data/sample/linear.rb +73 -37
- data/sample/nlsolve.rb +47 -30
- data/sample/pi.rb +2 -7
- data/sig/big_decimal.rbs +1502 -0
- data/sig/big_decimal_util.rbs +158 -0
- data/sig/big_math.rbs +423 -0
- metadata +7 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e4356e024d6f3144b6d788ea32ad1832dab72203834e2a91484edc75e7c6c08c
|
|
4
|
+
data.tar.gz: 7847d9f86e86e3f9be90a51fbfa6e6045bcdb6c1886ab1559b3fe2ebfe107356
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4bc5ce4f4fe5a041d83d8eec1d662bf35c50f8e2d2e317d659f833a9a2ee15280b6e8c3a4fa09dbaf177ad2daf34c9c15076819fb1bd4bf22b72efa8a1e9c6f5
|
|
7
|
+
data.tar.gz: d1a8c638bd997d8a882f9d2409dca84853cb6e2af768c79143554a6a316d5dd2a99f7b247fcf496079bba33faec31f93fcc4d78df5913b2350eb9655b91a2834
|
data/bigdecimal.gemspec
CHANGED
|
@@ -34,6 +34,9 @@ Gem::Specification.new do |s|
|
|
|
34
34
|
sample/linear.rb
|
|
35
35
|
sample/nlsolve.rb
|
|
36
36
|
sample/pi.rb
|
|
37
|
+
sig/big_decimal_util.rbs
|
|
38
|
+
sig/big_decimal.rbs
|
|
39
|
+
sig/big_math.rbs
|
|
37
40
|
]
|
|
38
41
|
if Gem::Platform === s.platform and s.platform =~ 'java' or RUBY_ENGINE == 'jruby'
|
|
39
42
|
s.platform = 'java'
|
|
@@ -43,15 +46,17 @@ Gem::Specification.new do |s|
|
|
|
43
46
|
ext/bigdecimal/bigdecimal.c
|
|
44
47
|
ext/bigdecimal/bigdecimal.h
|
|
45
48
|
ext/bigdecimal/bits.h
|
|
49
|
+
ext/bigdecimal/div.h
|
|
46
50
|
ext/bigdecimal/feature.h
|
|
47
51
|
ext/bigdecimal/missing.c
|
|
48
52
|
ext/bigdecimal/missing.h
|
|
53
|
+
ext/bigdecimal/ntt.h
|
|
49
54
|
ext/bigdecimal/missing/dtoa.c
|
|
50
55
|
ext/bigdecimal/static_assert.h
|
|
51
56
|
]
|
|
52
57
|
end
|
|
53
58
|
|
|
54
|
-
s.required_ruby_version = Gem::Requirement.new(">= 2.
|
|
59
|
+
s.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
|
|
55
60
|
|
|
56
61
|
s.metadata["changelog_uri"] = s.homepage + "/blob/master/CHANGES.md"
|
|
57
62
|
end
|
data/ext/bigdecimal/bigdecimal.c
CHANGED
|
@@ -29,12 +29,14 @@
|
|
|
29
29
|
#endif
|
|
30
30
|
|
|
31
31
|
#include "bits.h"
|
|
32
|
+
#include "ntt.h"
|
|
33
|
+
#include "div.h"
|
|
32
34
|
#include "static_assert.h"
|
|
33
35
|
|
|
34
|
-
#define BIGDECIMAL_VERSION "4.0
|
|
35
|
-
|
|
36
|
-
/* #define ENABLE_NUMERIC_STRING */
|
|
36
|
+
#define BIGDECIMAL_VERSION "4.1.0"
|
|
37
37
|
|
|
38
|
+
#define NTT_MULTIPLICATION_THRESHOLD 100
|
|
39
|
+
#define NEWTON_RAPHSON_DIVISION_THRESHOLD 200
|
|
38
40
|
#define SIGNED_VALUE_MAX INTPTR_MAX
|
|
39
41
|
#define SIGNED_VALUE_MIN INTPTR_MIN
|
|
40
42
|
#define MUL_OVERFLOW_SIGNED_VALUE_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, SIGNED_VALUE_MIN, SIGNED_VALUE_MAX)
|
|
@@ -75,11 +77,6 @@ static struct {
|
|
|
75
77
|
uint8_t mode;
|
|
76
78
|
} rbd_rounding_modes[RBD_NUM_ROUNDING_MODES];
|
|
77
79
|
|
|
78
|
-
typedef struct {
|
|
79
|
-
VALUE bigdecimal;
|
|
80
|
-
Real *real;
|
|
81
|
-
} BDVALUE;
|
|
82
|
-
|
|
83
80
|
typedef struct {
|
|
84
81
|
VALUE bigdecimal_or_nil;
|
|
85
82
|
Real *real_or_null;
|
|
@@ -207,7 +204,6 @@ rbd_allocate_struct_zero(int sign, size_t const digits)
|
|
|
207
204
|
static unsigned short VpGetException(void);
|
|
208
205
|
static void VpSetException(unsigned short f);
|
|
209
206
|
static void VpCheckException(Real *p, bool always);
|
|
210
|
-
static int AddExponent(Real *a, SIGNED_VALUE n);
|
|
211
207
|
static VALUE CheckGetValue(BDVALUE v);
|
|
212
208
|
static void VpInternalRound(Real *c, size_t ixDigit, DECDIG vPrev, DECDIG v);
|
|
213
209
|
static int VpLimitRound(Real *c, size_t ixDigit);
|
|
@@ -336,14 +332,6 @@ GetBDValueWithPrecInternal(VALUE v, size_t prec, int must)
|
|
|
336
332
|
break;
|
|
337
333
|
}
|
|
338
334
|
|
|
339
|
-
#ifdef ENABLE_NUMERIC_STRING
|
|
340
|
-
case T_STRING: {
|
|
341
|
-
const char *c_str = StringValueCStr(v);
|
|
342
|
-
v = rb_cstr_convert_to_BigDecimal(c_str, must);
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
#endif /* ENABLE_NUMERIC_STRING */
|
|
346
|
-
|
|
347
335
|
default:
|
|
348
336
|
goto SomeOneMayDoIt;
|
|
349
337
|
}
|
|
@@ -996,7 +984,7 @@ BigDecimal_mode(int argc, VALUE *argv, VALUE self)
|
|
|
996
984
|
static size_t
|
|
997
985
|
GetAddSubPrec(Real *a, Real *b)
|
|
998
986
|
{
|
|
999
|
-
if (
|
|
987
|
+
if (VpIsZero(a) || VpIsZero(b)) return Max(a->Prec, b->Prec);
|
|
1000
988
|
ssize_t min_a = a->exponent - a->Prec;
|
|
1001
989
|
ssize_t min_b = b->exponent - b->Prec;
|
|
1002
990
|
return Max(a->exponent, b->exponent) - Min(min_a, min_b);
|
|
@@ -1081,9 +1069,6 @@ BigDecimal_check_num(Real *p)
|
|
|
1081
1069
|
VpCheckException(p, true);
|
|
1082
1070
|
}
|
|
1083
1071
|
|
|
1084
|
-
static VALUE BigDecimal_fix(VALUE self);
|
|
1085
|
-
static VALUE BigDecimal_split(VALUE self);
|
|
1086
|
-
|
|
1087
1072
|
/* Returns the value as an Integer.
|
|
1088
1073
|
*
|
|
1089
1074
|
* If the BigDecimal is infinity or NaN, raises FloatDomainError.
|
|
@@ -1300,13 +1285,32 @@ BigDecimal_addsub_with_coerce(VALUE self, VALUE r, size_t prec, int operation)
|
|
|
1300
1285
|
if (VpIsNaN(a.real)) return CheckGetValue(a);
|
|
1301
1286
|
if (VpIsNaN(b.real)) return CheckGetValue(b);
|
|
1302
1287
|
|
|
1303
|
-
|
|
1304
|
-
if (mx == (size_t)-1L) {
|
|
1305
|
-
/* a or b is inf */
|
|
1288
|
+
if (VpIsInf(a.real) || VpIsInf(b.real)) {
|
|
1306
1289
|
c = NewZeroWrap(1, BASE_FIG);
|
|
1307
1290
|
VpAddSub(c.real, a.real, b.real, operation);
|
|
1308
1291
|
}
|
|
1309
1292
|
else {
|
|
1293
|
+
|
|
1294
|
+
// Optimization when exponent difference is large
|
|
1295
|
+
// (1.234e+1000).add(5.678e-1000, 10) == (1.234e+1000).add(0.1e+990, 10) in every rounding mode
|
|
1296
|
+
if (prec && !VpIsZero(a.real) && !VpIsZero(b.real)) {
|
|
1297
|
+
size_t precRoom = roomof(prec, BASE_FIG);
|
|
1298
|
+
if (a.real->exponent - (ssize_t)Max(a.real->Prec, precRoom) - 1 > b.real->exponent) {
|
|
1299
|
+
BDVALUE b2 = NewZeroWrap(1, BASE_FIG);
|
|
1300
|
+
VpSetOne(b2.real)
|
|
1301
|
+
VpSetSign(b2.real, b.real->sign);
|
|
1302
|
+
b2.real->exponent = a.real->exponent - (ssize_t)Max(a.real->Prec, precRoom) - 1;
|
|
1303
|
+
b = b2;
|
|
1304
|
+
} else if (b.real->exponent - (ssize_t)Max(b.real->Prec, precRoom) - 1 > a.real->exponent) {
|
|
1305
|
+
BDVALUE a2 = NewZeroWrap(1, BASE_FIG);
|
|
1306
|
+
VpSetOne(a2.real)
|
|
1307
|
+
VpSetSign(a2.real, a.real->sign);
|
|
1308
|
+
a2.real->exponent = b.real->exponent - (ssize_t)Max(b.real->Prec, precRoom) - 1;
|
|
1309
|
+
a = a2;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
mx = GetAddSubPrec(a.real, b.real);
|
|
1310
1314
|
c = NewZeroWrap(1, (mx + 1) * BASE_FIG);
|
|
1311
1315
|
size_t pl = VpGetPrecLimit();
|
|
1312
1316
|
if (prec) VpSetPrecLimit(prec);
|
|
@@ -3226,19 +3230,39 @@ BigDecimal_literal(const char *str)
|
|
|
3226
3230
|
|
|
3227
3231
|
#ifdef BIGDECIMAL_USE_VP_TEST_METHODS
|
|
3228
3232
|
VALUE
|
|
3229
|
-
|
|
3230
|
-
BDVALUE a,b,c,d;
|
|
3233
|
+
BigDecimal_vpdivd_generic(VALUE self, VALUE r, VALUE cprec, void (*vpdivd_func)(Real*, Real*, Real*, Real*)) {
|
|
3234
|
+
BDVALUE a, b, c, d;
|
|
3231
3235
|
size_t cn = NUM2INT(cprec);
|
|
3232
3236
|
a = GetBDValueMust(self);
|
|
3233
3237
|
b = GetBDValueMust(r);
|
|
3234
3238
|
c = NewZeroWrap(1, cn * BASE_FIG);
|
|
3235
3239
|
d = NewZeroWrap(1, VPDIVD_REM_PREC(a.real, b.real, c.real) * BASE_FIG);
|
|
3236
|
-
|
|
3240
|
+
vpdivd_func(c.real, d.real, a.real, b.real);
|
|
3237
3241
|
RB_GC_GUARD(a.bigdecimal);
|
|
3238
3242
|
RB_GC_GUARD(b.bigdecimal);
|
|
3239
3243
|
return rb_assoc_new(c.bigdecimal, d.bigdecimal);
|
|
3240
3244
|
}
|
|
3241
3245
|
|
|
3246
|
+
void
|
|
3247
|
+
VpDivdNormal(Real *c, Real *r, Real *a, Real *b) {
|
|
3248
|
+
VpDivd(c, r, a, b);
|
|
3249
|
+
}
|
|
3250
|
+
|
|
3251
|
+
VALUE
|
|
3252
|
+
BigDecimal_vpdivd(VALUE self, VALUE r, VALUE cprec) {
|
|
3253
|
+
return BigDecimal_vpdivd_generic(self, r, cprec, VpDivdNormal);
|
|
3254
|
+
}
|
|
3255
|
+
|
|
3256
|
+
VALUE
|
|
3257
|
+
BigDecimal_vpdivd_newton(VALUE self, VALUE r, VALUE cprec) {
|
|
3258
|
+
return BigDecimal_vpdivd_generic(self, r, cprec, VpDivdNewton);
|
|
3259
|
+
}
|
|
3260
|
+
|
|
3261
|
+
VALUE
|
|
3262
|
+
BigDecimal_newton_raphson_inverse(VALUE self, VALUE prec) {
|
|
3263
|
+
return newton_raphson_inverse(self, NUM2SIZET(prec));
|
|
3264
|
+
}
|
|
3265
|
+
|
|
3242
3266
|
VALUE
|
|
3243
3267
|
BigDecimal_vpmult(VALUE self, VALUE v) {
|
|
3244
3268
|
BDVALUE a,b,c;
|
|
@@ -3250,6 +3274,23 @@ BigDecimal_vpmult(VALUE self, VALUE v) {
|
|
|
3250
3274
|
RB_GC_GUARD(b.bigdecimal);
|
|
3251
3275
|
return c.bigdecimal;
|
|
3252
3276
|
}
|
|
3277
|
+
|
|
3278
|
+
VALUE
|
|
3279
|
+
BigDecimal_nttmult(VALUE self, VALUE v) {
|
|
3280
|
+
BDVALUE a,b,c;
|
|
3281
|
+
a = GetBDValueMust(self);
|
|
3282
|
+
b = GetBDValueMust(v);
|
|
3283
|
+
c = NewZeroWrap(1, VPMULT_RESULT_PREC(a.real, b.real) * BASE_FIG);
|
|
3284
|
+
ntt_multiply(a.real->Prec, b.real->Prec, a.real->frac, b.real->frac, c.real->frac);
|
|
3285
|
+
VpSetSign(c.real, a.real->sign * b.real->sign);
|
|
3286
|
+
c.real->exponent = a.real->exponent + b.real->exponent;
|
|
3287
|
+
c.real->Prec = a.real->Prec + b.real->Prec;
|
|
3288
|
+
VpNmlz(c.real);
|
|
3289
|
+
RB_GC_GUARD(a.bigdecimal);
|
|
3290
|
+
RB_GC_GUARD(b.bigdecimal);
|
|
3291
|
+
return c.bigdecimal;
|
|
3292
|
+
}
|
|
3293
|
+
|
|
3253
3294
|
#endif /* BIGDECIMAL_USE_VP_TEST_METHODS */
|
|
3254
3295
|
|
|
3255
3296
|
/* Document-class: BigDecimal
|
|
@@ -3620,7 +3661,10 @@ Init_bigdecimal(void)
|
|
|
3620
3661
|
|
|
3621
3662
|
#ifdef BIGDECIMAL_USE_VP_TEST_METHODS
|
|
3622
3663
|
rb_define_method(rb_cBigDecimal, "vpdivd", BigDecimal_vpdivd, 2);
|
|
3664
|
+
rb_define_method(rb_cBigDecimal, "vpdivd_newton", BigDecimal_vpdivd_newton, 2);
|
|
3665
|
+
rb_define_method(rb_cBigDecimal, "newton_raphson_inverse", BigDecimal_newton_raphson_inverse, 1);
|
|
3623
3666
|
rb_define_method(rb_cBigDecimal, "vpmult", BigDecimal_vpmult, 1);
|
|
3667
|
+
rb_define_method(rb_cBigDecimal, "nttmult", BigDecimal_nttmult, 1);
|
|
3624
3668
|
#endif /* BIGDECIMAL_USE_VP_TEST_METHODS */
|
|
3625
3669
|
|
|
3626
3670
|
#define ROUNDING_MODE(i, name, value) \
|
|
@@ -4903,6 +4947,13 @@ VpMult(Real *c, Real *a, Real *b)
|
|
|
4903
4947
|
c->exponent = a->exponent; /* set exponent */
|
|
4904
4948
|
VpSetSign(c, VpGetSign(a) * VpGetSign(b)); /* set sign */
|
|
4905
4949
|
if (!AddExponent(c, b->exponent)) return 0;
|
|
4950
|
+
|
|
4951
|
+
if (b->Prec >= NTT_MULTIPLICATION_THRESHOLD) {
|
|
4952
|
+
ntt_multiply(a->Prec, b->Prec, a->frac, b->frac, c->frac);
|
|
4953
|
+
c->Prec = a->Prec + b->Prec;
|
|
4954
|
+
goto Cleanup;
|
|
4955
|
+
}
|
|
4956
|
+
|
|
4906
4957
|
carry = 0;
|
|
4907
4958
|
nc = ind_c = MxIndAB;
|
|
4908
4959
|
memset(c->frac, 0, (nc + 1) * sizeof(DECDIG)); /* Initialize c */
|
|
@@ -4949,6 +5000,8 @@ VpMult(Real *c, Real *a, Real *b)
|
|
|
4949
5000
|
}
|
|
4950
5001
|
}
|
|
4951
5002
|
}
|
|
5003
|
+
|
|
5004
|
+
Cleanup:
|
|
4952
5005
|
VpNmlz(c);
|
|
4953
5006
|
|
|
4954
5007
|
Exit:
|
|
@@ -4996,6 +5049,11 @@ VpDivd(Real *c, Real *r, Real *a, Real *b)
|
|
|
4996
5049
|
|
|
4997
5050
|
if (word_a > word_r || word_b + word_c - 2 >= word_r) goto space_error;
|
|
4998
5051
|
|
|
5052
|
+
if (word_c >= NEWTON_RAPHSON_DIVISION_THRESHOLD && word_b >= NEWTON_RAPHSON_DIVISION_THRESHOLD) {
|
|
5053
|
+
VpDivdNewton(c, r, a, b);
|
|
5054
|
+
goto Exit;
|
|
5055
|
+
}
|
|
5056
|
+
|
|
4999
5057
|
for (i = 0; i < word_a; ++i) r->frac[i] = a->frac[i];
|
|
5000
5058
|
for (i = word_a; i < word_r; ++i) r->frac[i] = 0;
|
|
5001
5059
|
for (i = 0; i < word_c; ++i) c->frac[i] = 0;
|
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,11 @@ 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
|
+
|
|
191
171
|
/*
|
|
192
172
|
* ------------------
|
|
193
173
|
* EXPORTables.
|
|
@@ -232,10 +212,31 @@ VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
|
|
|
232
212
|
VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
|
|
233
213
|
VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
|
|
234
214
|
VP_EXPORT void VpFrac(Real *y, Real *x);
|
|
215
|
+
VP_EXPORT int AddExponent(Real *a, SIGNED_VALUE n);
|
|
235
216
|
|
|
236
217
|
/* VP constants */
|
|
237
218
|
VP_EXPORT Real *VpOne(void);
|
|
238
219
|
|
|
220
|
+
/*
|
|
221
|
+
* **** BigDecimal part ****
|
|
222
|
+
*/
|
|
223
|
+
VP_EXPORT VALUE BigDecimal_lt(VALUE self, VALUE r);
|
|
224
|
+
VP_EXPORT VALUE BigDecimal_ge(VALUE self, VALUE r);
|
|
225
|
+
VP_EXPORT VALUE BigDecimal_exponent(VALUE self);
|
|
226
|
+
VP_EXPORT VALUE BigDecimal_fix(VALUE self);
|
|
227
|
+
VP_EXPORT VALUE BigDecimal_frac(VALUE self);
|
|
228
|
+
VP_EXPORT VALUE BigDecimal_add(VALUE self, VALUE b);
|
|
229
|
+
VP_EXPORT VALUE BigDecimal_sub(VALUE self, VALUE b);
|
|
230
|
+
VP_EXPORT VALUE BigDecimal_mult(VALUE self, VALUE b);
|
|
231
|
+
VP_EXPORT VALUE BigDecimal_add2(VALUE self, VALUE b, VALUE n);
|
|
232
|
+
VP_EXPORT VALUE BigDecimal_sub2(VALUE self, VALUE b, VALUE n);
|
|
233
|
+
VP_EXPORT VALUE BigDecimal_mult2(VALUE self, VALUE b, VALUE n);
|
|
234
|
+
VP_EXPORT VALUE BigDecimal_split(VALUE self);
|
|
235
|
+
VP_EXPORT VALUE BigDecimal_decimal_shift(VALUE self, VALUE v);
|
|
236
|
+
VP_EXPORT inline BDVALUE GetBDValueMust(VALUE v);
|
|
237
|
+
VP_EXPORT inline BDVALUE rbd_allocate_struct_zero_wrap(int sign, size_t const digits);
|
|
238
|
+
#define NewZeroWrap rbd_allocate_struct_zero_wrap
|
|
239
|
+
|
|
239
240
|
/*
|
|
240
241
|
* ------------------
|
|
241
242
|
* MACRO definitions.
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Calculate the inverse of x using the Newton-Raphson method.
|
|
2
|
+
static VALUE
|
|
3
|
+
newton_raphson_inverse(VALUE x, size_t prec) {
|
|
4
|
+
BDVALUE bdone = NewZeroWrap(1, 1);
|
|
5
|
+
VpSetOne(bdone.real);
|
|
6
|
+
VALUE one = bdone.bigdecimal;
|
|
7
|
+
|
|
8
|
+
// Initial approximation in 2 digits
|
|
9
|
+
BDVALUE bdx = GetBDValueMust(x);
|
|
10
|
+
BDVALUE inv0 = NewZeroWrap(1, 2 * BIGDECIMAL_COMPONENT_FIGURES);
|
|
11
|
+
VpSetOne(inv0.real);
|
|
12
|
+
DECDIG_DBL numerator = (DECDIG_DBL)BIGDECIMAL_BASE * 100;
|
|
13
|
+
DECDIG_DBL denominator = (DECDIG_DBL)bdx.real->frac[0] * 100 + (DECDIG_DBL)(bdx.real->Prec >= 2 ? bdx.real->frac[1] : 0) * 100 / BIGDECIMAL_BASE;
|
|
14
|
+
inv0.real->frac[0] = (DECDIG)(numerator / denominator);
|
|
15
|
+
inv0.real->frac[1] = (DECDIG)((numerator % denominator) * (BIGDECIMAL_BASE / 100) / denominator * 100);
|
|
16
|
+
inv0.real->Prec = 2;
|
|
17
|
+
inv0.real->exponent = 1 - bdx.real->exponent;
|
|
18
|
+
VpNmlz(inv0.real);
|
|
19
|
+
RB_GC_GUARD(bdx.bigdecimal);
|
|
20
|
+
VALUE inv = inv0.bigdecimal;
|
|
21
|
+
|
|
22
|
+
int bl = 1;
|
|
23
|
+
while (((size_t)1 << bl) < prec) bl++;
|
|
24
|
+
|
|
25
|
+
for (int i = bl; i >= 0; i--) {
|
|
26
|
+
size_t n = (prec >> i) + 2;
|
|
27
|
+
if (n > prec) n = prec;
|
|
28
|
+
// Newton-Raphson iteration: inv_next = inv + inv * (1 - x * inv)
|
|
29
|
+
VALUE one_minus_x_inv = BigDecimal_sub2(
|
|
30
|
+
one,
|
|
31
|
+
BigDecimal_mult(BigDecimal_mult2(x, one, SIZET2NUM(n + 1)), inv),
|
|
32
|
+
SIZET2NUM(SIZET2NUM(n / 2))
|
|
33
|
+
);
|
|
34
|
+
inv = BigDecimal_add2(
|
|
35
|
+
inv,
|
|
36
|
+
BigDecimal_mult(inv, one_minus_x_inv),
|
|
37
|
+
SIZET2NUM(n)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return inv;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Calculates divmod by multiplying approximate reciprocal of y
|
|
44
|
+
static void
|
|
45
|
+
divmod_by_inv_mul(VALUE x, VALUE y, VALUE inv, VALUE *res_div, VALUE *res_mod) {
|
|
46
|
+
VALUE div = BigDecimal_fix(BigDecimal_mult(x, inv));
|
|
47
|
+
VALUE mod = BigDecimal_sub(x, BigDecimal_mult(div, y));
|
|
48
|
+
while (RTEST(BigDecimal_lt(mod, INT2FIX(0)))) {
|
|
49
|
+
mod = BigDecimal_add(mod, y);
|
|
50
|
+
div = BigDecimal_sub(div, INT2FIX(1));
|
|
51
|
+
}
|
|
52
|
+
while (RTEST(BigDecimal_ge(mod, y))) {
|
|
53
|
+
mod = BigDecimal_sub(mod, y);
|
|
54
|
+
div = BigDecimal_add(div, INT2FIX(1));
|
|
55
|
+
}
|
|
56
|
+
*res_div = div;
|
|
57
|
+
*res_mod = mod;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static void
|
|
61
|
+
slice_copy(DECDIG *dest, Real *src, size_t rshift, size_t length) {
|
|
62
|
+
ssize_t start = src->exponent - rshift - length;
|
|
63
|
+
if (start >= (ssize_t)src->Prec) return;
|
|
64
|
+
if (start < 0) {
|
|
65
|
+
dest -= start;
|
|
66
|
+
length += start;
|
|
67
|
+
start = 0;
|
|
68
|
+
}
|
|
69
|
+
size_t max_length = src->Prec - start;
|
|
70
|
+
memcpy(dest, src->frac + start, Min(length, max_length) * sizeof(DECDIG));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* Calculates divmod using Newton-Raphson method.
|
|
74
|
+
* x and y must be a BigDecimal representing an integer value.
|
|
75
|
+
*
|
|
76
|
+
* To calculate with low cost, we need to split x into blocks and perform divmod for each block.
|
|
77
|
+
* x_digits = remaining_digits(<= y_digits) + block_digits * num_blocks
|
|
78
|
+
*
|
|
79
|
+
* Example:
|
|
80
|
+
* xxx_xxxxx_xxxxx_xxxxx(18 digits) / yyyyy(5 digits)
|
|
81
|
+
* remaining_digits = 3, block_digits = 5, num_blocks = 3
|
|
82
|
+
* repeating xxxxx_xxxxxx.divmod(yyyyy) calculation 3 times.
|
|
83
|
+
*
|
|
84
|
+
* In each divmod step, dividend is at most (y_digits + block_digits) digits and divisor is y_digits digits.
|
|
85
|
+
* Reciprocal of y needs block_digits + 1 precision.
|
|
86
|
+
*/
|
|
87
|
+
static void
|
|
88
|
+
divmod_newton(VALUE x, VALUE y, VALUE *div_out, VALUE *mod_out) {
|
|
89
|
+
size_t x_digits = NUM2SIZET(BigDecimal_exponent(x));
|
|
90
|
+
size_t y_digits = NUM2SIZET(BigDecimal_exponent(y));
|
|
91
|
+
if (x_digits <= y_digits) x_digits = y_digits + 1;
|
|
92
|
+
|
|
93
|
+
size_t n = x_digits / y_digits;
|
|
94
|
+
size_t block_figs = (x_digits - y_digits) / n / BIGDECIMAL_COMPONENT_FIGURES + 1;
|
|
95
|
+
size_t block_digits = block_figs * BIGDECIMAL_COMPONENT_FIGURES;
|
|
96
|
+
size_t num_blocks = (x_digits - y_digits + block_digits - 1) / block_digits;
|
|
97
|
+
size_t y_figs = (y_digits - 1) / BIGDECIMAL_COMPONENT_FIGURES + 1;
|
|
98
|
+
VALUE yinv = newton_raphson_inverse(y, block_digits + 1);
|
|
99
|
+
|
|
100
|
+
BDVALUE divident = NewZeroWrap(1, BIGDECIMAL_COMPONENT_FIGURES * (y_figs + block_figs));
|
|
101
|
+
BDVALUE div_result = NewZeroWrap(1, BIGDECIMAL_COMPONENT_FIGURES * (num_blocks * block_figs + 1));
|
|
102
|
+
BDVALUE bdx = GetBDValueMust(x);
|
|
103
|
+
|
|
104
|
+
VALUE mod = BigDecimal_fix(BigDecimal_decimal_shift(x, SSIZET2NUM(-num_blocks * block_digits)));
|
|
105
|
+
for (ssize_t i = num_blocks - 1; i >= 0; i--) {
|
|
106
|
+
memset(divident.real->frac, 0, (y_figs + block_figs) * sizeof(DECDIG));
|
|
107
|
+
|
|
108
|
+
BDVALUE bdmod = GetBDValueMust(mod);
|
|
109
|
+
slice_copy(divident.real->frac, bdmod.real, 0, y_figs);
|
|
110
|
+
slice_copy(divident.real->frac + y_figs, bdx.real, i * block_figs, block_figs);
|
|
111
|
+
RB_GC_GUARD(bdmod.bigdecimal);
|
|
112
|
+
|
|
113
|
+
VpSetSign(divident.real, 1);
|
|
114
|
+
divident.real->exponent = y_figs + block_figs;
|
|
115
|
+
divident.real->Prec = y_figs + block_figs;
|
|
116
|
+
VpNmlz(divident.real);
|
|
117
|
+
|
|
118
|
+
VALUE div;
|
|
119
|
+
divmod_by_inv_mul(divident.bigdecimal, y, yinv, &div, &mod);
|
|
120
|
+
BDVALUE bddiv = GetBDValueMust(div);
|
|
121
|
+
slice_copy(div_result.real->frac + (num_blocks - i - 1) * block_figs, bddiv.real, 0, block_figs + 1);
|
|
122
|
+
RB_GC_GUARD(bddiv.bigdecimal);
|
|
123
|
+
}
|
|
124
|
+
VpSetSign(div_result.real, 1);
|
|
125
|
+
div_result.real->exponent = num_blocks * block_figs + 1;
|
|
126
|
+
div_result.real->Prec = num_blocks * block_figs + 1;
|
|
127
|
+
VpNmlz(div_result.real);
|
|
128
|
+
RB_GC_GUARD(bdx.bigdecimal);
|
|
129
|
+
RB_GC_GUARD(divident.bigdecimal);
|
|
130
|
+
RB_GC_GUARD(div_result.bigdecimal);
|
|
131
|
+
*div_out = div_result.bigdecimal;
|
|
132
|
+
*mod_out = mod;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static VALUE
|
|
136
|
+
VpDivdNewtonInner(VALUE args_ptr)
|
|
137
|
+
{
|
|
138
|
+
Real **args = (Real**)args_ptr;
|
|
139
|
+
Real *c = args[0], *r = args[1], *a = args[2], *b = args[3];
|
|
140
|
+
BDVALUE a2, b2, c2, r2;
|
|
141
|
+
VALUE div, mod, a2_frac = Qnil;
|
|
142
|
+
size_t div_prec = c->MaxPrec - 1;
|
|
143
|
+
size_t base_prec = b->Prec;
|
|
144
|
+
|
|
145
|
+
a2 = NewZeroWrap(1, a->Prec * BIGDECIMAL_COMPONENT_FIGURES);
|
|
146
|
+
b2 = NewZeroWrap(1, b->Prec * BIGDECIMAL_COMPONENT_FIGURES);
|
|
147
|
+
VpAsgn(a2.real, a, 1);
|
|
148
|
+
VpAsgn(b2.real, b, 1);
|
|
149
|
+
VpSetSign(a2.real, 1);
|
|
150
|
+
VpSetSign(b2.real, 1);
|
|
151
|
+
a2.real->exponent = base_prec + div_prec;
|
|
152
|
+
b2.real->exponent = base_prec;
|
|
153
|
+
|
|
154
|
+
if ((ssize_t)a2.real->Prec > a2.real->exponent) {
|
|
155
|
+
a2_frac = BigDecimal_frac(a2.bigdecimal);
|
|
156
|
+
VpMidRound(a2.real, VP_ROUND_DOWN, 0);
|
|
157
|
+
}
|
|
158
|
+
divmod_newton(a2.bigdecimal, b2.bigdecimal, &div, &mod);
|
|
159
|
+
if (a2_frac != Qnil) mod = BigDecimal_add(mod, a2_frac);
|
|
160
|
+
|
|
161
|
+
c2 = GetBDValueMust(div);
|
|
162
|
+
r2 = GetBDValueMust(mod);
|
|
163
|
+
VpAsgn(c, c2.real, VpGetSign(a) * VpGetSign(b));
|
|
164
|
+
VpAsgn(r, r2.real, VpGetSign(a));
|
|
165
|
+
AddExponent(c, a->exponent);
|
|
166
|
+
AddExponent(c, -b->exponent);
|
|
167
|
+
AddExponent(c, -div_prec);
|
|
168
|
+
AddExponent(r, a->exponent);
|
|
169
|
+
AddExponent(r, -base_prec - div_prec);
|
|
170
|
+
RB_GC_GUARD(a2.bigdecimal);
|
|
171
|
+
RB_GC_GUARD(a2.bigdecimal);
|
|
172
|
+
RB_GC_GUARD(c2.bigdecimal);
|
|
173
|
+
RB_GC_GUARD(r2.bigdecimal);
|
|
174
|
+
return Qnil;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static VALUE
|
|
178
|
+
ensure_restore_prec_limit(VALUE limit)
|
|
179
|
+
{
|
|
180
|
+
VpSetPrecLimit(NUM2SIZET(limit));
|
|
181
|
+
return Qnil;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static void
|
|
185
|
+
VpDivdNewton(Real *c, Real *r, Real *a, Real *b)
|
|
186
|
+
{
|
|
187
|
+
Real *args[4] = {c, r, a, b};
|
|
188
|
+
size_t pl = VpGetPrecLimit();
|
|
189
|
+
VpSetPrecLimit(0);
|
|
190
|
+
// Ensure restoring prec limit because some methods used in VpDivdNewtonInner may raise an exception
|
|
191
|
+
rb_ensure(VpDivdNewtonInner, (VALUE)args, ensure_restore_prec_limit, SIZET2NUM(pl));
|
|
192
|
+
}
|
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)
|
|
@@ -52,7 +52,6 @@ else
|
|
|
52
52
|
bigdecimal_rb = "$(srcdir)/../../lib/bigdecimal.rb"
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
$defs.push '-DBIGDECIMAL_USE_DECDIG_UINT16_T' if ENV['BIGDECIMAL_USE_DECDIG_UINT16_T'] == 'true'
|
|
56
55
|
$defs.push '-DBIGDECIMAL_USE_VP_TEST_METHODS' if ENV['BIGDECIMAL_USE_VP_TEST_METHODS'] == 'true'
|
|
57
56
|
|
|
58
57
|
create_makefile('bigdecimal') {|mf|
|
data/ext/bigdecimal/missing.h
CHANGED
|
@@ -58,7 +58,7 @@ char *BigDecimal_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, c
|
|
|
58
58
|
|
|
59
59
|
#ifndef HAVE_RB_COMPLEX_REAL
|
|
60
60
|
static inline VALUE
|
|
61
|
-
|
|
61
|
+
rb_complex_real_fallback(VALUE cmp)
|
|
62
62
|
{
|
|
63
63
|
#ifdef RCOMPLEX
|
|
64
64
|
return RCOMPLEX(cmp)->real;
|
|
@@ -66,11 +66,12 @@ rb_complex_real(VALUE cmp)
|
|
|
66
66
|
return rb_funcall(cmp, rb_intern("real"), 0);
|
|
67
67
|
#endif
|
|
68
68
|
}
|
|
69
|
+
#define rb_complex_real rb_complex_real_fallback
|
|
69
70
|
#endif
|
|
70
71
|
|
|
71
72
|
#ifndef HAVE_RB_COMPLEX_IMAG
|
|
72
73
|
static inline VALUE
|
|
73
|
-
|
|
74
|
+
rb_complex_imag_fallback(VALUE cmp)
|
|
74
75
|
{
|
|
75
76
|
# ifdef RCOMPLEX
|
|
76
77
|
return RCOMPLEX(cmp)->imag;
|
|
@@ -78,6 +79,7 @@ rb_complex_imag(VALUE cmp)
|
|
|
78
79
|
return rb_funcall(cmp, rb_intern("imag"), 0);
|
|
79
80
|
# endif
|
|
80
81
|
}
|
|
82
|
+
#define rb_complex_imag rb_complex_imag_fallback
|
|
81
83
|
#endif
|
|
82
84
|
|
|
83
85
|
/* st */
|