ruby-changes:45155
From: nobu <ko1@a...>
Date: Wed, 28 Dec 2016 13:41:04 +0900 (JST)
Subject: [ruby-changes:45155] nobu:r57227 (trunk): numeric.c: reduce fdiv
nobu 2016-12-28 13:40:58 +0900 (Wed, 28 Dec 2016) New Revision: 57227 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57227 Log: numeric.c: reduce fdiv * numeric.c (rb_int_fdiv_double): reduce first for more precise result. [ruby-core:78886] [Bug #13078] Modified files: trunk/internal.h trunk/numeric.c trunk/test/ruby/test_rational.rb Index: numeric.c =================================================================== --- numeric.c (revision 57226) +++ numeric.c (revision 57227) @@ -3614,6 +3614,13 @@ fix_fdiv_double(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3614 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); } Index: internal.h =================================================================== --- internal.h (revision 57226) +++ internal.h (revision 57227) @@ -1682,6 +1682,7 @@ VALUE rb_execarg_extract_options(VALUE e https://github.com/ruby/ruby/blob/trunk/internal.h#L1682 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); Index: test/ruby/test_rational.rb =================================================================== --- test/ruby/test_rational.rb (revision 57226) +++ test/ruby/test_rational.rb (revision 57227) @@ -905,6 +905,13 @@ class Rational_Test < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L905 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/