diff options
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 13 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal.rb | 16 |
3 files changed, 41 insertions, 0 deletions
@@ -1,3 +1,15 @@ +Tue Jun 1 00:32:00 2011 Kenta Murata <mrkn@mrkn.jp> + + * ext/bigdecimal/bigdecimal.c (GetVpValueWithPrec): support instantiation from + a Float through Rational. + + * ext/bigdecimal/bigdecimal.c (BigDecimal_new): ditto. + + * test/bigdecimal/test_bigdecimal.rb (test_global_new_float): add a test for + the above changes. + + * test/bigdecimal/test_bigdecimal.rb (test_new_with_float): ditto. + Tue Jun 1 00:07:00 2011 Kenta Murata <mrkn@mrkn.jp> * ext/bigdecimal/bigdecimal.c (BigDecimal_coerce): support coerce with a diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 571bf41461..726c063a1d 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -50,6 +50,7 @@ static ID id_banker; static ID id_ceiling; static ID id_ceil; static ID id_floor; +static ID id_to_r; /* MACRO's to guard objects from GC by keeping them in stack */ #define ENTER(n) volatile VALUE vStack[n];int iStack=0 @@ -145,6 +146,11 @@ GetVpValueWithPrec(VALUE v, long prec, int must) again: switch(TYPE(v)) { + case T_FLOAT: + if (prec < 0) goto unable_to_coerce_without_prec; + if (prec > DBL_DIG+1)goto SomeOneMayDoIt; + v = rb_funcall(v, id_to_r, 0); + /* fall through */ case T_RATIONAL: if (prec < 0) goto unable_to_coerce_without_prec; @@ -171,6 +177,7 @@ again: goto SomeOneMayDoIt; } break; + case T_FIXNUM: sprintf(szD, "%ld", FIX2LONG(v)); return VpCreateRbObject(VpBaseFig() * 2 + 1, szD); @@ -1803,6 +1810,11 @@ BigDecimal_new(int argc, VALUE *argv, VALUE self) case T_BIGNUM: return ToValue(GetVpValue(iniValue, 1)); + case T_FLOAT: + if (mf > DBL_DIG+1) { + rb_raise(rb_eArgError, "precision too large."); + } + /* fall through */ case T_RATIONAL: if (NIL_P(nFig)) { rb_raise(rb_eArgError, "can't omit precision for a Rational."); @@ -2232,6 +2244,7 @@ Init_bigdecimal(void) id_ceiling = rb_intern_const("ceiling"); id_ceil = rb_intern_const("ceil"); id_floor = rb_intern_const("floor"); + id_to_r = rb_intern_const("to_r"); } /* diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 63873d2b7c..c0c5ea44a0 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -42,6 +42,14 @@ class TestBigDecimal < Test::Unit::TestCase assert_raise(ArgumentError) { BigDecimal(1.quo(3)) } end + def test_global_new_with_float + assert_equal(BigDecimal("0.1235"), BigDecimal(0.1234567, 4)) + assert_equal(BigDecimal("-0.1235"), BigDecimal(-0.1234567, 4)) + assert_raise(ArgumentError) { BigDecimal(0.1) } + assert_raise(ArgumentError) { BigDecimal(0.1, Float::DIG + 2) } + assert_nothing_raised { BigDecimal(0.1, Float::DIG + 1) } + end + def test_new assert_equal(1, BigDecimal.new("1")) assert_equal(1, BigDecimal.new("1", 1)) @@ -70,6 +78,14 @@ class TestBigDecimal < Test::Unit::TestCase assert_raise(ArgumentError) { BigDecimal.new(1.quo(3)) } end + def test_new_with_float + assert_equal(BigDecimal("0.1235"), BigDecimal(0.1234567, 4)) + assert_equal(BigDecimal("-0.1235"), BigDecimal(-0.1234567, 4)) + assert_raise(ArgumentError) { BigDecimal.new(0.1) } + assert_raise(ArgumentError) { BigDecimal.new(0.1, Float::DIG + 2) } + assert_nothing_raised { BigDecimal.new(0.1, Float::DIG + 1) } + end + def _test_mode(type) BigDecimal.mode(type, true) assert_raise(FloatDomainError) { yield } |