bigdecimal 1.2.1 → 1.2.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea6ac110b94c2c87c1edad365c8f07966d1f43d6
4
- data.tar.gz: 5c3f0712afa20b5dc28dd150af767ccaebf1fe2f
3
+ metadata.gz: c97cb6ce62111804b296b14c294e00833500b5d9
4
+ data.tar.gz: d6c8d262b5acb0d1e5ff652ffc2b63a5acad8c21
5
5
  SHA512:
6
- metadata.gz: 3602737a5ef698b032a9d749791abba58ea73d77ce8b7e8b484c4b79588e2b5fe5388a084101e63b67646c81dd7e4c6bd2d1d5691d5c0db737bc6be122fb1981
7
- data.tar.gz: 280962bae9dbe5e7bdb3edb38d53e16e9dc449fa27c3156062145d2612f4e2ffecdd91ec496e77e07da4dcd647ad4bad7c6d9b86675eebf3b71e0068d7323ed6
6
+ metadata.gz: d69e3aa32a795c3ff76b080198867a7cff700a87ad373be151ead80f9035168216139c0db73d5c1bce8fd044cd863ef21cac7351b0d49b30fe35829b7696404a
7
+ data.tar.gz: ca5224f1997fb8c31decf7b41a26a05134efd6b6f5284dc576c89c6d51061eb58d51d72b5f05c008e063c5c73646aab167c4ef4fc226981b99ebf4ba0ac84070
data/bigdecimal.c CHANGED
@@ -146,6 +146,9 @@ BigDecimal_memsize(const void *ptr)
146
146
  static const rb_data_type_t BigDecimal_data_type = {
147
147
  "BigDecimal",
148
148
  { 0, BigDecimal_delete, BigDecimal_memsize, },
149
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
150
+ NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
151
+ #endif
149
152
  };
150
153
 
151
154
  static inline int
@@ -449,10 +452,10 @@ check_rounding_mode(VALUE const v)
449
452
  * When computation continues, results are as follows:
450
453
  *
451
454
  * EXCEPTION_NaN:: NaN
452
- * EXCEPTION_INFINITY:: +infinity or -infinity
455
+ * EXCEPTION_INFINITY:: +Infinity or -Infinity
453
456
  * EXCEPTION_UNDERFLOW:: 0
454
- * EXCEPTION_OVERFLOW:: +infinity or -infinity
455
- * EXCEPTION_ZERODIVIDE:: +infinity or -infinity
457
+ * EXCEPTION_OVERFLOW:: +Infinity or -Infinity
458
+ * EXCEPTION_ZERODIVIDE:: +Infinity or -Infinity
456
459
  *
457
460
  * One value of the mode parameter controls the rounding of numeric values:
458
461
  * BigDecimal::ROUND_MODE. The values it can take are:
@@ -598,7 +601,7 @@ BigDecimal_IsNaN(VALUE self)
598
601
  }
599
602
 
600
603
  /* Returns nil, -1, or +1 depending on whether the value is finite,
601
- * -infinity, or +infinity.
604
+ * -Infinity, or +Infinity.
602
605
  */
603
606
  static VALUE
604
607
  BigDecimal_IsInfinite(VALUE self)
@@ -1590,10 +1593,10 @@ BigDecimal_sqrt(VALUE self, VALUE nFig)
1590
1593
  size_t mx, n;
1591
1594
 
1592
1595
  GUARD_OBJ(a, GetVpValue(self, 1));
1593
- mx = a->Prec *(VpBaseFig() + 1);
1596
+ mx = a->Prec * (VpBaseFig() + 1);
1594
1597
 
1595
- n = GetPositiveInt(nFig) + VpDblFig() + 1;
1596
- if(mx <= n) mx = n;
1598
+ n = GetPositiveInt(nFig) + VpDblFig() + BASE_FIG;
1599
+ if (mx <= n) mx = n;
1597
1600
  GUARD_OBJ(c, VpCreateRbObject(mx, "0"));
1598
1601
  VpSqrt(c, a);
1599
1602
  return ToValue(c);
@@ -2380,6 +2383,9 @@ retry:
2380
2383
  GUARD_OBJ(y, VpCreateRbObject(1, "0"));
2381
2384
  }
2382
2385
  VpPower(y, x, int_exp);
2386
+ if (VpIsDef(y)) {
2387
+ VpMidRound(y, VpGetRoundMode(), n);
2388
+ }
2383
2389
  return ToValue(y);
2384
2390
  }
2385
2391
 
@@ -2552,8 +2558,8 @@ BigDecimal_limit(int argc, VALUE *argv, VALUE self)
2552
2558
  * BigDecimal::SIGN_NaN:: value is Not a Number
2553
2559
  * BigDecimal::SIGN_POSITIVE_ZERO:: value is +0
2554
2560
  * BigDecimal::SIGN_NEGATIVE_ZERO:: value is -0
2555
- * BigDecimal::SIGN_POSITIVE_INFINITE:: value is +infinity
2556
- * BigDecimal::SIGN_NEGATIVE_INFINITE:: value is -infinity
2561
+ * BigDecimal::SIGN_POSITIVE_INFINITE:: value is +Infinity
2562
+ * BigDecimal::SIGN_NEGATIVE_INFINITE:: value is -Infinity
2557
2563
  * BigDecimal::SIGN_POSITIVE_FINITE:: value is positive
2558
2564
  * BigDecimal::SIGN_NEGATIVE_FINITE:: value is negative
2559
2565
  */
@@ -2673,18 +2679,18 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
2673
2679
  * BigDecimalCmp function. */
2674
2680
  switch (TYPE(x)) {
2675
2681
  case T_DATA:
2676
- if (!is_kind_of_BigDecimal(x)) break;
2677
- vx = DATA_PTR(x);
2678
- negative = VpGetSign(vx) < 0;
2679
- infinite = VpIsPosInf(vx) || VpIsNegInf(vx);
2680
- nan = VpIsNaN(vx);
2681
- break;
2682
+ if (!is_kind_of_BigDecimal(x)) break;
2683
+ vx = DATA_PTR(x);
2684
+ negative = VpGetSign(vx) < 0;
2685
+ infinite = VpIsPosInf(vx) || VpIsNegInf(vx);
2686
+ nan = VpIsNaN(vx);
2687
+ break;
2682
2688
 
2683
2689
  case T_FIXNUM:
2684
- /* fall through */
2690
+ /* fall through */
2685
2691
  case T_BIGNUM:
2686
- vx = GetVpValue(x, 0);
2687
- break;
2692
+ vx = GetVpValue(x, 0);
2693
+ break;
2688
2694
 
2689
2695
  case T_FLOAT:
2690
2696
  flo = RFLOAT_VALUE(x);
@@ -2710,22 +2716,22 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
2710
2716
  else {
2711
2717
  Real* vy;
2712
2718
  vy = VpCreateRbObject(prec, "#0");
2713
- RB_GC_GUARD(vy->obj);
2714
2719
  VpSetInf(vy, VP_SIGN_POSITIVE_INFINITE);
2720
+ RB_GC_GUARD(vy->obj);
2715
2721
  return ToValue(vy);
2716
2722
  }
2717
2723
  }
2718
2724
  else if (nan) {
2719
2725
  Real* vy;
2720
2726
  vy = VpCreateRbObject(prec, "#0");
2721
- RB_GC_GUARD(vy->obj);
2722
2727
  VpSetNaN(vy);
2728
+ RB_GC_GUARD(vy->obj);
2723
2729
  return ToValue(vy);
2724
2730
  }
2725
2731
  else if (vx == NULL) {
2726
2732
  cannot_be_coerced_into_BigDecimal(rb_eArgError, x);
2727
2733
  }
2728
- x = RB_GC_GUARD(vx->obj);
2734
+ x = vx->obj;
2729
2735
 
2730
2736
  n = prec + rmpd_double_figures();
2731
2737
  negative = VpGetSign(vx) < 0;
@@ -2733,18 +2739,21 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
2733
2739
  VpSetSign(vx, 1);
2734
2740
  }
2735
2741
 
2736
- RB_GC_GUARD(one) = ToValue(VpCreateRbObject(1, "1"));
2737
- RB_GC_GUARD(x1) = one;
2738
- RB_GC_GUARD(y) = one;
2739
- RB_GC_GUARD(d) = y;
2740
- RB_GC_GUARD(z) = one;
2741
- i = 0;
2742
+ one = ToValue(VpCreateRbObject(1, "1"));
2743
+ x1 = one;
2744
+ y = one;
2745
+ d = y;
2746
+ z = one;
2747
+ i = 0;
2742
2748
 
2743
2749
  while (!VpIsZero((Real*)DATA_PTR(d))) {
2744
2750
  VALUE argv[2];
2745
2751
  SIGNED_VALUE const ey = VpExponent10(DATA_PTR(y));
2746
2752
  SIGNED_VALUE const ed = VpExponent10(DATA_PTR(d));
2747
2753
  ssize_t m = n - vabs(ey - ed);
2754
+
2755
+ rb_thread_check_ints();
2756
+
2748
2757
  if (m <= 0) {
2749
2758
  break;
2750
2759
  }
@@ -2771,6 +2780,13 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
2771
2780
  vprec = SSIZET2NUM(prec - VpExponent10(DATA_PTR(y)));
2772
2781
  return BigDecimal_round(1, &vprec, y);
2773
2782
  }
2783
+
2784
+ RB_GC_GUARD(one);
2785
+ RB_GC_GUARD(x);
2786
+ RB_GC_GUARD(x1);
2787
+ RB_GC_GUARD(y);
2788
+ RB_GC_GUARD(d);
2789
+ RB_GC_GUARD(z);
2774
2790
  }
2775
2791
 
2776
2792
  /* call-seq:
@@ -2957,7 +2973,7 @@ get_vp_value:
2957
2973
  * For example, try:
2958
2974
  *
2959
2975
  * sum = 0
2960
- * for i in (1..10000)
2976
+ * 10_000.times do
2961
2977
  * sum = sum + 0.0001
2962
2978
  * end
2963
2979
  * print sum #=> 0.9999999999999062
@@ -2967,7 +2983,7 @@ get_vp_value:
2967
2983
  * require 'bigdecimal'
2968
2984
  *
2969
2985
  * sum = BigDecimal.new("0")
2970
- * for i in (1..10000)
2986
+ * 10_000.times do
2971
2987
  * sum = sum + BigDecimal.new("0.0001")
2972
2988
  * end
2973
2989
  * print sum #=> 0.1E1
@@ -2988,8 +3004,8 @@ get_vp_value:
2988
3004
  * BigDecimal sometimes needs to return infinity, for example if you divide
2989
3005
  * a value by zero.
2990
3006
  *
2991
- * BigDecimal.new("1.0") / BigDecimal.new("0.0") #=> infinity
2992
- * BigDecimal.new("-1.0") / BigDecimal.new("0.0") #=> -infinity
3007
+ * BigDecimal.new("1.0") / BigDecimal.new("0.0") #=> Infinity
3008
+ * BigDecimal.new("-1.0") / BigDecimal.new("0.0") #=> -Infinity
2993
3009
  *
2994
3010
  * You can represent infinite numbers to BigDecimal using the strings
2995
3011
  * <code>'Infinity'</code>, <code>'+Infinity'</code> and
@@ -3009,8 +3025,8 @@ get_vp_value:
3009
3025
  * NaN is never considered to be the same as any other value, even NaN itself:
3010
3026
  *
3011
3027
  * n = BigDecimal.new('NaN')
3012
- * n == 0.0 #=> nil
3013
- * n == n #=> nil
3028
+ * n == 0.0 #=> false
3029
+ * n == n #=> false
3014
3030
  *
3015
3031
  * === Positive and negative zero
3016
3032
  *
@@ -3152,10 +3168,10 @@ Init_bigdecimal(void)
3152
3168
  * See BigDecimal.mode.
3153
3169
  */
3154
3170
  rb_define_const(rb_cBigDecimal, "ROUND_HALF_DOWN", INT2FIX(VP_ROUND_HALF_DOWN));
3155
- /* 5: Round towards +infinity. See BigDecimal.mode. */
3171
+ /* 5: Round towards +Infinity. See BigDecimal.mode. */
3156
3172
  rb_define_const(rb_cBigDecimal, "ROUND_CEILING", INT2FIX(VP_ROUND_CEIL));
3157
3173
 
3158
- /* 6: Round towards -infinity. See BigDecimal.mode. */
3174
+ /* 6: Round towards -Infinity. See BigDecimal.mode. */
3159
3175
  rb_define_const(rb_cBigDecimal, "ROUND_FLOOR", INT2FIX(VP_ROUND_FLOOR));
3160
3176
 
3161
3177
  /* 7: Round towards the even neighbor. See BigDecimal.mode. */
@@ -3815,7 +3831,9 @@ VpAlloc(size_t mx, const char *szVal)
3815
3831
  size_t mf = VpGetPrecLimit();
3816
3832
  VALUE buf;
3817
3833
 
3818
- mx = (mx + BASE_FIG - 1) / BASE_FIG + 1; /* Determine allocation unit. */
3834
+ mx = (mx + BASE_FIG - 1) / BASE_FIG; /* Determine allocation unit. */
3835
+ if (mx == 0) ++mx;
3836
+
3819
3837
  if (szVal) {
3820
3838
  while (ISSPACE(*szVal)) szVal++;
3821
3839
  if (*szVal != '#') {
@@ -3936,7 +3954,7 @@ VpAlloc(size_t mx, const char *szVal)
3936
3954
  }
3937
3955
  nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */
3938
3956
  /* units for szVal[] */
3939
- if (mx <= 0) mx = 1;
3957
+ if (mx == 0) mx = 1;
3940
3958
  nalloc = Max(nalloc, mx);
3941
3959
  mx = nalloc;
3942
3960
  vp = VpAllocReal(mx);
@@ -5029,7 +5047,7 @@ VpFormatSt(char *psz, size_t fFmt)
5029
5047
  size_t ie, i, nf = 0;
5030
5048
  char ch;
5031
5049
 
5032
- if (fFmt <= 0) return;
5050
+ if (fFmt == 0) return;
5033
5051
 
5034
5052
  ie = strlen(psz);
5035
5053
  for (i = 0; i < ie; ++i) {
@@ -5653,6 +5671,7 @@ VpSqrt(Real *y, Real *x)
5653
5671
 
5654
5672
  n = (SIGNED_VALUE)y->MaxPrec;
5655
5673
  if (x->MaxPrec > (size_t)n) n = (ssize_t)x->MaxPrec;
5674
+
5656
5675
  /* allocate temporally variables */
5657
5676
  f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1");
5658
5677
  r = VpAlloc((n + n) * (BASE_FIG + 2), "#1");
@@ -5690,8 +5709,7 @@ VpSqrt(Real *y, Real *x)
5690
5709
  if (VpIsZero(f)) goto converge;
5691
5710
  VpAddSub(r, f, y, 1); /* r = y + f */
5692
5711
  VpAsgn(y, r, 1); /* y = r */
5693
- if (f->exponent <= prec) goto converge;
5694
- } while(++nr < n);
5712
+ } while (++nr < n);
5695
5713
 
5696
5714
  #ifdef BIGDECIMAL_DEBUG
5697
5715
  if (gfDebug) {
@@ -6162,12 +6180,12 @@ VpVarCheck(Real * v)
6162
6180
  {
6163
6181
  size_t i;
6164
6182
 
6165
- if (v->MaxPrec <= 0) {
6183
+ if (v->MaxPrec == 0) {
6166
6184
  printf("ERROR(VpVarCheck): Illegal Max. Precision(=%"PRIuSIZE")\n",
6167
6185
  v->MaxPrec);
6168
6186
  return 1;
6169
6187
  }
6170
- if (v->Prec <= 0 || v->Prec > v->MaxPrec) {
6188
+ if (v->Prec == 0 || v->Prec > v->MaxPrec) {
6171
6189
  printf("ERROR(VpVarCheck): Illegal Precision(=%"PRIuSIZE")\n", v->Prec);
6172
6190
  printf(" Max. Prec.=%"PRIuSIZE"\n", v->MaxPrec);
6173
6191
  return 2;
data/bigdecimal.gemspec CHANGED
@@ -1,5 +1,5 @@
1
1
  # -*- ruby -*-
2
- _VERSION = "1.2.1"
2
+ _VERSION = "1.2.2"
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "bigdecimal"
@@ -1,3 +1,7 @@
1
+ # BigDecimal extends the native Integer class to provide the #to_d method.
2
+ #
3
+ # When you require the BigDecimal library in your application, this methodwill
4
+ # be available on Integer objects.
1
5
  class Integer < Numeric
2
6
  # call-seq:
3
7
  # int.to_d -> bigdecimal
@@ -15,6 +19,10 @@ class Integer < Numeric
15
19
  end
16
20
  end
17
21
 
22
+ # BigDecimal extends the native Float class to provide the #to_d method.
23
+ #
24
+ # When you require BigDecimal in your application, this method will be
25
+ # available on Float objects.
18
26
  class Float < Numeric
19
27
  # call-seq:
20
28
  # flt.to_d -> bigdecimal
@@ -32,6 +40,10 @@ class Float < Numeric
32
40
  end
33
41
  end
34
42
 
43
+ # BigDecimal extends the native String class to provide the #to_d method.
44
+ #
45
+ # When you require BigDecimal in your application, this method will be
46
+ # available on String objects.
35
47
  class String
36
48
  # call-seq:
37
49
  # string.to_d -> bigdecimal
@@ -49,6 +61,11 @@ class String
49
61
  end
50
62
  end
51
63
 
64
+ # BigDecimal extends the native Numeric class to provide the #to_digits and
65
+ # #to_d methods.
66
+ #
67
+ # When you require BigDecimal in your application, this method will be
68
+ # available on BigDecimal objects.
52
69
  class BigDecimal < Numeric
53
70
  # call-seq:
54
71
  # a.to_digits -> string
@@ -81,18 +98,23 @@ class BigDecimal < Numeric
81
98
  end
82
99
  end
83
100
 
101
+ # BigDecimal extends the native Rational class to provide the #to_d method.
102
+ #
103
+ # When you require BigDecimal in your application, this method will be
104
+ # available on Rational objects.
84
105
  class Rational < Numeric
85
106
  # call-seq:
86
- # r.to_d -> bigdecimal
87
- # r.to_d(sig) -> bigdecimal
107
+ # r.to_d(precision) -> bigdecimal
88
108
  #
89
- # Converts a Rational to a BigDecimal. Takes an optional parameter +sig+ to
90
- # limit the amount of significant digits.
109
+ # Converts a Rational to a BigDecimal.
110
+ #
111
+ # The required +precision+ parameter is used to determine the amount of
112
+ # significant digits for the result. See BigDecimal#div for more information,
113
+ # as it is used along with the #denominator and the +precision+ for
114
+ # parameters.
91
115
  #
92
116
  # r = (22/7.0).to_r
93
117
  # # => (7077085128725065/2251799813685248)
94
- # r.to_d
95
- # # => #<BigDecimal:1a52bd8,'0.3142857142 8571427937 0154144999 105E1',45(63)>
96
118
  # r.to_d(3)
97
119
  # # => #<BigDecimal:1a44d08,'0.314E1',18(36)>
98
120
  def to_d(precision)
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.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenta Murata
@@ -53,8 +53,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  requirements: []
55
55
  rubyforge_project:
56
- rubygems_version: 2.0.2
56
+ rubygems_version: 2.0.3
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: