bigdecimal 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bigdecimal.c +60 -64
- data/bigdecimal.gemspec +1 -2
- data/lib/bigdecimal/math.rb +1 -14
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60d3dce1a57a777beadceb1451b2e0972b60181a
|
4
|
+
data.tar.gz: 65e7f3cab117bbbdc380fa3680187fa666b2ce9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c9dbd7e5132491e100390193c623310c24f24b65dfd5dbb351dd8254e353d0748f5c5fffb3c868fb0f889095d60080d5e2b026f74caf30eae8925a23c912c0a
|
7
|
+
data.tar.gz: 827e65f0a6ad81a6a2d499aa4d53fd566f3d67338b9294d1bf17642e24dedcd3e9c5ad562064f32ab9b5ee256d6d5f24b355bd3f094cef6ecf6d668aa21ca532
|
data/bigdecimal.c
CHANGED
@@ -190,13 +190,13 @@ cannot_be_coerced_into_BigDecimal(VALUE exc_class, VALUE v)
|
|
190
190
|
rb_exc_raise(rb_exc_new3(exc_class, str));
|
191
191
|
}
|
192
192
|
|
193
|
-
static VALUE BigDecimal_div2(
|
193
|
+
static inline VALUE BigDecimal_div2(VALUE, VALUE, VALUE);
|
194
194
|
|
195
195
|
static Real*
|
196
196
|
GetVpValueWithPrec(VALUE v, long prec, int must)
|
197
197
|
{
|
198
198
|
Real *pv;
|
199
|
-
VALUE num, bg
|
199
|
+
VALUE num, bg;
|
200
200
|
char szD[128];
|
201
201
|
VALUE orig = Qundef;
|
202
202
|
|
@@ -215,9 +215,7 @@ again:
|
|
215
215
|
pv = GetVpValueWithPrec(num, -1, must);
|
216
216
|
if (pv == NULL) goto SomeOneMayDoIt;
|
217
217
|
|
218
|
-
|
219
|
-
args[1] = LONG2NUM(prec);
|
220
|
-
v = BigDecimal_div2(2, args, ToValue(pv));
|
218
|
+
v = BigDecimal_div2(ToValue(pv), RRATIONAL(v)->den, LONG2NUM(prec));
|
221
219
|
goto again;
|
222
220
|
}
|
223
221
|
|
@@ -1456,42 +1454,55 @@ BigDecimal_divmod(VALUE self, VALUE r)
|
|
1456
1454
|
/*
|
1457
1455
|
* See BigDecimal#quo
|
1458
1456
|
*/
|
1459
|
-
static VALUE
|
1460
|
-
BigDecimal_div2(
|
1457
|
+
static inline VALUE
|
1458
|
+
BigDecimal_div2(VALUE self, VALUE b, VALUE n)
|
1461
1459
|
{
|
1462
1460
|
ENTER(5);
|
1463
|
-
|
1464
|
-
int na = rb_scan_args(argc, argv, "11", &b, &n);
|
1465
|
-
if (na == 1) { /* div in Float sense */
|
1466
|
-
Real *div = NULL;
|
1467
|
-
Real *mod;
|
1468
|
-
if (BigDecimal_DoDivmod(self, b, &div, &mod)) {
|
1469
|
-
return BigDecimal_to_i(ToValue(div));
|
1470
|
-
}
|
1471
|
-
return DoSomeOne(self, b, rb_intern("div"));
|
1472
|
-
} else { /* div in BigDecimal sense */
|
1473
|
-
SIGNED_VALUE ix = GetPositiveInt(n);
|
1474
|
-
if (ix == 0) return BigDecimal_div(self, b);
|
1475
|
-
else {
|
1476
|
-
Real *res = NULL;
|
1477
|
-
Real *av = NULL, *bv = NULL, *cv = NULL;
|
1478
|
-
size_t mx = ix + VpBaseFig()*2;
|
1479
|
-
size_t pl = VpSetPrecLimit(0);
|
1461
|
+
SIGNED_VALUE ix;
|
1480
1462
|
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1463
|
+
if (NIL_P(n)) { /* div in Float sense */
|
1464
|
+
Real *div = NULL;
|
1465
|
+
Real *mod;
|
1466
|
+
if (BigDecimal_DoDivmod(self, b, &div, &mod)) {
|
1467
|
+
return BigDecimal_to_i(ToValue(div));
|
1468
|
+
}
|
1469
|
+
return DoSomeOne(self, b, rb_intern("div"));
|
1470
|
+
}
|
1471
|
+
|
1472
|
+
/* div in BigDecimal sense */
|
1473
|
+
ix = GetPositiveInt(n);
|
1474
|
+
if (ix == 0) {
|
1475
|
+
return BigDecimal_div(self, b);
|
1476
|
+
}
|
1477
|
+
else {
|
1478
|
+
Real *res = NULL;
|
1479
|
+
Real *av = NULL, *bv = NULL, *cv = NULL;
|
1480
|
+
size_t mx = ix + VpBaseFig()*2;
|
1481
|
+
size_t pl = VpSetPrecLimit(0);
|
1482
|
+
|
1483
|
+
GUARD_OBJ(cv, VpCreateRbObject(mx, "0"));
|
1484
|
+
GUARD_OBJ(av, GetVpValue(self, 1));
|
1485
|
+
GUARD_OBJ(bv, GetVpValue(b, 1));
|
1486
|
+
mx = av->Prec + bv->Prec + 2;
|
1487
|
+
if (mx <= cv->MaxPrec) mx = cv->MaxPrec + 1;
|
1488
|
+
GUARD_OBJ(res, VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0"));
|
1489
|
+
VpDivd(cv, res, av, bv);
|
1490
|
+
VpSetPrecLimit(pl);
|
1491
|
+
VpLeftRound(cv, VpGetRoundMode(), ix);
|
1492
|
+
return ToValue(cv);
|
1492
1493
|
}
|
1493
1494
|
}
|
1494
1495
|
|
1496
|
+
static VALUE
|
1497
|
+
BigDecimal_div3(int argc, VALUE *argv, VALUE self)
|
1498
|
+
{
|
1499
|
+
VALUE b,n;
|
1500
|
+
|
1501
|
+
rb_scan_args(argc, argv, "11", &b, &n);
|
1502
|
+
|
1503
|
+
return BigDecimal_div2(self, b, n);
|
1504
|
+
}
|
1505
|
+
|
1495
1506
|
static VALUE
|
1496
1507
|
BigDecimal_add2(VALUE self, VALUE b, VALUE n)
|
1497
1508
|
{
|
@@ -2183,7 +2194,7 @@ BigDecimal_power(int argc, VALUE*argv, VALUE self)
|
|
2183
2194
|
return ToValue(y);
|
2184
2195
|
}
|
2185
2196
|
|
2186
|
-
retry:
|
2197
|
+
retry:
|
2187
2198
|
switch (TYPE(vexp)) {
|
2188
2199
|
case T_FIXNUM:
|
2189
2200
|
break;
|
@@ -2370,7 +2381,7 @@ retry:
|
|
2370
2381
|
}
|
2371
2382
|
}
|
2372
2383
|
|
2373
|
-
int_exp =
|
2384
|
+
int_exp = FIX2LONG(vexp);
|
2374
2385
|
ma = int_exp;
|
2375
2386
|
if (ma < 0) ma = -ma;
|
2376
2387
|
if (ma == 0) ma = 1;
|
@@ -2383,7 +2394,7 @@ retry:
|
|
2383
2394
|
GUARD_OBJ(y, VpCreateRbObject(1, "0"));
|
2384
2395
|
}
|
2385
2396
|
VpPower(y, x, int_exp);
|
2386
|
-
if (VpIsDef(y)) {
|
2397
|
+
if (!NIL_P(prec) && VpIsDef(y)) {
|
2387
2398
|
VpMidRound(y, VpGetRoundMode(), n);
|
2388
2399
|
}
|
2389
2400
|
return ToValue(y);
|
@@ -2664,7 +2675,7 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
|
|
2664
2675
|
{
|
2665
2676
|
ssize_t prec, n, i;
|
2666
2677
|
Real* vx = NULL;
|
2667
|
-
VALUE one, d,
|
2678
|
+
VALUE one, d, y;
|
2668
2679
|
int negative = 0;
|
2669
2680
|
int infinite = 0;
|
2670
2681
|
int nan = 0;
|
@@ -2740,14 +2751,11 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
|
|
2740
2751
|
}
|
2741
2752
|
|
2742
2753
|
one = ToValue(VpCreateRbObject(1, "1"));
|
2743
|
-
x1 = one;
|
2744
2754
|
y = one;
|
2745
2755
|
d = y;
|
2746
|
-
|
2747
|
-
i = 0;
|
2756
|
+
i = 1;
|
2748
2757
|
|
2749
2758
|
while (!VpIsZero((Real*)DATA_PTR(d))) {
|
2750
|
-
VALUE argv[2];
|
2751
2759
|
SIGNED_VALUE const ey = VpExponent10(DATA_PTR(y));
|
2752
2760
|
SIGNED_VALUE const ed = VpExponent10(DATA_PTR(d));
|
2753
2761
|
ssize_t m = n - vabs(ey - ed);
|
@@ -2761,20 +2769,14 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
|
|
2761
2769
|
m = rmpd_double_figures();
|
2762
2770
|
}
|
2763
2771
|
|
2764
|
-
|
2765
|
-
|
2766
|
-
|
2767
|
-
|
2768
|
-
argv[1] = SSIZET2NUM(m);
|
2769
|
-
d = BigDecimal_div2(2, argv, x1);
|
2770
|
-
y = BigDecimal_add(y, d);
|
2772
|
+
d = BigDecimal_mult(d, x); /* d <- d * x */
|
2773
|
+
d = BigDecimal_div2(d, SSIZET2NUM(i), SSIZET2NUM(m)); /* d <- d / i */
|
2774
|
+
y = BigDecimal_add(y, d); /* y <- y + d */
|
2775
|
+
++i; /* i <- i + 1 */
|
2771
2776
|
}
|
2772
2777
|
|
2773
2778
|
if (negative) {
|
2774
|
-
|
2775
|
-
argv[0] = y;
|
2776
|
-
argv[1] = vprec;
|
2777
|
-
return BigDecimal_div2(2, argv, one);
|
2779
|
+
return BigDecimal_div2(one, y, vprec);
|
2778
2780
|
}
|
2779
2781
|
else {
|
2780
2782
|
vprec = SSIZET2NUM(prec - VpExponent10(DATA_PTR(y)));
|
@@ -2783,10 +2785,8 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
|
|
2783
2785
|
|
2784
2786
|
RB_GC_GUARD(one);
|
2785
2787
|
RB_GC_GUARD(x);
|
2786
|
-
RB_GC_GUARD(x1);
|
2787
2788
|
RB_GC_GUARD(y);
|
2788
2789
|
RB_GC_GUARD(d);
|
2789
|
-
RB_GC_GUARD(z);
|
2790
2790
|
}
|
2791
2791
|
|
2792
2792
|
/* call-seq:
|
@@ -2807,7 +2807,7 @@ BigMath_s_log(VALUE klass, VALUE x, VALUE vprec)
|
|
2807
2807
|
ssize_t prec, n, i;
|
2808
2808
|
SIGNED_VALUE expo;
|
2809
2809
|
Real* vx = NULL;
|
2810
|
-
VALUE
|
2810
|
+
VALUE vn, one, two, w, x2, y, d;
|
2811
2811
|
int zero = 0;
|
2812
2812
|
int negative = 0;
|
2813
2813
|
int infinite = 0;
|
@@ -2913,9 +2913,7 @@ get_vp_value:
|
|
2913
2913
|
expo = 0;
|
2914
2914
|
}
|
2915
2915
|
w = BigDecimal_sub(x, one);
|
2916
|
-
|
2917
|
-
argv[1] = vn;
|
2918
|
-
x = BigDecimal_div2(2, argv, w);
|
2916
|
+
x = BigDecimal_div2(w, BigDecimal_add(x, one), vn);
|
2919
2917
|
RB_GC_GUARD(x2) = BigDecimal_mult2(x, x, vn);
|
2920
2918
|
RB_GC_GUARD(y) = x;
|
2921
2919
|
RB_GC_GUARD(d) = y;
|
@@ -2933,9 +2931,7 @@ get_vp_value:
|
|
2933
2931
|
|
2934
2932
|
x = BigDecimal_mult2(x2, x, vn);
|
2935
2933
|
i += 2;
|
2936
|
-
|
2937
|
-
argv[1] = SSIZET2NUM(m);
|
2938
|
-
d = BigDecimal_div2(2, argv, x);
|
2934
|
+
d = BigDecimal_div2(x, SSIZET2NUM(i), SSIZET2NUM(m));
|
2939
2935
|
y = BigDecimal_add(y, d);
|
2940
2936
|
}
|
2941
2937
|
|
@@ -3214,7 +3210,7 @@ Init_bigdecimal(void)
|
|
3214
3210
|
rb_define_method(rb_cBigDecimal, "add", BigDecimal_add2, 2);
|
3215
3211
|
rb_define_method(rb_cBigDecimal, "sub", BigDecimal_sub2, 2);
|
3216
3212
|
rb_define_method(rb_cBigDecimal, "mult", BigDecimal_mult2, 2);
|
3217
|
-
rb_define_method(rb_cBigDecimal, "div",
|
3213
|
+
rb_define_method(rb_cBigDecimal, "div", BigDecimal_div3, -1);
|
3218
3214
|
rb_define_method(rb_cBigDecimal, "hash", BigDecimal_hash, 0);
|
3219
3215
|
rb_define_method(rb_cBigDecimal, "to_s", BigDecimal_to_s, -1);
|
3220
3216
|
rb_define_method(rb_cBigDecimal, "to_i", BigDecimal_to_i, 0);
|
data/bigdecimal.gemspec
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# -*- ruby -*-
|
2
|
-
_VERSION = "1.2.
|
2
|
+
_VERSION = "1.2.3"
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "bigdecimal"
|
6
6
|
s.version = _VERSION
|
7
|
-
s.date = "2012-02-19"
|
8
7
|
s.summary = "Arbitrary-precision decimal floating-point number library."
|
9
8
|
s.homepage = "http://www.ruby-lang.org"
|
10
9
|
s.email = "mrkn@mrkn.jp"
|
data/lib/bigdecimal/math.rb
CHANGED
@@ -226,19 +226,6 @@ module BigMath
|
|
226
226
|
#
|
227
227
|
def E(prec)
|
228
228
|
raise ArgumentError, "Zero or negative precision for E" if prec <= 0
|
229
|
-
|
230
|
-
one = BigDecimal("1")
|
231
|
-
y = one
|
232
|
-
d = y
|
233
|
-
z = one
|
234
|
-
i = 0
|
235
|
-
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
236
|
-
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
237
|
-
i += 1
|
238
|
-
z *= i
|
239
|
-
d = one.div(z,m)
|
240
|
-
y += d
|
241
|
-
end
|
242
|
-
y
|
229
|
+
BigMath.exp(1, prec)
|
243
230
|
end
|
244
231
|
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: 1.2.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenta Murata
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2013-11-25 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: This library provides arbitrary-precision decimal floating-point number
|
16
16
|
class.
|
@@ -20,10 +20,10 @@ extensions:
|
|
20
20
|
- extconf.rb
|
21
21
|
extra_rdoc_files: []
|
22
22
|
files:
|
23
|
-
-
|
23
|
+
- README
|
24
24
|
- bigdecimal.c
|
25
|
+
- bigdecimal.gemspec
|
25
26
|
- bigdecimal.h
|
26
|
-
- README
|
27
27
|
- depend
|
28
28
|
- extconf.rb
|
29
29
|
- lib/bigdecimal/jacobian.rb
|
@@ -40,22 +40,21 @@ metadata: {}
|
|
40
40
|
post_install_message:
|
41
41
|
rdoc_options: []
|
42
42
|
require_paths:
|
43
|
-
- .
|
43
|
+
- "."
|
44
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '0'
|
54
54
|
requirements: []
|
55
55
|
rubyforge_project:
|
56
|
-
rubygems_version: 2.0.
|
56
|
+
rubygems_version: 2.2.0.preview.2
|
57
57
|
signing_key:
|
58
58
|
specification_version: 4
|
59
59
|
summary: Arbitrary-precision decimal floating-point number library.
|
60
60
|
test_files: []
|
61
|
-
has_rdoc:
|