Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Square Root

Reply
Thread Tools

Square Root

 
 
Angus Hammond
Guest
Posts: n/a
 
      05-29-2010
Is there a way to get an accurate square root?
This means that a negative number should return the appropriate complex
number. I have been having some trouble with this in a program that
solves quadratic equations recently. Ideally but not necessarily it
should be able to get the square root of complex numbers as well.
NB Neither -2**0.5 or Math::sqrt(-2) return appropriate answers. (-2 and
an error respectively)
Thanks in advance.

 
Reply With Quote
 
 
 
 
Jean-Julien Fleck
Guest
Posts: n/a
 
      05-29-2010
Hello Angus,

2010/5/29 Angus Hammond <(E-Mail Removed)>:
> Is there a way to get an accurate square root?
> This means that a negative number should return the appropriate complex
> number. I have been having some trouble with this in a program that solve=

s
> quadratic equations recently. Ideally but not necessarily it should be ab=

le
> to get the square root of complex numbers as well.
> NB Neither -2**0.5 or Math::sqrt(-2) return appropriate answers. (-2 and =

an
> error respectively)


require 'complex'
include Math
c =3D Complex(-2,0)
sqrt(c)

>> require 'complex'

=3D> true
>> include Math

=3D> Object
>> c =3D Complex(-2,0)

=3D> Complex(-2, 0)
>> sqrt(c)

=3D> Complex(0.0, 1.4142135623731)

Cheers,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber

 
Reply With Quote
 
 
 
 
Angus Hammond
Guest
Posts: n/a
 
      05-29-2010
Thanks I had tried require complex and Math::sqrt but only separately.
Thanks again.
Angus

On 29/05/10 21:03, Jean-Julien Fleck wrote:
> Hello Angus,
>
> 2010/5/29 Angus Hammond<(E-Mail Removed)>:
>
>> Is there a way to get an accurate square root?
>> This means that a negative number should return the appropriate complex
>> number. I have been having some trouble with this in a program that solves
>> quadratic equations recently. Ideally but not necessarily it should be able
>> to get the square root of complex numbers as well.
>> NB Neither -2**0.5 or Math::sqrt(-2) return appropriate answers. (-2 and an
>> error respectively)
>>

> require 'complex'
> include Math
> c = Complex(-2,0)
> sqrt(c)
>
>
>>> require 'complex'
>>>

> => true
>
>>> include Math
>>>

> => Object
>
>>> c = Complex(-2,0)
>>>

> => Complex(-2, 0)
>
>>> sqrt(c)
>>>

> => Complex(0.0, 1.4142135623731)
>
> Cheers,
>
>



 
Reply With Quote
 
jzakiya
Guest
Posts: n/a
 
      06-02-2010
On May 29, 3:33*pm, Angus Hammond <(E-Mail Removed)> wrote:
> Thanks I had tried require complex and Math::sqrt but only separately.
> Thanks again.
> Angus
>
> On 29/05/10 21:03, Jean-Julien Fleck wrote:
>
> > Hello Angus,

>
> > 2010/5/29 Angus Hammond<(E-Mail Removed)>:

>
> >> Is there a way to get an accurate square root?
> >> This means that a negative number should return the appropriate complex
> >> number. I have been having some trouble with this in a program that solves
> >> quadratic equations recently. Ideally but not necessarily it should beable
> >> to get the square root of complex numbers as well.
> >> NB Neither -2**0.5 or Math::sqrt(-2) return appropriate answers. (-2 and an
> >> error respectively)

>
> > require 'complex'
> > include Math
> > c = Complex(-2,0)
> > sqrt(c)

>
> >>> require 'complex'

>
> > => *true

>
> >>> include Math

>
> > => *Object

>
> >>> c = Complex(-2,0)

>
> > => *Complex(-2, 0)

>
> >>> sqrt(c)

>
> > => *Complex(0.0, 1.4142135623731)

>
> > Cheers,

>
>


FYI. For more extensive (and easy) way to add the ability
to find the nth roots of all numerical types check out
my Roots module here:

http://gist.github.com/422636

Jabari
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fix point square root Christian VHDL 5 03-18-2010 02:14 PM
Looking for Square Root Sign rfdjr1@optonline.net Computer Support 10 04-22-2009 02:03 PM
'big square root' for BigDecimal Jeremy Watts Java 4 05-26-2005 09:01 PM
SRT DIvision, Square root and reciprocal square root alghazo@siu.edu VHDL 0 05-27-2004 06:23 AM
Square Root of floating point number Luca VHDL 1 04-29-2004 02:51 PM



Advertisments