summaryrefslogtreecommitdiff
path: root/complex.c
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 /complex.c
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
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c28
1 files changed, 19 insertions, 9 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;
}
/*