summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-03 02:39:42 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-03 02:39:42 +0000
commit65992061b918c397cfff6214bec556d7e83b835d (patch)
treec445905ea340318687021ed6126304fde7e69157
parentce2bc45ae8679b40e97d6794e4e7a31b68444615 (diff)
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@62173 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--complex.c28
-rw-r--r--numeric.c6
-rw-r--r--test/ruby/test_complex.rb9
-rw-r--r--version.h2
4 files changed, 32 insertions, 13 deletions
diff --git a/complex.c b/complex.c
index 4737891270..5aa76ad230 100644
--- a/complex.c
+++ b/complex.c
@@ -237,6 +237,22 @@ f_zero_p(VALUE x)
#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)
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;
}
/*
diff --git a/numeric.c b/numeric.c
index 631e199dcc..de30ab8497 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1782,8 +1782,8 @@ flo_is_infinite_p(VALUE num)
*
*/
-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)
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);
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index 5707047338..8e5f637b79 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -832,6 +832,12 @@ class Complex_Test < Test::Unit::TestCase
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::TestCase
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
diff --git a/version.h b/version.h
index dad6505d5c..e9e584cbc4 100644
--- a/version.h
+++ b/version.h
@@ -1,6 +1,6 @@
#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