ruby-changes:47919
From: nobu <ko1@a...>
Date: Wed, 27 Sep 2017 11:38:55 +0900 (JST)
Subject: [ruby-changes:47919] nobu:r60040 (trunk): complex.c: no overflow
nobu 2017-09-27 11:38:51 +0900 (Wed, 27 Sep 2017) New Revision: 60040 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60040 Log: complex.c: no overflow * complex.c (rb_complex_finite_p): get rid of overflow and unnecessary multiplication. Modified files: trunk/complex.c trunk/numeric.c trunk/test/ruby/test_complex.rb Index: complex.c =================================================================== --- complex.c (revision 60039) +++ complex.c (revision 60040) @@ -237,6 +237,22 @@ f_zero_p(VALUE x) https://github.com/ruby/ruby/blob/trunk/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/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: numeric.c =================================================================== --- numeric.c (revision 60039) +++ numeric.c (revision 60040) @@ -1763,8 +1763,8 @@ flo_is_infinite_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1763 * i.e. it is not infinite and Float#nan? is +false+. */ -static VALUE -flo_is_finite_p(VALUE num) +VALUE +rb_flo_is_finite_p(VALUE num) { double value = RFLOAT_VALUE(num); @@ -5592,7 +5592,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5592 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: test/ruby/test_complex.rb =================================================================== --- test/ruby/test_complex.rb (revision 60039) +++ test/ruby/test_complex.rb (revision 60040) @@ -832,6 +832,9 @@ class Complex_Test < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/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?) end def test_infinite_p -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/