diff --git a/internal.h b/internal.h index 6a7f039cd0..dd7d09a6e3 100644 --- a/internal.h +++ b/internal.h @@ -1178,6 +1178,7 @@ VALUE rb_int_cmp(VALUE x, VALUE y); VALUE rb_int_equal(VALUE x, VALUE y); VALUE rb_int_divmod(VALUE x, VALUE y); VALUE rb_int_and(VALUE x, VALUE y); +VALUE rb_int_lshift(VALUE x, VALUE y); #if USE_FLONUM #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) diff --git a/numeric.c b/numeric.c index e0d6358932..590bec72e1 100644 --- a/numeric.c +++ b/numeric.c @@ -4390,7 +4390,7 @@ fix_lshift(long val, unsigned long width) return LONG2NUM(val); } -static VALUE +VALUE rb_int_lshift(VALUE x, VALUE y) { if (FIXNUM_P(x)) { diff --git a/rational.c b/rational.c index 67752ff961..c7f66c4767 100644 --- a/rational.c +++ b/rational.c @@ -2071,14 +2071,17 @@ float_to_r(VALUE self) long ln = FIX2LONG(n); if (ln == 0) - return f_to_r(f); + return rb_rational_new1(f); if (ln > 0) - return f_to_r(f_lshift(f, n)); + return rb_rational_new1(rb_int_lshift(f, n)); ln = -ln; - return rb_rational_new2(f, f_lshift(ONE, INT2FIX(ln))); + return rb_rational_new2(f, rb_int_lshift(ONE, INT2FIX(ln))); } #else - return f_to_r(f_mul(f, f_expt(INT2FIX(FLT_RADIX), n))); + f = rb_int_mul(f, rb_int_pow(INT2FIX(FLT_RADIX), n)); + if (RB_TYPE_P(f, T_RATIONAL)) + return f; + return rb_rational_new1(f); #endif }