From: Tadayoshi Funaba Date: 2009-06-15T22:30:15+09:00 Subject: [ruby-dev:38637] Re: Fixnum#fdiv 後者の考えに基いて修正してみました。 Inf.fdiv(Float::MAX.to_i*2) などの場合も対処しないといけないと思いますが、とりあえず。 Index: complex.c =================================================================== --- complex.c (revision 23694) +++ complex.c (working copy) @@ -660,6 +660,7 @@ static VALUE nucomp_fdiv(VALUE self, VALUE other) { + rb_warning("Complex#fdiv will be deprecated in the future"); return nucomp_divide(self, other, f_fdiv, id_fdiv); } Index: bignum.c =================================================================== --- bignum.c (revision 23694) +++ bignum.c (working copy) @@ -2382,6 +2382,53 @@ return x; } +static VALUE +big_fdiv(VALUE x, VALUE y) +{ +#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG) + VALUE z; + long l, ex, ey; + int i; + + bigtrunc(x); + l = RBIGNUM_LEN(x) - 1; + ex = l * BITSPERDIG; + ex += bdigbitsize(BDIGITS(x)[l]); + ex -= 2 * DBL_BIGDIG * BITSPERDIG; + if (ex) x = big_shift(x, ex); + + switch (TYPE(y)) { + case T_FIXNUM: + y = rb_int2big(FIX2LONG(y)); + case T_BIGNUM: { + bigtrunc(y); + l = RBIGNUM_LEN(y) - 1; + ey = l * BITSPERDIG; + ey += bdigbitsize(BDIGITS(y)[l]); + ey -= DBL_BIGDIG * BITSPERDIG; + if (ey) y = big_shift(y, ey); + bignum: + bigdivrem(x, y, &z, 0); + l = ex - ey; +#if SIZEOF_LONG > SIZEOF_INT + { + /* Visual C++ can't be here */ + if (l > INT_MAX) return DBL2NUM(ruby_div0(1.0)); + if (l < INT_MIN) return DBL2NUM(0.0); + } +#endif + return DBL2NUM(ldexp(big2dbl(z), (int)l)); + } + case T_FLOAT: + y = dbl2big(ldexp(frexp(RFLOAT_VALUE(y), &i), DBL_MANT_DIG)); + ey = i - DBL_MANT_DIG; + goto bignum; + } + /* NOTREACHED */ + abort(); + return Qnil; +} + /* * call-seq: * big.fdiv(numeric) -> float @@ -2394,65 +2441,32 @@ * */ -static VALUE + +VALUE rb_big_fdiv(VALUE x, VALUE y) { - double dx = big2dbl(x); - double dy; + double dx, dy; - if (isinf(dx)) { -#define DBL_BIGDIG ((DBL_MANT_DIG + BITSPERDIG) / BITSPERDIG) - VALUE z; - long l, ex, ey; - int i; - - bigtrunc(x); - l = RBIGNUM_LEN(x) - 1; - ex = l * BITSPERDIG; - ex += bdigbitsize(BDIGITS(x)[l]); - ex -= 2 * DBL_BIGDIG * BITSPERDIG; - if (ex) x = big_shift(x, ex); - - switch (TYPE(y)) { - case T_FIXNUM: - y = rb_int2big(FIX2LONG(y)); - case T_BIGNUM: { - bigtrunc(y); - l = RBIGNUM_LEN(y) - 1; - ey = l * BITSPERDIG; - ey += bdigbitsize(BDIGITS(y)[l]); - ey -= DBL_BIGDIG * BITSPERDIG; - if (ey) y = big_shift(y, ey); - bignum: - bigdivrem(x, y, &z, 0); - l = ex - ey; -#if SIZEOF_LONG > SIZEOF_INT - { - /* Visual C++ can't be here */ - if (l > INT_MAX) return DBL2NUM(ruby_div0(1.0)); - if (l < INT_MIN) return DBL2NUM(0.0); - } -#endif - return DBL2NUM(ldexp(big2dbl(z), (int)l)); - } - case T_FLOAT: - if (isnan(RFLOAT_VALUE(y))) return y; - y = dbl2big(ldexp(frexp(RFLOAT_VALUE(y), &i), DBL_MANT_DIG)); - ey = i - DBL_MANT_DIG; - goto bignum; - } - } + dx = big2dbl(x); switch (TYPE(y)) { case T_FIXNUM: dy = (double)FIX2LONG(y); + if (isinf(dx)) + return big_fdiv(x, y); break; case T_BIGNUM: dy = rb_big2dbl(y); + if (isinf(dx) || isinf(dy)) + return big_fdiv(x, y); break; case T_FLOAT: dy = RFLOAT_VALUE(y); + if (isnan(dy)) + return y; + if (isinf(dx)) + return big_fdiv(x, y); break; default: Index: rational.c =================================================================== --- rational.c (revision 23694) +++ rational.c (working copy) @@ -769,7 +769,7 @@ static VALUE nurat_fdiv(VALUE self, VALUE other) { - return f_div(f_to_f(self), other); + return f_to_f(f_div(self, other)); } static VALUE