aboutsummaryrefslogtreecommitdiffstats
path: root/ext/bigdecimal/bigdecimal.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-31 15:33:42 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-31 15:33:42 +0000
commit678b6820f70d6bd09d6e5179755311f0774db22a (patch)
tree8f0046c73f7bfef401bc285dab97ad83c25e2b4f /ext/bigdecimal/bigdecimal.c
parent8f9ff461cf1f8d7e8031f9f5b7755bf154655d74 (diff)
downloadruby-678b6820f70d6bd09d6e5179755311f0774db22a.tar.gz
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31870 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal/bigdecimal.c')
-rw-r--r--ext/bigdecimal/bigdecimal.c13
1 files changed, 13 insertions, 0 deletions
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");
}
/*