Bug #5020

Rational cannot coerce into Complex with imag.

Added by Usaku NAKAMURA almost 3 years ago. Updated over 2 years ago.

[ruby-dev:44088]
Status:Closed
Priority:Normal
Assignee:Kenta Murata
Category:core
Target version:1.9.3
ruby -v:ruby 1.9.4dev (2011-07-11 trunk 32518) [x64-mswin64_100] Backport:

Description

とある実験をしていて気付いたのですが、虚部ありのComplexを引数としてRational#coerceを呼ぶとTypeErrorが発生します。

Rational(1,2).coerce(Complex(1,1)) #=> TypeError

虚部がなければ問題ありません。ちなみに結果はRationalになります。

Rational(1,2).coerce(Complex(1,0)) #=> [(1/1), (1/2)]

一方で、レシーバと引数をひっくり返すと、Complex側に虚部があってもなくてもcoerceできます。ちなみに結果はComplexになります。

Complex(1,0).coerce(Rational(1,2)) #=> [((1/2)+0i), (1+0i)]
Complex(1,1).coerce(Rational(1,2)) #=> [((1/2)+0i), (1+1i)]

Rational#coerce(aComplex)でaComplexに虚部がない時にRationalな結果を返すことについては意見はないのですが、虚部があるときにTypeErrorになるのはバグだと思います。
いかがでしょうか?

Index: rational.c

--- rational.c (revision 32520)
+++ rational.c (working copy)
@@ -1108,6 +1108,8 @@ nuratcoerce(VALUE self, VALUE other)
if (k
exactzerop(RCOMPLEX(other)->imag))
return rbassocnew(frationalnewbang1
(CLASS
OF(self), RCOMPLEX(other)->real), self);
+ else
+ return rbassocnew(other, rb_Complex(self, INT2FIX(0)));
}

 rb_raise(rb_eTypeError, "%s can't be coerced into %s",

Associated revisions

Revision 32994
Added by Kenta Murata over 2 years ago

  • rational.c (nurat_coerce): Rational#coerce should converts itself into Complex if the argument is a Complex with non-zero imaginary part. [Bug #5020]
  • test/ruby/testrational.rb (testcoerce): test for the above change.

History

#1 Updated by Motohiro KOSAKI almost 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Kenta Murata

バグという意見に同意ですが、regressionじゃないようなので誰もハンドルしないなら
来週ぐらいにタイムアウト発動させる予定です

#2 Updated by Kenta Murata over 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r32994.
Usaku, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • rational.c (nurat_coerce): Rational#coerce should converts itself into Complex if the argument is a Complex with non-zero imaginary part. [Bug #5020]
  • test/ruby/testrational.rb (testcoerce): test for the above change.

Also available in: Atom PDF