ruby-changes:50055
From: nagachika <ko1@a...>
Date: Sat, 3 Feb 2018 11:39:48 +0900 (JST)
Subject: [ruby-changes:50055] nagachika:r62173 (ruby_2_4): merge revision(s) 60040, 60188: [Backport #14014]
nagachika 2018-02-03 11:39:42 +0900 (Sat, 03 Feb 2018) New Revision: 62173 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62173 Log: merge revision(s) 60040,60188: [Backport #14014] complex.c: no overflow * complex.c (rb_complex_finite_p): get rid of overflow and unnecessary multiplication. test_complex.rb: NaN Complex * test/ruby/test_complex.rb (test_finite_p): assertions for NaN Complex. NaN is not an infinite nor a finite number. [ruby-core:83272] [Bug #14014] Modified directories: branches/ruby_2_4/ Modified files: branches/ruby_2_4/complex.c branches/ruby_2_4/numeric.c branches/ruby_2_4/test/ruby/test_complex.rb branches/ruby_2_4/version.h Index: ruby_2_4/test/ruby/test_complex.rb =================================================================== --- ruby_2_4/test/ruby/test_complex.rb (revision 62172) +++ ruby_2_4/test/ruby/test_complex.rb (revision 62173) @@ -832,6 +832,12 @@ class Complex_Test < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/ruby_2_4/test/ruby/test_complex.rb#L832 assert_predicate(-1-1i, :finite?) assert_not_predicate(Float::INFINITY + 1i, :finite?) assert_not_predicate(Complex(1, Float::INFINITY), :finite?) + assert_predicate(Complex(Float::MAX, 0.0), :finite?) + assert_predicate(Complex(0.0, Float::MAX), :finite?) + assert_predicate(Complex(Float::MAX, Float::MAX), :finite?) + assert_not_predicate(Complex(Float::NAN, 0), :finite?) + assert_not_predicate(Complex(0, Float::NAN), :finite?) + assert_not_predicate(Complex(Float::NAN, Float::NAN), :finite?) end def test_infinite_p @@ -847,6 +853,9 @@ class Complex_Test < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/ruby_2_4/test/ruby/test_complex.rb#L853 assert_equal(1, Complex(-1, Float::INFINITY).infinite?) assert_equal(1, Complex(1, -Float::INFINITY).infinite?) assert_equal(1, Complex(-1, -Float::INFINITY).infinite?) + assert_nil(Complex(Float::NAN, 0).infinite?) + assert_nil(Complex(0, Float::NAN).infinite?) + assert_nil(Complex(Float::NAN, Float::NAN).infinite?) end def test_supp Index: ruby_2_4/numeric.c =================================================================== --- ruby_2_4/numeric.c (revision 62172) +++ ruby_2_4/numeric.c (revision 62173) @@ -1782,8 +1782,8 @@ flo_is_infinite_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/ruby_2_4/numeric.c#L1782 * */ -static VALUE -flo_is_finite_p(VALUE num) +VALUE +rb_flo_is_finite_p(VALUE num) { double value = RFLOAT_VALUE(num); @@ -5495,7 +5495,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/ruby_2_4/numeric.c#L5495 rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0); rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0); - rb_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0); + rb_define_method(rb_cFloat, "finite?", rb_flo_is_finite_p, 0); rb_define_method(rb_cFloat, "next_float", flo_next_float, 0); rb_define_method(rb_cFloat, "prev_float", flo_prev_float, 0); rb_define_method(rb_cFloat, "positive?", flo_positive_p, 0); Index: ruby_2_4/version.h =================================================================== --- ruby_2_4/version.h (revision 62172) +++ ruby_2_4/version.h (revision 62173) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_4/version.h#L1 #define RUBY_VERSION "2.4.4" #define RUBY_RELEASE_DATE "2018-02-03" -#define RUBY_PATCHLEVEL 230 +#define RUBY_PATCHLEVEL 231 #define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_MONTH 2 Index: ruby_2_4/complex.c =================================================================== --- ruby_2_4/complex.c (revision 62172) +++ ruby_2_4/complex.c (revision 62173) @@ -237,6 +237,22 @@ f_zero_p(VALUE x) https://github.com/ruby/ruby/blob/trunk/ruby_2_4/complex.c#L237 #define f_nonzero_p(x) (!f_zero_p(x)) +VALUE rb_flo_is_finite_p(VALUE num); +inline static int +f_finite_p(VALUE x) +{ + if (RB_INTEGER_TYPE_P(x)) { + return TRUE; + } + else if (RB_FLOAT_TYPE_P(x)) { + return (int)rb_flo_is_finite_p(x); + } + else if (RB_TYPE_P(x, T_RATIONAL)) { + return TRUE; + } + return RTEST(rb_funcallv(x, id_finite_p, 0, 0)); +} + inline static int f_kind_of_p(VALUE x, VALUE c) { @@ -1326,18 +1342,12 @@ nucomp_inspect(VALUE self) https://github.com/ruby/ruby/blob/trunk/ruby_2_4/complex.c#L1342 static VALUE rb_complex_finite_p(VALUE self) { - VALUE magnitude = nucomp_abs(self); + get_dat1(self); - if (FINITE_TYPE_P(magnitude)) { + if (f_finite_p(dat->real) && f_finite_p(dat->imag)) { return Qtrue; } - else if (RB_FLOAT_TYPE_P(magnitude)) { - const double f = RFLOAT_VALUE(magnitude); - return isinf(f) ? Qfalse : Qtrue; - } - else { - return rb_funcall(magnitude, id_finite_p, 0); - } + return Qfalse; } /* Index: ruby_2_4 =================================================================== --- ruby_2_4 (revision 62172) +++ ruby_2_4 (revision 62173) Property changes on: ruby_2_4 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /trunk:r60040,60188 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/