summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenta Murata <mrkn@mrkn.jp>2021-01-09 15:28:08 +0900
committerKenta Murata <mrkn@mrkn.jp>2021-01-09 20:36:47 +0900
commit3d9c95996dfc8e1d6bc263509273f13caaf06f77 (patch)
tree20f325ed4dcc5d00b7f9489903eed7009ca8dacc
parentbe34e31d8e68b365a104c955389c6e9f865e054e (diff)
[ruby/bigdecimal] Stop using GetVpValueWithPrec in rb_rational_convert_to_BigDecimal
https://github.com/ruby/bigdecimal/commit/b4f470da61 https://github.com/ruby/bigdecimal/commit/44a78df866
-rw-r--r--ext/bigdecimal/bigdecimal.c40
1 files changed, 11 insertions, 29 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index ef17882db1..460456d953 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -200,17 +200,16 @@ cannot_be_coerced_into_BigDecimal(VALUE exc_class, VALUE v)
static inline VALUE BigDecimal_div2(VALUE, VALUE, VALUE);
static VALUE rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
+static VALUE rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
static Real*
GetVpValueWithPrec(VALUE v, long prec, int must)
{
ENTER(1);
Real *pv;
- VALUE num, bg;
+ VALUE bg;
char szD[128];
- VALUE orig = Qundef;
-again:
switch(TYPE(v)) {
case T_FLOAT: {
VALUE obj = rb_float_convert_to_BigDecimal(v, prec, must);
@@ -218,20 +217,11 @@ again:
return pv;
}
- case T_RATIONAL:
- if (prec < 0) goto unable_to_coerce_without_prec;
-
- if (orig == Qundef ? (orig = v, 1) : orig != v) {
- num = rb_rational_num(v);
- pv = GetVpValueWithPrec(num, -1, must);
- if (pv == NULL) goto SomeOneMayDoIt;
-
- v = BigDecimal_div2(VpCheckGetValue(pv), rb_rational_den(v), LONG2NUM(prec));
- goto again;
- }
-
- v = orig;
- goto SomeOneMayDoIt;
+ case T_RATIONAL: {
+ VALUE obj = rb_rational_convert_to_BigDecimal(v, prec, must);
+ TypedData_Get_Struct(obj, Real, &BigDecimal_data_type, pv);
+ return pv;
+ }
case T_DATA:
if (is_kind_of_BigDecimal(v)) {
@@ -268,14 +258,6 @@ SomeOneMayDoIt:
cannot_be_coerced_into_BigDecimal(rb_eTypeError, v);
}
return NULL; /* NULL means to coerce */
-
-unable_to_coerce_without_prec:
- if (must) {
- rb_raise(rb_eArgError,
- "%"PRIsVALUE" can't be coerced into BigDecimal without a precision",
- RB_OBJ_CLASSNAME(v));
- }
- return NULL;
}
static Real*
@@ -2794,8 +2776,6 @@ rb_inum_convert_to_BigDecimal(VALUE val, RB_UNUSED_VAR(size_t digs), int raise_e
}
}
-static VALUE rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
-
static VALUE
rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
{
@@ -2849,8 +2829,10 @@ rb_rational_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception)
"can't omit precision for a %"PRIsVALUE".",
CLASS_OF(val));
}
- Real *vp = GetVpValueWithPrec(val, digs, 1);
- return check_exception(vp->obj);
+
+ VALUE num = rb_inum_convert_to_BigDecimal(rb_rational_num(val), 0, raise_exception);
+ VALUE d = BigDecimal_div2(num, rb_rational_den(val), SIZET2NUM(digs));
+ return d;
}
static VALUE