Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
- index 53fb678..988a2a1 100644
- --- a/ext/bigdecimal/bigdecimal.c
- +++ b/ext/bigdecimal/bigdecimal.c
- @@ -131,6 +131,8 @@ ToValue(Real *p)
- return p->obj;
- }
- +static VALUE BigDecimal_div2(int, VALUE*, VALUE);
- +
- static Real *
- GetVpValue(VALUE v, int must)
- {
- @@ -145,11 +147,13 @@ again:
- {
- case T_RATIONAL:
- if(orig == Qundef ? (orig = v, 1) : orig != v) {
- - if(!util_loaded) {
- - rb_require("bigdecimal/util");
- - util_loaded = 1;
- - }
- - v = rb_funcall2(v, rb_intern("to_d"), 0, 0);
- + VALUE num, args[2];
- + num = RRATIONAL(v)->num;
- + args[0] = RRATIONAL(v)->den;
- + args[1] = INT2FIX(VpDblFig()*2 + 1);
- + pv = GetVpValue(num, must);
- + if (pv == NULL) goto SomeOneMayDoIt;
- + v = BigDecimal_div2(2, args, ToValue(pv));
- goto again;
- }
- v = orig;
- @@ -1738,25 +1742,6 @@ BigDecimal_power(VALUE self, VALUE p)
- return ToValue(y);
- }
- -static VALUE
- -BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
- -{
- - ENTER(5);
- - Real *pv;
- - size_t mf;
- - VALUE nFig;
- - VALUE iniValue;
- -
- - if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
- - mf = 0;
- - } else {
- - mf = GetPositiveInt(nFig);
- - }
- - SafeStringValue(iniValue);
- - GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
- - return ToValue(pv);
- -}
- -
- /* call-seq:
- * new(initial, digits)
- *
- @@ -1780,14 +1765,80 @@ BigDecimal_new(int argc, VALUE *argv, VALUE self)
- if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
- mf = 0;
- - } else {
- + }
- + else {
- mf = GetPositiveInt(nFig);
- }
- + switch (TYPE(iniValue)) {
- + case T_RATIONAL:
- + /* fall through */
- + case T_FIXNUM:
- + /* fall through */
- + case T_BIGNUM:
- + return ToValue(GetVpValue(iniValue, 1));
- +
- + case T_DATA:
- + if (rb_typeddata_is_kind_of(iniValue, &BigDecimal_data_type)) {
- + return BigDecimal_dup(iniValue);
- + }
- + break;
- +
- + case T_STRING:
- + /* fall through */
- + default:
- + break;
- + }
- + if (TYPE(iniValue) != T_STRING) {
- + rb_raise(rb_eArgError, "unable to convert to BigDecimal from %s.",
- + rb_obj_classname(iniValue));
- + }
- SafeStringValue(iniValue);
- GUARD_OBJ(pv,VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
- return ToValue(pv);
- }
- +static VALUE
- +BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
- +{
- + ENTER(5);
- + Real *pv;
- + size_t mf;
- + VALUE nFig;
- + VALUE iniValue;
- +
- + if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
- + mf = 0;
- + } else {
- + mf = GetPositiveInt(nFig);
- + }
- + switch (TYPE(iniValue)) {
- + case T_RATIONAL:
- + /* fall through */
- + case T_FIXNUM:
- + /* fall through */
- + case T_BIGNUM:
- + return ToValue(GetVpValue(iniValue, 1));
- +
- + case T_DATA:
- + if (rb_typeddata_is_kind_of(iniValue, &BigDecimal_data_type)) {
- + return BigDecimal_dup(iniValue);
- + }
- + break;
- +
- + case T_STRING:
- + /* fall through */
- + default:
- + break;
- + }
- + if (TYPE(iniValue) != T_STRING) {
- + rb_raise(rb_eArgError, "unable to convert to BigDecimal from %s.",
- + rb_obj_classname(iniValue));
- + }
- + SafeStringValue(iniValue);
- + GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
- + return ToValue(pv);
- +}
- +
- /* call-seq:
- * BigDecimal.limit(digits)
- *
- diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
- index 8106305..61d0817 100644
- --- a/test/bigdecimal/test_bigdecimal.rb
- +++ b/test/bigdecimal/test_bigdecimal.rb
- @@ -27,6 +27,11 @@ class TestBigDecimal < Test::Unit::TestCase
- assert_equal(1, BigDecimal("1"))
- assert_equal(1, BigDecimal("1", 1))
- assert_raise(ArgumentError) { BigDecimal("1", -1) }
- +
- + assert_equal(1, BigDecimal(1))
- + assert_equal(-1, BigDecimal(-1))
- + assert_equal(BigDecimal("0.1"), BigDecimal(1.quo(10)))
- + assert_equal(BigDecimal("-0.1"), BigDecimal(-1.quo(10)))
- end
- def test_new
- @@ -42,6 +47,11 @@ class TestBigDecimal < Test::Unit::TestCase
- assert_equal(-1, BigDecimal.new("-Infinity").infinite?)
- assert_equal(true, BigDecimal.new("NaN").nan?)
- assert_equal( 1, BigDecimal.new("1E1111111111111111111").infinite?)
- +
- + assert_equal(1, BigDecimal.new(1))
- + assert_equal(-1, BigDecimal.new(-1))
- + assert_equal(BigDecimal.new("0.1"), BigDecimal.new(1.quo(10)))
- + assert_equal(BigDecimal.new("-0.1"), BigDecimal.new(-1.quo(10)))
- end
- def _test_mode(type)
- @@ -459,6 +469,9 @@ class TestBigDecimal < Test::Unit::TestCase
- assert_instance_of(Float, a)
- assert_instance_of(Float, b)
- assert_equal(2, 1 + BigDecimal.new("1"), '[ruby-core:25697]')
- +
- + a, b = BigDecimal("1").coerce(1.quo(10))
- + assert_equal(BigDecimal("0.1"), a, '[ruby-core:34318]')
- end
- def test_uplus
Add Comment
Please, Sign In to add comment