Complex.rb changes exceptions of Math

Issue #3137 has been updated by Koichi Sasada.

この件,どうしましょうか.

Bug #3137: complex.rb changes exceptions of Math

Author: Yusuke E.
Status: Assigned
Priority: Normal
Assignee: Keiju Ishitsuka
Category: lib
Target version: 1.9.x
ruby -v: ruby 1.9.2dev (2010-04-12 trunk 27317) [i686-linux]

=begin
いしつかさん
遠藤です。

[ruby-core:28204] にて Brian F. が「complex を require すると
Math.atan(nil) で投げられる例外が変わる」という報告をしています。

$ ./ruby -e ‘p Math.atanh(nil)’
-e:1:in atanh': can't convert nil into Float (TypeError) from -e:1:in

$ ./ruby -rcomplex -e ‘p Math.atanh(nil)’
/home/mame/work/ruby-trunk-local/lib/ruby/1.9.1/cmath.rb:196:in
atanh': undefined method real?’ for nil:NilClass (NoMethodError)
from -e:1:in `’

Ruby レベルのライブラリは duck typing のためにむやみに型チェック
すべきでないとはいえ、CMath は組み込みの Math クラスの置き換えを
前提としているので、なるべく Math クラスの挙動を尊重した方がよい
と思いました。

以下のパッチをコミットしてもいいでしょうか。

ついでですが、[ruby-dev:40953] も見てください。

diff --git a/lib/cmath.rb b/lib/cmath.rb
index b23dac2…aa2d9bb 100644
— a/lib/cmath.rb
+++ b/lib/cmath.rb
@@ -27,6 +27,7 @@ module CMath
alias atanh! atanh

def exp(z)
  • z = Float(z)
    if z.real?
    exp!(z)
    else
    @@ -36,9 +37,9 @@ module CMath
    end
    end
  • def log(*args)
  • z, b = args
  • if z.real? and z >= 0 and (b.nil? or b >= 0)
  • def log(z, b = nil)
  • z = Float(z)
  • if z.real? and z >= 0 and (b.nil? or (b = Float(b); b >= 0))
    log!(*args)
    else
    a = Complex(log!(z.abs), z.arg)
    @@ -50,6 +51,7 @@ module CMath
    end
def log2(z)
  • z = Float(z)
    if z.real? and z >= 0
    log2!(z)
    else
    @@ -58,6 +60,7 @@ module CMath
    end
def log10(z)
  • z = Float(z)
    if z.real? and z >= 0
    log10!(z)
    else
    @@ -66,6 +69,7 @@ module CMath
    end
def sqrt(z)
  • z = Float(z)
    if z.real?
    if z < 0
    Complex(0, sqrt!(-z))
    @@ -85,6 +89,7 @@ module CMath
    end
def cbrt(z)
  • z = Float(z)
    if z.real? and z >= 0
    cbrt!(z)
    else
    @@ -93,6 +98,7 @@ module CMath
    end
def sin(z)
  • z = Float(z)
    if z.real?
    sin!(z)
    else
    @@ -102,6 +108,7 @@ module CMath
    end
def cos(z)
  • z = Float(z)
    if z.real?
    cos!(z)
    else
    @@ -111,6 +118,7 @@ module CMath
    end
def tan(z)
  • z = Float(z)
    if z.real?
    tan!(z)
    else
    @@ -119,6 +127,7 @@ module CMath
    end
def sinh(z)
  • z = Float(z)
    if z.real?
    sinh!(z)
    else
    @@ -128,6 +137,7 @@ module CMath
    end
def cosh(z)
  • z = Float(z)
    if z.real?
    cosh!(z)
    else
    @@ -137,6 +147,7 @@ module CMath
    end
def tanh(z)
  • z = Float(z)
    if z.real?
    tanh!(z)
    else
    @@ -145,6 +156,7 @@ module CMath
    end
def asin(z)
  • z = Float(z)
    if z.real? and z >= -1 and z <= 1
    asin!(z)
    else
    @@ -153,6 +165,7 @@ module CMath
    end
def acos(z)
  • z = Float(z)
    if z.real? and z >= -1 and z <= 1
    acos!(z)
    else
    @@ -161,6 +174,7 @@ module CMath
    end
def atan(z)
  • z = Float(z)
    if z.real?
    atan!(z)
    else
    @@ -169,6 +183,7 @@ module CMath
    end
def atan2(y,x)
  • x, y = Float(x), Float(y)
    if y.real? and x.real?
    atan2!(y,x)
    else
    @@ -177,6 +192,7 @@ module CMath
    end
def asinh(z)
  • z = Float(z)
    if z.real?
    asinh!(z)
    else
    @@ -185,6 +201,7 @@ module CMath
    end
def acosh(z)
  • z = Float(z)
    if z.real? and z >= 1
    acosh!(z)
    else
    @@ -193,6 +210,7 @@ module CMath
    end
def atanh(z)
  • z = Float(z)
    if z.real? and z >= -1 and z <= 1
    atanh!(z)
    else


Yusuke E. [email protected]
=end

$B$1$$$8$e!w$$$7$D$+$G$9(B.

$B$3$l$i$N%Q%C%A$@$H(B,
$BJ#AG?t$,EO$5$l$?$H$-@5$7$$F0:n$r$7$J$/$J$j$^$9(B.

$B$?$@(B, NoMethodError$B$b%$%^%$%A$G$9$h$M$'(B.

$B$3$l$C$F(B,
$BNc30$N%/%i%9$,0lCW$7$F$$$l$P%a%C%;!<%8$NFbMF$,0[$J$C$F$$$F$b(B
$BNI$$$o$1$G$9$h$M(B?

$B$=$l$G$"$l$P(B, CMath$B$N$J$+$G(B, Object#real? $B$rDj5A$7$F(B,
TypeError$B$r=P$9(B
$B$h$&$K$7$h$&$H;W$$$^$9(B.

In [ruby-dev:43716] the message: “[ruby-dev:43716] [Ruby 1.9 - Bug
#3137] complex.rb changes exceptions of Math”, on Jun/11 14:59(JST)
Koichi Sasada writes:

Priority: Normal
[ruby-core:28204] $B$K$F(B Brian F. $B$,!V(Bcomplex $B$r(B require $B$9$k$H(B

    exp!(z)
  • if z.real? and z >= 0 and (b.nil? or (b = Float(b); b >= 0))
    else

    if z.real? and z >= 0
    @@ -102,6 +108,7 @@ module CMath
    def tan(z)
    sinh!(z)
    end

  • z = Float(z)
    else

    if z.real?
    @@ -193,6 +210,7 @@ module CMath
    =end


http://redmine.ruby-lang.org

__
---------------------------------------------------->> $B@PDM(B $B7=<y(B
<<—
---------------------------------->> e-mail: [email protected] <<—

$B$=$l$G$"$l$P(B, CMath$B$N$J$+$G(B, Object#real? $B$rDj5A$7$F(B,
TypeError$B$r=P$9(B
$B$h$&$K$7$h$&$H;W$$$^$9(B.

$BC1=c$K5?Ld$G$9$,!"$3$NJ}K!$ONI$$$N$G$7$g$&$+(B?

$B8DJL$K(B Numeric
$B$G$"$k$+3NG’$9$k$+!"$I$&$7$F$b$3$N$d$jJ}$r$9$k$N$G$"$l$P!"(B
complex.rb $B$G$d$k$H$+!#(Bcomplex.rb $B$r$D$+$o$J$1$l$P!"(BMath
$B$OCV$-49$($i$l(B
$B$^$;$s$+$i!#(B

$B$1$$$8$e!w$$$7$D$+$G$9(B.

In [ruby-dev:43790] the message: “[ruby-dev:43790] Re: [Ruby 1.9 - Bug
#3137] complex.rb changes exceptions of Math”, on Jun/14 00:25(JST)
Tadayoshi F. writes:

$B$=$l$G$"$l$P(B, CMath$B$N$J$+$G(B, Object#real? $B$rDj5A$7$F(B,
TypeError$B$r=P$9(B

$B$h$&$K$7$h$&$H;W$$$^$9(B.

$BC1=c$K5?Ld$G$9$,!"$3$NJ}K!$ONI$$$N$G$7$g$&$+(B?

$B%0%m!<%P%k$J%a%=%C%I$rDj5A$7(B, $B$+$DL>A0$,0-$$$C$F$3$H$G$9(B?

$B8DJL$K(B Numeric $B$G$"$k$+3NG’$9$k$+!"$I$&$7$F$b$3$N$d$jJ}$r$9$k$N$G$"$l$P!"(B
complex.rb $B$G$d$k$H$+!#(Bcomplex.rb $B$r$D$+$o$J$1$l$P!"(BMath
$B$OCV$-49$($i$l(B
$B$^$;$s$+$i!#(B

$B8e<T$O(B, Math$B$H(BCMath$B$G0c$&Nc30$K$J$C$F$7$^$$$^$9$7(B,
$BNc30$N<oN`$,$"$^$j(B
$B$U$5$o$7$/$J$$$N$b;v<B$J$N$G(B,
$B:NMQ$9$k$J$iA0<T$K$J$k$H;W$$$^$9$,(B.

__
---------------------------------------------------->> $B@PDM(B $B7=<y(B
<<—
---------------------------------->> e-mail: [email protected] <<—

$B%0%m!<%P%k$J%a%=%C%I$rDj5A$7(B, $B$+$DL>A0$,0-$$$C$F$3$H$G$9(B?

$B26$b(B Object $B$H$+(B Numeric
$B$K$4$C$=$j%a%=%C%I$r3NJ]$7$F$*$3$&$+$J$"!"B>=j(B
$B$G<h$i$l$kA0$K!#(B