From: keiju@... (=?ISO-2022-JP?B?GyRCQFBETTc9PHkbKEI=?=) Date: 2003-07-22T21:40:02+09:00 Subject: [ruby-dev:20787] Re: complex.rb けいじゅ@いしつかです. In [ruby-dev:20782] the message: "[ruby-dev:20782] Re: complex.rb", on Jul/22 17:30(JST) Yukihiro Matsumoto writes: >まつもと ゆきひろです >|complex.rb についての修正案を[ruby-math:00543]で提案しましたが、その後 >|合意していただいた点がまだ反映されていません。他に若干の修正点を加えて >|パッチを作りましたので、お取り込み願います。このパッチの修正点は、 >| >|・Complex#** のバグfix >|・image に imag という別名をつける >|・Math.sqrt の再定義の修正 (正木さん案[ruby-math:00553]) >|・Complex#abs, Complex#arg を少し効率よくする > >このパッチは取り込みました。 どもです. なぜ, 修正されていなかった言うと... 次に続く. >|あと、ここには含まれていませんが、complex.rb 中の Fixnum#** の再定義は、 >|Complex とは関係ないだけでなく、(-2)**2 => Complex(4, 0) という副作用 >|があるので、削除して欲しいです。(というのも合意してもらったはず) > >石塚さん、作業しますか。それとも私がやりましょうか? こっちの件はそれほど単純じゃないなというとことで, 全体が止まっていました. Complec以外のNumericでは, 以下の感じになります: # i ** j => int (j>=0) same original ** # i ** j => float (j<0) same original ** # i ** f/r => float (i>=0) same original ** # i ** f/r => complex (i<0) same complex#** # i ** z => complex same complex#** # i ** else => else to coerce ここで, i, j: Integer f: Float r: Rational z: Complex です. 指数部分が4番目以降では, 削除すれば良いわけではないと. んで, 以下のような感じで考えていたらしいです: def **(other) if other.real_number? if other.kind_of?(Integer) or self >= 0 or other.kind_of?(Float) && other.ceil == other or (defined?(Rational) && other.kind_of?(Rational) && other.denominator == 1) or (defined?(BigDecimal) && other.kind_of?(BigDecimal) && other.ceil == other) self.power!(other) else Complex.new(self)**other end else x, y = other.coerce(self) x**y end end # でも, こんな修正が必要になるのも ** が coerceメカニズムを導入してい # ないからではないかと... __ ---------------------------------------------------->> 石塚 圭樹 <<--- ---------------------------------->> e-mail: keiju@ishitsuka.com <<---