bigdecimal 1.4.0.pre.20181130a → 1.4.0.pre.20181204a

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5feef7369a59fe24f0c6c235b860ae376fc818ce75017edd4e072b65f66fa0f6
4
- data.tar.gz: bc46a83aeef02e6acc38827b1d62175fb3c16e7f4f2306f5b1ae529b1026af44
3
+ metadata.gz: cd175d854d1fbce48f6f894247f0e93b8c47f8ab44311d3802dfd6251736aeee
4
+ data.tar.gz: 6c0f0986a74dc44bc89e02f18349fab3042e9459bff242f9437a00e10f0d34b2
5
5
  SHA512:
6
- metadata.gz: 395bcbed4906f0928b70861586d1e577caa1938ae599700a6f2b60011124e899cc6ac0dd50ea297c3f1a486fccca6a5c17835a72f1084fc4aa900ab2b5c00497
7
- data.tar.gz: 888f8a07210e091fd51a8b5c005e99e739f1dbdc08067f7874244f24ee7524cc3824df9e083d0fd40159c09210cb307f0dbbcee974f2d0eb1376531635213ebd
6
+ metadata.gz: d4ba585b71f0a72653a115e544febe08f0db4b7aa56c8d74bfa265b573ddefafd8fea4cc1968df3154b573dca2906594a6c83ed1b0482b0cd73c3b1cd9d1972a
7
+ data.tar.gz: afeb4fc25449a1ee18500a095be5ffcbffe6bc450fd42da510f49f79cea840da4f7a2add6297896f57c703c30fbb5d634ad6b96b9bca8702e10ef93aaff8dcc8
data/bigdecimal.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- bigdecimal_version = '1.4.0.pre.20181130a'
3
+ bigdecimal_version = '1.4.0.pre.20181204a'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "bigdecimal"
@@ -646,7 +646,7 @@ VP_EXPORT Real *
646
646
  VpNewRbClass(size_t mx, const char *str, VALUE klass)
647
647
  {
648
648
  VALUE obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, 0);
649
- Real *pv = VpAlloc(mx, str, 1);
649
+ Real *pv = VpAlloc(mx, str, 1, 1);
650
650
  RTYPEDDATA_DATA(obj) = pv;
651
651
  pv->obj = obj;
652
652
  RB_OBJ_FREEZE(obj);
@@ -2538,28 +2538,6 @@ BigDecimal_power_op(VALUE self, VALUE exp)
2538
2538
  return BigDecimal_power(1, &exp, self);
2539
2539
  }
2540
2540
 
2541
- static Real *BigDecimal_new(int argc, VALUE *argv);
2542
-
2543
- static VALUE
2544
- BigDecimal_initialize(int argc, VALUE *argv, VALUE self)
2545
- {
2546
- ENTER(1);
2547
- Real *pv = rb_check_typeddata(self, &BigDecimal_data_type);
2548
- Real *x;
2549
-
2550
- GUARD_OBJ(x, BigDecimal_new(argc, argv));
2551
- if (ToValue(x)) {
2552
- pv = VpCopy(pv, x);
2553
- }
2554
- else {
2555
- VpFree(pv);
2556
- pv = x;
2557
- }
2558
- DATA_PTR(self) = pv;
2559
- pv->obj = self;
2560
- return self;
2561
- }
2562
-
2563
2541
  /* :nodoc:
2564
2542
  *
2565
2543
  * private method for dup and clone the provided BigDecimal +other+
@@ -2582,19 +2560,60 @@ BigDecimal_clone(VALUE self)
2582
2560
  return self;
2583
2561
  }
2584
2562
 
2563
+ static int
2564
+ opts_exception_p(VALUE opts)
2565
+ {
2566
+ static ID kwds[1];
2567
+ VALUE exception;
2568
+ if (!kwds[0]) {
2569
+ kwds[0] = rb_intern_const("exception");
2570
+ }
2571
+ rb_get_kwargs(opts, kwds, 0, 1, &exception);
2572
+ return exception != Qfalse;
2573
+ }
2574
+
2585
2575
  static Real *
2586
2576
  BigDecimal_new(int argc, VALUE *argv)
2587
2577
  {
2588
2578
  size_t mf;
2579
+ VALUE opts = Qnil;
2589
2580
  VALUE nFig;
2590
2581
  VALUE iniValue;
2591
2582
  double d;
2583
+ int exc;
2592
2584
 
2593
- if (rb_scan_args(argc, argv, "11", &iniValue, &nFig) == 1) {
2585
+ argc = rb_scan_args(argc, argv, "11:", &iniValue, &nFig, &opts);
2586
+ exc = opts_exception_p(opts);
2587
+
2588
+ if (argc == 1) {
2594
2589
  mf = 0;
2595
2590
  }
2596
2591
  else {
2597
- mf = GetPrecisionInt(nFig);
2592
+ /* expand GetPrecisionInt for exception suppression */
2593
+ ssize_t n = NUM2INT(nFig);
2594
+ if (n < 0) {
2595
+ if (!exc) {
2596
+ return NULL;
2597
+ }
2598
+ rb_raise(rb_eArgError, "negative precision");
2599
+ }
2600
+ mf = (size_t)n;
2601
+ }
2602
+
2603
+ if (SPECIAL_CONST_P(iniValue)) {
2604
+ switch (iniValue) {
2605
+ case Qnil:
2606
+ if (!exc) return NULL;
2607
+ rb_raise(rb_eTypeError, "can't convert nil into BigDecimal");
2608
+ case Qtrue:
2609
+ if (!exc) return NULL;
2610
+ rb_raise(rb_eTypeError, "can't convert true into BigDecimal");
2611
+ case Qfalse:
2612
+ if (!exc) return NULL;
2613
+ rb_raise(rb_eTypeError, "can't convert false into BigDecimal");
2614
+ default:
2615
+ break;
2616
+ }
2598
2617
  }
2599
2618
 
2600
2619
  switch (TYPE(iniValue)) {
@@ -2617,11 +2636,17 @@ BigDecimal_new(int argc, VALUE *argv)
2617
2636
  return pv;
2618
2637
  }
2619
2638
  if (mf > DBL_DIG+1) {
2639
+ if (!exc) {
2640
+ return NULL;
2641
+ }
2620
2642
  rb_raise(rb_eArgError, "precision too large.");
2621
2643
  }
2622
2644
  /* fall through */
2623
2645
  case T_RATIONAL:
2624
2646
  if (NIL_P(nFig)) {
2647
+ if (!exc) {
2648
+ return NULL;
2649
+ }
2625
2650
  rb_raise(rb_eArgError,
2626
2651
  "can't omit precision for a %"PRIsVALUE".",
2627
2652
  RB_OBJ_CLASSNAME(iniValue));
@@ -2633,8 +2658,13 @@ BigDecimal_new(int argc, VALUE *argv)
2633
2658
  default:
2634
2659
  break;
2635
2660
  }
2661
+ /* TODO: support to_d */
2662
+ if (!exc) {
2663
+ iniValue = rb_check_convert_type(iniValue, T_STRING, "String", "to_str");
2664
+ if (NIL_P(iniValue)) return NULL;
2665
+ }
2636
2666
  StringValueCStr(iniValue);
2637
- return VpAlloc(mf, RSTRING_PTR(iniValue), 1);
2667
+ return VpAlloc(mf, RSTRING_PTR(iniValue), 1, exc);
2638
2668
  }
2639
2669
 
2640
2670
  /* call-seq:
@@ -2669,14 +2699,16 @@ BigDecimal_new(int argc, VALUE *argv)
2669
2699
  * value is omitted, this exception is raised.
2670
2700
  */
2671
2701
  static VALUE
2672
- BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
2702
+ f_BigDecimal(int argc, VALUE *argv, VALUE self)
2673
2703
  {
2674
2704
  ENTER(1);
2675
2705
  Real *pv;
2676
2706
  VALUE obj;
2677
2707
 
2678
2708
  obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, 0);
2679
- GUARD_OBJ(pv, BigDecimal_new(argc, argv));
2709
+ pv = BigDecimal_new(argc, argv);
2710
+ if (pv == NULL) return Qnil;
2711
+ SAVE(pv);
2680
2712
  if (ToValue(pv)) pv = VpCopy(NULL, pv);
2681
2713
  RTYPEDDATA_DATA(obj) = pv;
2682
2714
  RB_OBJ_FREEZE(obj);
@@ -3112,7 +3144,7 @@ rmpd_util_str_to_d(VALUE str)
3112
3144
  VALUE obj;
3113
3145
 
3114
3146
  c_str = StringValueCStr(str);
3115
- GUARD_OBJ(pv, VpAlloc(0, c_str, 0));
3147
+ GUARD_OBJ(pv, VpAlloc(0, c_str, 0, 1));
3116
3148
  obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, pv);
3117
3149
  RB_OBJ_FREEZE(obj);
3118
3150
  return obj;
@@ -3259,7 +3291,7 @@ Init_bigdecimal(void)
3259
3291
  rb_cBigDecimal = rb_define_class("BigDecimal", rb_cNumeric);
3260
3292
 
3261
3293
  /* Global function */
3262
- rb_define_global_function("BigDecimal", BigDecimal_global_new, -1);
3294
+ rb_define_global_function("BigDecimal", f_BigDecimal, -1);
3263
3295
 
3264
3296
  /* Class methods */
3265
3297
  rb_undef_method(CLASS_OF(rb_cBigDecimal), "allocate");
@@ -3387,14 +3419,13 @@ Init_bigdecimal(void)
3387
3419
 
3388
3420
  arg = rb_str_new2("+Infinity");
3389
3421
  /* Positive infinity value. */
3390
- rb_define_const(rb_cBigDecimal, "INFINITY", BigDecimal_global_new(1, &arg, rb_cBigDecimal));
3422
+ rb_define_const(rb_cBigDecimal, "INFINITY", f_BigDecimal(1, &arg, rb_cBigDecimal));
3391
3423
  arg = rb_str_new2("NaN");
3392
3424
  /* 'Not a Number' value. */
3393
- rb_define_const(rb_cBigDecimal, "NAN", BigDecimal_global_new(1, &arg, rb_cBigDecimal));
3425
+ rb_define_const(rb_cBigDecimal, "NAN", f_BigDecimal(1, &arg, rb_cBigDecimal));
3394
3426
 
3395
3427
 
3396
3428
  /* instance methods */
3397
- rb_define_method(rb_cBigDecimal, "initialize", BigDecimal_initialize, -1);
3398
3429
  rb_define_method(rb_cBigDecimal, "initialize_copy", BigDecimal_initialize_copy, 1);
3399
3430
  rb_define_method(rb_cBigDecimal, "precs", BigDecimal_prec, 0);
3400
3431
 
@@ -3693,13 +3724,7 @@ VpSetRoundMode(unsigned short n)
3693
3724
  * (to let the compiler know they may be changed in outside
3694
3725
  * (... but not actually..)).
3695
3726
  */
3696
- volatile const double gZero_ABCED9B1_CE73__00400511F31D = 0.0;
3697
3727
  volatile const double gOne_ABCED9B4_CE73__00400511F31D = 1.0;
3698
- static double
3699
- Zero(void)
3700
- {
3701
- return gZero_ABCED9B1_CE73__00400511F31D;
3702
- }
3703
3728
 
3704
3729
  static double
3705
3730
  One(void)
@@ -3937,8 +3962,8 @@ VpInit(BDIGIT BaseVal)
3937
3962
  VpGetDoubleNegZero();
3938
3963
 
3939
3964
  /* Allocates Vp constants. */
3940
- VpConstOne = VpAlloc(1UL, "1", 1);
3941
- VpPt5 = VpAlloc(1UL, ".5", 1);
3965
+ VpConstOne = VpAlloc(1UL, "1", 1, 1);
3966
+ VpPt5 = VpAlloc(1UL, ".5", 1, 1);
3942
3967
 
3943
3968
  #ifdef BIGDECIMAL_DEBUG
3944
3969
  gnAlloc = 0;
@@ -4062,7 +4087,7 @@ rmpd_parse_special_string(const char *str)
4062
4087
  * NULL be returned if memory allocation is failed,or any error.
4063
4088
  */
4064
4089
  VP_EXPORT Real *
4065
- VpAlloc(size_t mx, const char *szVal, int strict_p)
4090
+ VpAlloc(size_t mx, const char *szVal, int strict_p, int exc)
4066
4091
  {
4067
4092
  const char *orig_szVal = szVal;
4068
4093
  size_t i, j, ni, ipf, nf, ipe, ne, dot_seen, exp_seen, nalloc;
@@ -4261,7 +4286,9 @@ VpAlloc(size_t mx, const char *szVal, int strict_p)
4261
4286
  if (!strict_p) {
4262
4287
  goto return_zero;
4263
4288
  }
4264
-
4289
+ if (!exc) {
4290
+ return NULL;
4291
+ }
4265
4292
  str = rb_str_new2(orig_szVal);
4266
4293
  rb_raise(rb_eArgError, "invalid value for BigDecimal(): \"%"PRIsVALUE"\"", str);
4267
4294
  }
@@ -4838,7 +4865,7 @@ VpMult(Real *c, Real *a, Real *b)
4838
4865
 
4839
4866
  if (MxIndC < MxIndAB) { /* The Max. prec. of c < Prec(a)+Prec(b) */
4840
4867
  w = c;
4841
- c = VpAlloc((size_t)((MxIndAB + 1) * BASE_FIG), "#0", 1);
4868
+ c = VpAlloc((size_t)((MxIndAB + 1) * BASE_FIG), "#0", 1, 1);
4842
4869
  MxIndC = MxIndAB;
4843
4870
  }
4844
4871
 
@@ -6006,8 +6033,8 @@ VpSqrt(Real *y, Real *x)
6006
6033
  if (x->MaxPrec > (size_t)n) n = (ssize_t)x->MaxPrec;
6007
6034
 
6008
6035
  /* allocate temporally variables */
6009
- f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1", 1);
6010
- r = VpAlloc((n + n) * (BASE_FIG + 2), "#1", 1);
6036
+ f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1", 1, 1);
6037
+ r = VpAlloc((n + n) * (BASE_FIG + 2), "#1", 1, 1);
6011
6038
 
6012
6039
  nr = 0;
6013
6040
  y_prec = y->MaxPrec;
@@ -6459,8 +6486,8 @@ VpPower(Real *y, Real *x, SIGNED_VALUE n)
6459
6486
 
6460
6487
  /* Allocate working variables */
6461
6488
 
6462
- w1 = VpAlloc((y->MaxPrec + 2) * BASE_FIG, "#0", 1);
6463
- w2 = VpAlloc((w1->MaxPrec * 2 + 1) * BASE_FIG, "#0", 1);
6489
+ w1 = VpAlloc((y->MaxPrec + 2) * BASE_FIG, "#0", 1, 1);
6490
+ w2 = VpAlloc((w1->MaxPrec * 2 + 1) * BASE_FIG, "#0", 1, 1);
6464
6491
  /* calculation start */
6465
6492
 
6466
6493
  VpAsgn(y, x, 1);
@@ -308,7 +308,7 @@ VP_EXPORT size_t VpInit(BDIGIT BaseVal);
308
308
  VP_EXPORT void *VpMemAlloc(size_t mb);
309
309
  VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
310
310
  VP_EXPORT void VpFree(Real *pv);
311
- VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal, int strict_p);
311
+ VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal, int strict_p, int exc);
312
312
  VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
313
313
  VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
314
314
  VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
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.0.pre.20181130a
4
+ version: 1.4.0.pre.20181204a
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: 2018-11-30 00:00:00.000000000 Z
13
+ date: 2018-12-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake