Returns a newFloat object having approximately the same value as
the BigDecimal number. Normal accuracy limits and built-in
errors of binary Float arithmetic apply.
static VALUE
BigDecimal_to_f(VALUE self)
{
ENTER(1);
Real *p;
double d;
S_LONG e;
char *buf;
volatile VALUE str;
GUARD_OBJ(p,GetVpValue(self,1));
if (VpVtoD(&d, &e, p)!=1) return rb_float_new(d);
if (e > DBL_MAX_10_EXP+BASE_FIG) goto erange;
str = rb_str_new(0, VpNumOfChars(p,"E"));
buf = RSTRING_PTR(str);
VpToString(p, buf, 0, 0);
errno = 0;
d = strtod(buf, 0);
if(errno == ERANGE) {
erange:
VpException(VP_EXCEPTION_OVERFLOW,"BigDecimal to Float conversion",0);
if(d>0.0) d = VpGetDoublePosInf();
else d = VpGetDoubleNegInf();
}
return rb_float_new(d);
}