Complex sqrt not working?

I don’t understand why the sqrt function doesn’t seem to work for
Complex arguments. “Programming Ruby” page 443 says that Math functions
including sin, cos and sqrt are extended to support a Complex argument.
When I try it the sin and cos functions work but sqrt does not.

Is this a bug in Ruby 1.6.8 or am I doing something wrong?

Thanks in advance.

bash-2.05b$ cat ztest.rb
require “complex”

z=Complex(1.0,1.0)
p Math.sin(z)
p Math.cos(z)
p z**0.5
p Math.sqrt(z)

bash-2.05b$ ruby -w ztest.rb
Complex(1.298457581, 0.6349639148)
Complex(0.8337300251, -0.9888977058)
Complex(1.098684113, 0.4550898606)
/USR/LOCAL/lib/ruby/1.6/complex.rb:389:in sqrt': undefined methodRational’ for Math:Module (NameError)
from ztest.rb:7
bash-2.05b$
bash-2.05b$ ruby --version
ruby 1.6.8 (2002-12-24) [i686-cygwin]
[1]+ Done emacs ztest.txt
bash-2.05b$

···


Alan Linton

Is this a bug in Ruby 1.6.8 or am I doing something wrong?

probably a bug in complex.rb, it must test if Rational is defined (like in
**) before trying to call it

If you really need sqrt(), make an additionnal require 'rational'

Guy Decoux

Hi,

···

In message “Re: Complex sqrt not working?” on 03/03/23, ts decoux@moulon.inra.fr writes:

Is this a bug in Ruby 1.6.8 or am I doing something wrong?

probably a bug in complex.rb, it must test if Rational is defined (like in
**) before trying to call it

If you really need sqrt(), make an additionnal require ‘rational’

It will be fixed. Thank you.

						matz.