Square Root

Discussion in 'Ruby' started by Angus Hammond, May 29, 2010.

  1. 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.
     
    Angus Hammond, May 29, 2010
    #1
    1. Advertising

  2. Hello Angus,

    2010/5/29 Angus Hammond <>:
    > 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
     
    Jean-Julien Fleck, May 29, 2010
    #2
    1. Advertising

  3. 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<>:
    >
    >> 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,
    >
    >
     
    Angus Hammond, May 29, 2010
    #3
  4. Angus Hammond

    jzakiya Guest

    On May 29, 3:33 pm, Angus Hammond <> 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<>:

    >
    > >> 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
     
    jzakiya, Jun 2, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luca
    Replies:
    1
    Views:
    1,053
    salman sheikh
    Apr 29, 2004
  2. Replies:
    0
    Views:
    1,255
  3. Christian

    Fix point square root

    Christian, Apr 25, 2005, in forum: VHDL
    Replies:
    5
    Views:
    6,693
    jeppe
    Mar 18, 2010
  4. Jeremy Watts

    'big square root' for BigDecimal

    Jeremy Watts, May 26, 2005, in forum: Java
    Replies:
    4
    Views:
    3,589
    Boudewijn Dijkstra
    May 26, 2005
  5. David Harmon
    Replies:
    2
    Views:
    2,925
    cplusplus
    Jun 14, 2006
Loading...

Share This Page