From d3c1b0b9218f3106736897044cefeb8c6c926279 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Sat, 9 Jan 2021 15:16:35 +0900 Subject: [PATCH] Use rb_float_convert_to_BigDecimal in GetVpValueWIthPrec --- ext/bigdecimal/bigdecimal.c | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 5defd0f1..51946154 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -199,6 +199,7 @@ 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 Real* GetVpValueWithPrec(VALUE v, long prec, int must) @@ -208,27 +209,14 @@ GetVpValueWithPrec(VALUE v, long prec, int must) VALUE num, bg; char szD[128]; VALUE orig = Qundef; - double d; again: switch(TYPE(v)) { - case T_FLOAT: - if (prec < 0) goto unable_to_coerce_without_prec; - if (prec > (long)DBLE_FIG) goto SomeOneMayDoIt; - d = RFLOAT_VALUE(v); - if (!isfinite(d)) { - pv = VpCreateRbObject(1, NULL, true); - VpDtoV(pv, d); - return pv; - } - if (d != 0.0) { - v = rb_funcall(v, id_to_r, 0); - goto again; - } - if (1/d < 0.0) { - return VpCreateRbObject(prec, "-0", true); - } - return VpCreateRbObject(prec, "0", true); + case T_FLOAT: { + VALUE obj = rb_float_convert_to_BigDecimal(v, prec, must); + TypedData_Get_Struct(obj, Real, &BigDecimal_data_type, pv); + return pv; + } case T_RATIONAL: if (prec < 0) goto unable_to_coerce_without_prec;