Flowdock
to_f() public

No documentation

This method has no description. You can help the Ruby community by adding new notes.

Hide source
static VALUE
nurat_to_f(VALUE self)
{
    VALUE num, den;
    int minus = 0;
    long nl, dl, ne, de;
    int e;
    double f;

    {
        get_dat1(self);

        if (f_zero_p(dat->num))
            return rb_float_new(0.0);

        num = dat->num;
        den = dat->den;
    }

    if (f_negative_p(num)) {
        num = f_negate(num);
        minus = 1;
    }

    nl = i_ilog2(num);
    dl = i_ilog2(den);

    ne = 0;
    if (nl > ml) {
        ne = nl - ml;
        num = f_rshift(num, LONG2NUM(ne));
    }

    de = 0;
    if (dl > ml) {
        de = dl - ml;
        den = f_rshift(den, LONG2NUM(de));
    }

    e = (int)(ne - de);

    if ((e > DBL_MAX_EXP) || (e < DBL_MIN_EXP)) {
        rb_warning("%s out of Float range", rb_obj_classname(self));
        return rb_float_new(e > 0 ? HUGE_VAL : 0.0);
    }

    f = NUM2DBL(num) / NUM2DBL(den);
    if (minus)
        f = -f;
    f = ldexp(f, e);

    if (isinf(f) || isnan(f))
        rb_warning("%s out of Float range", rb_obj_classname(self));

    return rb_float_new(f);
}
Register or log in to add new notes.