bigdecimal 1.4.4 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87db143e2139576067ccfd911dde4e081aad19bc0faa08ca6b74a34c430e82de
4
- data.tar.gz: 54bde88f847fae9e241ec3817e96f45d21a8831b5827f76a0b25915b576d0900
3
+ metadata.gz: 4fe17cbc3e4c4013708f79a93c975f84b3b55c53028671ef217dde778438a909
4
+ data.tar.gz: 5ec9c5839c1ff24a1d08bca8087fff348ec5eaf5aeac910ebb4c972dc00b7b20
5
5
  SHA512:
6
- metadata.gz: 360aa77ae2daadf7ff173669e115b76038b17f233694b316b17a159bec27be4eb1c57adee88350e3e3bde27097257b6d3526e057a28f5a4e457864cceb3aec5a
7
- data.tar.gz: 4cbb886e60336b3741f5f1cef0a29877384f1e51b49aee13a18d1afd4f4cea3b86a826cb1ee2923f23684a148334c9ec0a09bef620b29eb62283baa299b45366
6
+ metadata.gz: 653b43d52285c5b25dbef5a4ced242d289b966699e099ae37f122a120e924cabd484a534df3d62c1c2e03ae76fefdb874564631bcdd617f041b664b42c34d981
7
+ data.tar.gz: 0cc5144ca6df649f27338327021bc7644cd55f7834ff035e473b9abb68d821ea28f9a89f3e6210224547cce0be0c0a56bc9c72d89e63b8446407f2f3ef4ff602
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- bigdecimal_version = '1.4.4'
3
+ bigdecimal_version = '2.0.0'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "bigdecimal"
@@ -30,11 +30,10 @@ Gem::Specification.new do |s|
30
30
  sample/pi.rb
31
31
  ]
32
32
 
33
- s.required_ruby_version = Gem::Requirement.new(">= 2.3.0".freeze)
33
+ s.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
34
34
 
35
35
  s.add_development_dependency "rake", "~> 10.0"
36
36
  s.add_development_dependency "rake-compiler", ">= 0.9"
37
- s.add_development_dependency "rake-compiler-dock", ">= 0.6.1"
38
37
  s.add_development_dependency "minitest", "< 5.0.0"
39
38
  s.add_development_dependency "pry"
40
39
  end
@@ -127,6 +127,30 @@ rb_rational_den(VALUE rat)
127
127
  }
128
128
  #endif
129
129
 
130
+ #ifndef HAVE_RB_COMPLEX_REAL
131
+ static inline VALUE
132
+ rb_complex_real(VALUE cmp)
133
+ {
134
+ #ifdef HAVE_TYPE_STRUCT_RCOMPLEX
135
+ return RCOMPLEX(cmp)->real;
136
+ #else
137
+ return rb_funcall(cmp, rb_intern("real"), 0);
138
+ #endif
139
+ }
140
+ #endif
141
+
142
+ #ifndef HAVE_RB_COMPLEX_IMAG
143
+ static inline VALUE
144
+ rb_complex_imag(VALUE cmp)
145
+ {
146
+ #ifdef HAVE_TYPE_STRUCT_RCOMPLEX
147
+ return RCOMPLEX(cmp)->imag;
148
+ #else
149
+ return rb_funcall(cmp, rb_intern("imag"), 0);
150
+ #endif
151
+ }
152
+ #endif
153
+
130
154
  #define BIGDECIMAL_POSITIVE_P(bd) ((bd)->sign > 0)
131
155
  #define BIGDECIMAL_NEGATIVE_P(bd) ((bd)->sign < 0)
132
156
 
@@ -276,7 +300,6 @@ again:
276
300
  #ifdef ENABLE_NUMERIC_STRING
277
301
  case T_STRING:
278
302
  StringValueCStr(v);
279
- rb_check_safe_obj(v);
280
303
  return VpCreateRbObject(RSTRING_LEN(v) + VpBaseFig() + 1,
281
304
  RSTRING_PTR(v));
282
305
  #endif /* ENABLE_NUMERIC_STRING */
@@ -418,7 +441,6 @@ BigDecimal_load(VALUE self, VALUE str)
418
441
  unsigned long m=0;
419
442
 
420
443
  pch = (unsigned char *)StringValueCStr(str);
421
- rb_check_safe_obj(str);
422
444
  /* First get max prec */
423
445
  while((*pch) != (unsigned char)'\0' && (ch = *pch++) != (unsigned char)':') {
424
446
  if(!ISDIGIT(ch)) {
@@ -1756,12 +1778,15 @@ BigDecimal_fix(VALUE self)
1756
1778
  * round(n, mode)
1757
1779
  *
1758
1780
  * Round to the nearest integer (by default), returning the result as a
1759
- * BigDecimal.
1781
+ * BigDecimal if n is specified, or as an Integer if it isn't.
1760
1782
  *
1761
1783
  * BigDecimal('3.14159').round #=> 3
1762
1784
  * BigDecimal('8.7').round #=> 9
1763
1785
  * BigDecimal('-9.9').round #=> -10
1764
1786
  *
1787
+ * BigDecimal('3.14159').round(2).class.name #=> "BigDecimal"
1788
+ * BigDecimal('3.14159').round.class.name #=> "Integer"
1789
+ *
1765
1790
  * If n is specified and positive, the fractional part of the result has no
1766
1791
  * more than that many digits.
1767
1792
  *
@@ -2027,7 +2052,6 @@ BigDecimal_to_s(int argc, VALUE *argv, VALUE self)
2027
2052
  if (rb_scan_args(argc, argv, "01", &f) == 1) {
2028
2053
  if (RB_TYPE_P(f, T_STRING)) {
2029
2054
  psz = StringValueCStr(f);
2030
- rb_check_safe_obj(f);
2031
2055
  if (*psz == ' ') {
2032
2056
  fPlus = 1;
2033
2057
  psz++;
@@ -2560,6 +2584,10 @@ BigDecimal_clone(VALUE self)
2560
2584
  return self;
2561
2585
  }
2562
2586
 
2587
+ #ifdef HAVE_RB_OPTS_EXCEPTION_P
2588
+ int rb_opts_exception_p(VALUE opts, int default_value);
2589
+ #define opts_exception_p(opts) rb_opts_exception_p((opts), 1)
2590
+ #else
2563
2591
  static int
2564
2592
  opts_exception_p(VALUE opts)
2565
2593
  {
@@ -2568,12 +2596,20 @@ opts_exception_p(VALUE opts)
2568
2596
  if (!kwds[0]) {
2569
2597
  kwds[0] = rb_intern_const("exception");
2570
2598
  }
2571
- rb_get_kwargs(opts, kwds, 0, 1, &exception);
2599
+ if (!rb_get_kwargs(opts, kwds, 0, 1, &exception)) return 1;
2600
+ switch (exception) {
2601
+ case Qtrue: case Qfalse:
2602
+ break;
2603
+ default:
2604
+ rb_raise(rb_eArgError, "true or false is expected as exception: %+"PRIsVALUE,
2605
+ exception);
2606
+ }
2572
2607
  return exception != Qfalse;
2573
2608
  }
2609
+ #endif
2574
2610
 
2575
2611
  static Real *
2576
- VpNewVarArgs(int argc, VALUE *argv)
2612
+ VpNewVarArg(int argc, VALUE *argv)
2577
2613
  {
2578
2614
  size_t mf;
2579
2615
  VALUE opts = Qnil;
@@ -2616,6 +2652,7 @@ VpNewVarArgs(int argc, VALUE *argv)
2616
2652
  }
2617
2653
  }
2618
2654
 
2655
+ retry:
2619
2656
  switch (TYPE(iniValue)) {
2620
2657
  case T_DATA:
2621
2658
  if (is_kind_of_BigDecimal(iniValue)) {
@@ -2653,6 +2690,18 @@ VpNewVarArgs(int argc, VALUE *argv)
2653
2690
  }
2654
2691
  return GetVpValueWithPrec(iniValue, mf, 1);
2655
2692
 
2693
+ case T_COMPLEX:
2694
+ {
2695
+ VALUE im;
2696
+ im = rb_complex_imag(iniValue);
2697
+ if (!is_zero(im)) {
2698
+ rb_raise(rb_eArgError,
2699
+ "Unable to make a BigDecimal from non-zero imaginary number");
2700
+ }
2701
+ iniValue = rb_complex_real(iniValue);
2702
+ goto retry;
2703
+ }
2704
+
2656
2705
  case T_STRING:
2657
2706
  /* fall through */
2658
2707
  default:
@@ -2667,23 +2716,6 @@ VpNewVarArgs(int argc, VALUE *argv)
2667
2716
  return VpAlloc(mf, RSTRING_PTR(iniValue), 1, exc);
2668
2717
  }
2669
2718
 
2670
- static VALUE
2671
- BigDecimal_new(int argc, VALUE *argv, VALUE klass)
2672
- {
2673
- ENTER(1);
2674
- Real *pv;
2675
- VALUE obj;
2676
-
2677
- obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, 0);
2678
- pv = VpNewVarArgs(argc, argv);
2679
- if (pv == NULL) return Qnil;
2680
- SAVE(pv);
2681
- if (ToValue(pv)) pv = VpCopy(NULL, pv);
2682
- RTYPEDDATA_DATA(obj) = pv;
2683
- RB_OBJ_FREEZE(obj);
2684
- return pv->obj = obj;
2685
- }
2686
-
2687
2719
  /* call-seq:
2688
2720
  * BigDecimal(initial, digits, exception: true)
2689
2721
  *
@@ -2723,28 +2755,35 @@ BigDecimal_new(int argc, VALUE *argv, VALUE klass)
2723
2755
  static VALUE
2724
2756
  f_BigDecimal(int argc, VALUE *argv, VALUE self)
2725
2757
  {
2726
- return BigDecimal_new(argc, argv, rb_cBigDecimal);
2758
+ ENTER(1);
2759
+ Real *pv;
2760
+ VALUE obj;
2761
+
2762
+ if (argc > 0 && CLASS_OF(argv[0]) == rb_cBigDecimal) {
2763
+ if (argc == 1 || (argc == 2 && RB_TYPE_P(argv[1], T_HASH))) return argv[0];
2764
+ }
2765
+ obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, 0);
2766
+ pv = VpNewVarArg(argc, argv);
2767
+ if (pv == NULL) return Qnil;
2768
+ SAVE(pv);
2769
+ if (ToValue(pv)) pv = VpCopy(NULL, pv);
2770
+ RTYPEDDATA_DATA(obj) = pv;
2771
+ RB_OBJ_FREEZE(obj);
2772
+ return pv->obj = obj;
2727
2773
  }
2728
2774
 
2729
2775
  static VALUE
2730
2776
  BigDecimal_s_interpret_loosely(VALUE klass, VALUE str)
2731
2777
  {
2732
- ENTER(1);
2733
- char const *c_str;
2734
- Real *pv;
2735
-
2736
- c_str = StringValueCStr(str);
2737
- GUARD_OBJ(pv, VpAlloc(0, c_str, 0, 1));
2738
- pv->obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, pv);
2739
- RB_OBJ_FREEZE(pv->obj);
2740
- return pv->obj;
2741
- }
2778
+ ENTER(1);
2779
+ char const *c_str;
2780
+ Real *pv;
2742
2781
 
2743
- /* DEPRECATED: BigDecimal.new() */
2744
- static VALUE
2745
- BigDecimal_s_new(int argc, VALUE *argv, VALUE klass)
2746
- {
2747
- return BigDecimal_new(argc, argv, klass);
2782
+ c_str = StringValueCStr(str);
2783
+ GUARD_OBJ(pv, VpAlloc(0, c_str, 0, 1));
2784
+ pv->obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, pv);
2785
+ RB_OBJ_FREEZE(pv->obj);
2786
+ return pv->obj;
2748
2787
  }
2749
2788
 
2750
2789
  /* call-seq:
@@ -2966,6 +3005,10 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
2966
3005
  n = prec + rmpd_double_figures();
2967
3006
  negative = BIGDECIMAL_NEGATIVE_P(vx);
2968
3007
  if (negative) {
3008
+ VALUE x_zero = INT2NUM(1);
3009
+ VALUE x_copy = f_BigDecimal(1, &x_zero, klass);
3010
+ x = BigDecimal_initialize_copy(x_copy, x);
3011
+ vx = DATA_PTR(x);
2969
3012
  VpSetSign(vx, 1);
2970
3013
  }
2971
3014
 
@@ -3312,8 +3355,8 @@ Init_bigdecimal(void)
3312
3355
 
3313
3356
  /* Class methods */
3314
3357
  rb_undef_method(CLASS_OF(rb_cBigDecimal), "allocate");
3358
+ rb_undef_method(CLASS_OF(rb_cBigDecimal), "new");
3315
3359
  rb_define_singleton_method(rb_cBigDecimal, "interpret_loosely", BigDecimal_s_interpret_loosely, 1);
3316
- rb_define_singleton_method(rb_cBigDecimal, "new", BigDecimal_s_new, -1);
3317
3360
  rb_define_singleton_method(rb_cBigDecimal, "mode", BigDecimal_mode, -1);
3318
3361
  rb_define_singleton_method(rb_cBigDecimal, "limit", BigDecimal_limit, -1);
3319
3362
  rb_define_singleton_method(rb_cBigDecimal, "double_fig", BigDecimal_double_fig, 0);
@@ -3444,7 +3487,6 @@ Init_bigdecimal(void)
3444
3487
 
3445
3488
 
3446
3489
  /* instance methods */
3447
- rb_define_method(rb_cBigDecimal, "initialize_copy", BigDecimal_initialize_copy, 1);
3448
3490
  rb_define_method(rb_cBigDecimal, "precs", BigDecimal_prec, 0);
3449
3491
 
3450
3492
  rb_define_method(rb_cBigDecimal, "add", BigDecimal_add2, 2);
@@ -36,8 +36,12 @@ have_func("isfinite", "math.h")
36
36
  have_type("struct RRational", "ruby.h")
37
37
  have_func("rb_rational_num", "ruby.h")
38
38
  have_func("rb_rational_den", "ruby.h")
39
+ have_type("struct RComplex", "ruby.h")
40
+ have_func("rb_complex_real", "ruby.h")
41
+ have_func("rb_complex_imag", "ruby.h")
39
42
  have_func("rb_array_const_ptr", "ruby.h")
40
43
  have_func("rb_sym2str", "ruby.h")
44
+ have_func("rb_opts_exception_p", "ruby.h")
41
45
 
42
46
  if File.file?(File.expand_path('../lib/bigdecimal.rb', __FILE__))
43
47
  bigdecimal_rb = "$(srcdir)/lib/bigdecimal.rb"
@@ -1,22 +1 @@
1
- begin
2
- require "#{RUBY_VERSION[/\d+\.\d+/]}/bigdecimal.so"
3
- rescue LoadError
4
- require 'bigdecimal.so'
5
- end
6
-
7
- class BigDecimal
8
- module Deprecation
9
- def new(*args, **kwargs)
10
- warn "BigDecimal.new is deprecated; use BigDecimal() method instead.", uplevel: 1
11
- super
12
- end
13
- end
14
-
15
- class << self
16
- prepend Deprecation
17
-
18
- def inherited(subclass)
19
- warn "subclassing BigDecimal will be disallowed after bigdecimal version 2.0", uplevel: 1
20
- end
21
- end
22
- end
1
+ require 'bigdecimal.so'
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: false
2
- #
2
+
3
+ require 'bigdecimal'
4
+
3
5
  # require 'bigdecimal/jacobian'
4
6
  #
5
7
  # Provides methods to compute the Jacobian matrix of a set of equations at a
@@ -21,9 +23,6 @@
21
23
  #
22
24
  # fx is f.values(x).
23
25
  #
24
-
25
- require 'bigdecimal'
26
-
27
26
  module Jacobian
28
27
  module_function
29
28
 
@@ -131,6 +131,39 @@ class Rational < Numeric
131
131
  end
132
132
 
133
133
 
134
+ class Complex < Numeric
135
+ # call-seq:
136
+ # cmp.to_d -> bigdecimal
137
+ # cmp.to_d(precision) -> bigdecimal
138
+ #
139
+ # Returns the value as a BigDecimal.
140
+ #
141
+ # The +precision+ parameter is required for a rational complex number.
142
+ # This parameter is used to determine the number of significant digits
143
+ # for the result.
144
+ #
145
+ # require 'bigdecimal'
146
+ # require 'bigdecimal/util'
147
+ #
148
+ # Complex(0.1234567, 0).to_d(4) # => 0.1235e0
149
+ # Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
150
+ #
151
+ # See also BigDecimal::new.
152
+ #
153
+ def to_d(*args)
154
+ BigDecimal(self) unless self.imag.zero? # to raise eerror
155
+
156
+ if args.length == 0
157
+ case self.real
158
+ when Rational
159
+ BigDecimal(self.real) # to raise error
160
+ end
161
+ end
162
+ self.real.to_d(*args)
163
+ end
164
+ end
165
+
166
+
134
167
  class NilClass
135
168
  # call-seq:
136
169
  # nil.to_d -> bigdecimal
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.4.4
4
+ version: 2.0.0
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: 2019-05-13 00:00:00.000000000 Z
13
+ date: 2019-12-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -40,20 +40,6 @@ dependencies:
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0.9'
43
- - !ruby/object:Gem::Dependency
44
- name: rake-compiler-dock
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: 0.6.1
50
- type: :development
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: 0.6.1
57
43
  - !ruby/object:Gem::Dependency
58
44
  name: minitest
59
45
  requirement: !ruby/object:Gem::Requirement
@@ -116,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
102
  requirements:
117
103
  - - ">="
118
104
  - !ruby/object:Gem::Version
119
- version: 2.3.0
105
+ version: 2.4.0
120
106
  required_rubygems_version: !ruby/object:Gem::Requirement
121
107
  requirements:
122
108
  - - ">="