From ea52cac1487cea91a148daffbfe0dacef672051b Mon Sep 17 00:00:00 2001 From: naruse Date: Sat, 11 Mar 2017 13:03:46 +0000 Subject: merge revision(s) 57227: [Backport #13078] numeric.c: reduce fdiv * numeric.c (rb_int_fdiv_double): reduce first for more precise result. [ruby-core:78886] [Bug #13078] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@57840 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- internal.h | 1 + numeric.c | 7 +++++++ test/ruby/test_rational.rb | 7 +++++++ version.h | 2 +- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/internal.h b/internal.h index 70cf607234..2346345480 100644 --- a/internal.h +++ b/internal.h @@ -1682,6 +1682,7 @@ VALUE rb_execarg_extract_options(VALUE execarg_obj, VALUE opthash); void rb_execarg_setenv(VALUE execarg_obj, VALUE env); /* rational.c (export) */ +VALUE rb_gcd(VALUE x, VALUE y); VALUE rb_gcd_normal(VALUE self, VALUE other); #if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H) VALUE rb_gcd_gmp(VALUE x, VALUE y); diff --git a/numeric.c b/numeric.c index 01f9f45420..28ad30d764 100644 --- a/numeric.c +++ b/numeric.c @@ -3614,6 +3614,13 @@ fix_fdiv_double(VALUE x, VALUE y) double rb_int_fdiv_double(VALUE x, VALUE y) { + if (RB_INTEGER_TYPE_P(y) && !FIXNUM_ZERO_P(y)) { + VALUE gcd = rb_gcd(x, y); + if (!FIXNUM_ZERO_P(gcd)) { + x = rb_int_idiv(x, gcd); + y = rb_int_idiv(y, gcd); + } + } if (FIXNUM_P(x)) { return fix_fdiv_double(x, y); } diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 99f80bc561..9651f46f01 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -905,6 +905,13 @@ class Rational_Test < Test::Unit::TestCase assert_equal(5000000000.0, 10000000000.fdiv(2)) assert_equal(0.5, 1.0.fdiv(2)) assert_equal(0.25, Rational(1,2).fdiv(2)) + + a = 0xa42fcabf_c51ce400_00001000_00000000_00000000_00000000_00000000_00000000 + b = 1<<1074 + assert_equal(Rational(a, b).to_f, a.fdiv(b)) + a = 3 + b = 0x20_0000_0000_0001 + assert_equal(Rational(a, b).to_f, a.fdiv(b)) end def test_ruby19 diff --git a/version.h b/version.h index 390006b1ae..5fefe537b2 100644 --- a/version.h +++ b/version.h @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.4.0" #define RUBY_RELEASE_DATE "2017-03-11" -#define RUBY_PATCHLEVEL 8 +#define RUBY_PATCHLEVEL 9 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 3 -- cgit v1.2.3