Guest User

Untitled

a guest
Jul 19th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
  2. index 53fb678..988a2a1 100644
  3. --- a/ext/bigdecimal/bigdecimal.c
  4. +++ b/ext/bigdecimal/bigdecimal.c
  5. @@ -131,6 +131,8 @@ ToValue(Real *p)
  6. return p->obj;
  7. }
  8.  
  9. +static VALUE BigDecimal_div2(int, VALUE*, VALUE);
  10. +
  11. static Real *
  12. GetVpValue(VALUE v, int must)
  13. {
  14. @@ -145,11 +147,13 @@ again:
  15. {
  16. case T_RATIONAL:
  17. if(orig == Qundef ? (orig = v, 1) : orig != v) {
  18. - if(!util_loaded) {
  19. - rb_require("bigdecimal/util");
  20. - util_loaded = 1;
  21. - }
  22. - v = rb_funcall2(v, rb_intern("to_d"), 0, 0);
  23. + VALUE num, args[2];
  24. + num = RRATIONAL(v)->num;
  25. + args[0] = RRATIONAL(v)->den;
  26. + args[1] = INT2FIX(VpDblFig()*2 + 1);
  27. + pv = GetVpValue(num, must);
  28. + if (pv == NULL) goto SomeOneMayDoIt;
  29. + v = BigDecimal_div2(2, args, ToValue(pv));
  30. goto again;
  31. }
  32. v = orig;
  33. @@ -1738,25 +1742,6 @@ BigDecimal_power(VALUE self, VALUE p)
  34. return ToValue(y);
  35. }
  36.  
  37. -static VALUE
  38. -BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
  39. -{
  40. - ENTER(5);
  41. - Real *pv;
  42. - size_t mf;
  43. - VALUE nFig;
  44. - VALUE iniValue;
  45. -
  46. - if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
  47. - mf = 0;
  48. - } else {
  49. - mf = GetPositiveInt(nFig);
  50. - }
  51. - SafeStringValue(iniValue);
  52. - GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
  53. - return ToValue(pv);
  54. -}
  55. -
  56. /* call-seq:
  57. * new(initial, digits)
  58. *
  59. @@ -1780,14 +1765,80 @@ BigDecimal_new(int argc, VALUE *argv, VALUE self)
  60.  
  61. if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
  62. mf = 0;
  63. - } else {
  64. + }
  65. + else {
  66. mf = GetPositiveInt(nFig);
  67. }
  68. + switch (TYPE(iniValue)) {
  69. + case T_RATIONAL:
  70. + /* fall through */
  71. + case T_FIXNUM:
  72. + /* fall through */
  73. + case T_BIGNUM:
  74. + return ToValue(GetVpValue(iniValue, 1));
  75. +
  76. + case T_DATA:
  77. + if (rb_typeddata_is_kind_of(iniValue, &BigDecimal_data_type)) {
  78. + return BigDecimal_dup(iniValue);
  79. + }
  80. + break;
  81. +
  82. + case T_STRING:
  83. + /* fall through */
  84. + default:
  85. + break;
  86. + }
  87. + if (TYPE(iniValue) != T_STRING) {
  88. + rb_raise(rb_eArgError, "unable to convert to BigDecimal from %s.",
  89. + rb_obj_classname(iniValue));
  90. + }
  91. SafeStringValue(iniValue);
  92. GUARD_OBJ(pv,VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
  93. return ToValue(pv);
  94. }
  95.  
  96. +static VALUE
  97. +BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
  98. +{
  99. + ENTER(5);
  100. + Real *pv;
  101. + size_t mf;
  102. + VALUE nFig;
  103. + VALUE iniValue;
  104. +
  105. + if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
  106. + mf = 0;
  107. + } else {
  108. + mf = GetPositiveInt(nFig);
  109. + }
  110. + switch (TYPE(iniValue)) {
  111. + case T_RATIONAL:
  112. + /* fall through */
  113. + case T_FIXNUM:
  114. + /* fall through */
  115. + case T_BIGNUM:
  116. + return ToValue(GetVpValue(iniValue, 1));
  117. +
  118. + case T_DATA:
  119. + if (rb_typeddata_is_kind_of(iniValue, &BigDecimal_data_type)) {
  120. + return BigDecimal_dup(iniValue);
  121. + }
  122. + break;
  123. +
  124. + case T_STRING:
  125. + /* fall through */
  126. + default:
  127. + break;
  128. + }
  129. + if (TYPE(iniValue) != T_STRING) {
  130. + rb_raise(rb_eArgError, "unable to convert to BigDecimal from %s.",
  131. + rb_obj_classname(iniValue));
  132. + }
  133. + SafeStringValue(iniValue);
  134. + GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
  135. + return ToValue(pv);
  136. +}
  137. +
  138. /* call-seq:
  139. * BigDecimal.limit(digits)
  140. *
  141. diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
  142. index 8106305..61d0817 100644
  143. --- a/test/bigdecimal/test_bigdecimal.rb
  144. +++ b/test/bigdecimal/test_bigdecimal.rb
  145. @@ -27,6 +27,11 @@ class TestBigDecimal < Test::Unit::TestCase
  146. assert_equal(1, BigDecimal("1"))
  147. assert_equal(1, BigDecimal("1", 1))
  148. assert_raise(ArgumentError) { BigDecimal("1", -1) }
  149. +
  150. + assert_equal(1, BigDecimal(1))
  151. + assert_equal(-1, BigDecimal(-1))
  152. + assert_equal(BigDecimal("0.1"), BigDecimal(1.quo(10)))
  153. + assert_equal(BigDecimal("-0.1"), BigDecimal(-1.quo(10)))
  154. end
  155.  
  156. def test_new
  157. @@ -42,6 +47,11 @@ class TestBigDecimal < Test::Unit::TestCase
  158. assert_equal(-1, BigDecimal.new("-Infinity").infinite?)
  159. assert_equal(true, BigDecimal.new("NaN").nan?)
  160. assert_equal( 1, BigDecimal.new("1E1111111111111111111").infinite?)
  161. +
  162. + assert_equal(1, BigDecimal.new(1))
  163. + assert_equal(-1, BigDecimal.new(-1))
  164. + assert_equal(BigDecimal.new("0.1"), BigDecimal.new(1.quo(10)))
  165. + assert_equal(BigDecimal.new("-0.1"), BigDecimal.new(-1.quo(10)))
  166. end
  167.  
  168. def _test_mode(type)
  169. @@ -459,6 +469,9 @@ class TestBigDecimal < Test::Unit::TestCase
  170. assert_instance_of(Float, a)
  171. assert_instance_of(Float, b)
  172. assert_equal(2, 1 + BigDecimal.new("1"), '[ruby-core:25697]')
  173. +
  174. + a, b = BigDecimal("1").coerce(1.quo(10))
  175. + assert_equal(BigDecimal("0.1"), a, '[ruby-core:34318]')
  176. end
  177.  
  178. def test_uplus
Add Comment
Please, Sign In to add comment