Skip to content

Commit d3c1b0b

Browse files
committed
Use rb_float_convert_to_BigDecimal in GetVpValueWIthPrec
1 parent 33e7c50 commit d3c1b0b

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ cannot_be_coerced_into_BigDecimal(VALUE exc_class, VALUE v)
199199
}
200200

201201
static inline VALUE BigDecimal_div2(VALUE, VALUE, VALUE);
202+
static VALUE rb_float_convert_to_BigDecimal(VALUE val, size_t digs, int raise_exception);
202203

203204
static Real*
204205
GetVpValueWithPrec(VALUE v, long prec, int must)
@@ -208,27 +209,14 @@ GetVpValueWithPrec(VALUE v, long prec, int must)
208209
VALUE num, bg;
209210
char szD[128];
210211
VALUE orig = Qundef;
211-
double d;
212212

213213
again:
214214
switch(TYPE(v)) {
215-
case T_FLOAT:
216-
if (prec < 0) goto unable_to_coerce_without_prec;
217-
if (prec > (long)DBLE_FIG) goto SomeOneMayDoIt;
218-
d = RFLOAT_VALUE(v);
219-
if (!isfinite(d)) {
220-
pv = VpCreateRbObject(1, NULL, true);
221-
VpDtoV(pv, d);
222-
return pv;
223-
}
224-
if (d != 0.0) {
225-
v = rb_funcall(v, id_to_r, 0);
226-
goto again;
227-
}
228-
if (1/d < 0.0) {
229-
return VpCreateRbObject(prec, "-0", true);
230-
}
231-
return VpCreateRbObject(prec, "0", true);
215+
case T_FLOAT: {
216+
VALUE obj = rb_float_convert_to_BigDecimal(v, prec, must);
217+
TypedData_Get_Struct(obj, Real, &BigDecimal_data_type, pv);
218+
return pv;
219+
}
232220

233221
case T_RATIONAL:
234222
if (prec < 0) goto unable_to_coerce_without_prec;

0 commit comments

Comments
 (0)