[Bug #18937] Coerce non-real non-Numeric into Complex at comparisons
This commit is contained in:
parent
a9b59e24f4
commit
9212d96307
Notes:
git
2022-09-02 14:33:56 +09:00
16
complex.c
16
complex.c
@ -1124,15 +1124,23 @@ nucomp_cmp(VALUE self, VALUE other)
|
|||||||
if (!k_numeric_p(other)) {
|
if (!k_numeric_p(other)) {
|
||||||
return rb_num_coerce_cmp(self, other, idCmp);
|
return rb_num_coerce_cmp(self, other, idCmp);
|
||||||
}
|
}
|
||||||
if (nucomp_real_p(self)) {
|
if (!nucomp_real_p(self)) {
|
||||||
if (RB_TYPE_P(other, T_COMPLEX) && nucomp_real_p(other)) {
|
return Qnil;
|
||||||
|
}
|
||||||
|
if (RB_TYPE_P(other, T_COMPLEX)) {
|
||||||
|
if (nucomp_real_p(other)) {
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
return rb_funcall(adat->real, idCmp, 1, bdat->real);
|
return rb_funcall(adat->real, idCmp, 1, bdat->real);
|
||||||
}
|
}
|
||||||
else if (f_real_p(other)) {
|
}
|
||||||
get_dat1(self);
|
else {
|
||||||
|
get_dat1(self);
|
||||||
|
if (f_real_p(other)) {
|
||||||
return rb_funcall(dat->real, idCmp, 1, other);
|
return rb_funcall(dat->real, idCmp, 1, other);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return rb_num_coerce_cmp(dat->real, other, idCmp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
@ -568,19 +568,23 @@ class Complex_Test < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
class ObjectX
|
class ObjectX
|
||||||
def +(x) Rational(1) end
|
def initialize(real = true, n = 1) @n = n; @real = real; end
|
||||||
|
def +(x) Rational(@n) end
|
||||||
alias - +
|
alias - +
|
||||||
alias * +
|
alias * +
|
||||||
alias / +
|
alias / +
|
||||||
alias quo +
|
alias quo +
|
||||||
alias ** +
|
alias ** +
|
||||||
def coerce(x) [x, Complex(1)] end
|
def coerce(x) [x, Complex(@n)] end
|
||||||
|
def real?; @real; end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_coerce2
|
def test_coerce2
|
||||||
x = ObjectX.new
|
x = ObjectX.new
|
||||||
|
y = ObjectX.new(false)
|
||||||
%w(+ - * / quo ** <=>).each do |op|
|
%w(+ - * / quo ** <=>).each do |op|
|
||||||
assert_kind_of(Numeric, Complex(1).__send__(op, x))
|
assert_kind_of(Numeric, Complex(1).__send__(op, x), op)
|
||||||
|
assert_kind_of(Numeric, Complex(1).__send__(op, y), op)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user