aboutsummaryrefslogtreecommitdiffstats
path: root/complex.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-27 02:38:51 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-27 02:38:51 +0000
commit215b33e5bfeb264c3ee3f7d05bd0c742fc48ff65 (patch)
tree418f4554975429beaa0bafadc986e88b7facc091 /complex.c
parentc40a51bcd42311061536627d569d27c894c227a2 (diff)
downloadruby-215b33e5bfeb264c3ee3f7d05bd0c742fc48ff65.tar.gz
complex.c: no overflow
* complex.c (rb_complex_finite_p): get rid of overflow and unnecessary multiplication. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60040 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 10ab4a4563..7cf1e7bf8a 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;
}
/*