Distance between 2points (squareroot and power of help needed)

Discussion in 'C++' started by gbvk@hotmail.co.uk, Mar 12, 2008.

  1. Guest

    Hello

    i'm currently writing a program in c++ and have got stuck trying to
    find the squareroot and how to raise powers of a problem.

    Dist= square root of ( ((Ax-Bx) to the power 2 )+ ((Ay-By)to the pwer
    2) )

    any help? i'm very new to the coding world :/#

    G
     
    , Mar 12, 2008
    #1
    1. Advertising

  2. Christopher Guest

    On Mar 12, 10:17 am, wrote:
    > Hello
    >
    > i'm currently writing a program in c++ and have got stuck trying to
    > find the squareroot and how to raise powers of a problem.
    >
    > Dist= square root of ( ((Ax-Bx) to the power 2 )+ ((Ay-By)to the pwer
    > 2) )
    >
    > any help? i'm very new to the coding world :/#
    >
    > G


    Consult your C++ reference for the <cmath> header. It contains sqrt
    and pow functions.
     
    Christopher, Mar 12, 2008
    #2
    1. Advertising

  3. Guest

    On 12 Mar, 15:26, Christopher <> wrote:
    > On Mar 12, 10:17 am, wrote:
    >
    > > Hello

    >
    > > i'm currently writing a program in c++ and have got stuck trying to
    > > find the squareroot and how to raise powers of a problem.

    >
    > > Dist= square root of ( ((Ax-Bx) to the power 2 )+ ((Ay-By)to the pwer
    > > 2) )

    >
    > > any help? i'm very new to the coding world :/#

    >
    > > G

    >
    > Consult your C++ reference for the <cmath> header. It contains sqrt
    > and pow functions.


    Thank you :)

    "Dist=sqrt((pow(Dx,2))+(pow(Dy,2)));"
     
    , Mar 12, 2008
    #3
  4. Dave Steffen Guest

    writes:

    > On 12 Mar, 15:26, Christopher <> wrote:
    >> On Mar 12, 10:17 am, wrote:
    >>
    >> > Hello

    >>
    >> > i'm currently writing a program in c++ and have got stuck trying to
    >> > find the squareroot and how to raise powers of a problem.

    [...]
    >> Consult your C++ reference for the <cmath> header. It contains sqrt
    >> and pow functions.

    >
    > Thank you :)
    >
    > "Dist=sqrt((pow(Dx,2))+(pow(Dy,2)));"


    Please note that this not necessarily the best thing to do; this can
    have bad numerical problems under some circumstances (e.g. Dx or Dy
    is larger than the square root of the largest representable number,
    or one being much larger than the other).

    Consider using one of the hypot functions instead; and/or see
    Numerical Recipes (I think 'pythag' is their version) for more
    discussion.

    --
    Dave Steffen - Software Engineer 4
    Numerica Corporation (www.numerica.us <http://www.numerica.us/> )
     
    Dave Steffen, Mar 12, 2008
    #4
  5. Hi,

    schrieb:
    > "Dist=sqrt((pow(Dx,2))+(pow(Dy,2)));"


    pow() is very slow because it operates with log() and exp().
    Use
    Dist=sqrt(Dx*Dx + Dy*Dy);
    instead.


    Marcel
     
    Marcel Müller, Mar 12, 2008
    #5
  6. John Brawley Guest

    "Marcel Müller" <> wrote in message
    news:47d80fb8$0$6726$-online.net...
    > Hi,
    >
    > schrieb:
    > > "Dist=sqrt((pow(Dx,2))+(pow(Dy,2)));"

    >
    > pow() is very slow because it operates with log() and exp().
    > Use
    > Dist=sqrt(Dx*Dx + Dy*Dy);
    > instead.


    I agree. It seems basic 'written out' math is fastest wherever possible.
    I use exactly what the OP is doing, several places in my program, and I
    write it like:
    pDist=sqrt(((p1x-p2x)*(p1x-p2x))+((p1y-p2y)*(p1y-p2y))+((p1z-p2z)*(p1z-p2z))
    );
    (for 2 points in 3D space away from 0,0,0) and like:
    pDist=sqrt((a*a)+(b*b)+(c*c))
    for a point whose other 'point' is 0,0,0 .
    (This is the same value as r, in spherical coordinates r, phi, theta.)


    --
    Peace
    JB

    Web: http://tetrahedraverse.com
     
    John Brawley, Mar 13, 2008
    #6
    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. Replies:
    3
    Views:
    2,214
  2. Replies:
    8
    Views:
    380
    Mark Dickinson
    Apr 17, 2008
  3. jazzy
    Replies:
    0
    Views:
    424
    jazzy
    Jan 4, 2012
  4. Patrick Gundlach
    Replies:
    2
    Views:
    111
    Patrick Gundlach
    Jan 23, 2007
  5. Gilles Lenfant
    Replies:
    1
    Views:
    128
    Martin Honnen
    Aug 27, 2003
Loading...

Share This Page