summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-03-07 11:54:07 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-03-07 11:54:07 +0000
commitb31ba1056ee4647f188b997dcb47450bfe1cb25e (patch)
tree88550df5f6b1e740ed63b379c710e2b40675b5a5
parentb479a2d43953ef8e1b498a3f1c0dac3e32269c69 (diff)
complex.c: check type
* complex.c (nucomp_s_canonicalize_internal): determine the type by the internal type, not by a method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62688 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--complex.c10
-rw-r--r--test/ruby/test_complex.rb12
2 files changed, 19 insertions, 3 deletions
diff --git a/complex.c b/complex.c
index b2f206d7ce..6b54d44ba2 100644
--- a/complex.c
+++ b/complex.c
@@ -354,20 +354,24 @@ nucomp_real_check(VALUE num)
inline static VALUE
nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag)
{
+ int complex_r, complex_i;
#ifdef CANONICALIZATION_FOR_MATHN
if (k_exact_zero_p(imag) && canonicalization)
return real;
#endif
- if (f_real_p(real) && f_real_p(imag))
+ complex_r = RB_TYPE_P(real, T_COMPLEX);
+ complex_i = RB_TYPE_P(imag, T_COMPLEX);
+ if (!complex_r && !complex_i) {
return nucomp_s_new_internal(klass, real, imag);
- else if (f_real_p(real)) {
+ }
+ else if (!complex_r) {
get_dat1(imag);
return nucomp_s_new_internal(klass,
f_sub(real, dat->imag),
f_add(ZERO, dat->real));
}
- else if (f_real_p(imag)) {
+ else if (!complex_i) {
get_dat1(real);
return nucomp_s_new_internal(klass,
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index 316e3e21ff..48fb9947a3 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -958,4 +958,16 @@ class Complex_Test < Test::Unit::TestCase
def test_known_bug
end
+ def test_canonicalize_internal
+ obj = Class.new(Numeric) do
+ attr_accessor :real
+ alias real? real
+ end.new
+ obj.real = true
+ c = Complex.rect(obj, 1);
+ obj.real = false
+ c = c.conj
+ assert_equal(obj, c.real)
+ assert_equal(-1, c.imag)
+ end
end