Velocity Reviews > C++ > Distance between 2points (squareroot and power of help needed)

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

gbvk@hotmail.co.uk
Guest
Posts: n/a

 03-12-2008
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

Christopher
Guest
Posts: n/a

 03-12-2008
On Mar 12, 10:17 am, (E-Mail Removed) 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

and pow functions.

gbvk@hotmail.co.uk
Guest
Posts: n/a

 03-12-2008
On 12 Mar, 15:26, Christopher <(E-Mail Removed)> wrote:
> On Mar 12, 10:17 am, (E-Mail Removed) 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)));"

Dave Steffen
Guest
Posts: n/a

 03-12-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> On 12 Mar, 15:26, Christopher <(E-Mail Removed)> wrote:
>> On Mar 12, 10:17 am, (E-Mail Removed) 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/> )

Marcel Müller
Guest
Posts: n/a

 03-12-2008
Hi,

(E-Mail Removed) 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);

Marcel

John Brawley
Guest
Posts: n/a

 03-13-2008

"Marcel Müller" <(E-Mail Removed)> wrote in message
news:47d80fb8\$0\$6726\$(E-Mail Removed)-online.net...
> Hi,
>
> (E-Mail Removed) 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);

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
(E-Mail Removed)
Web: http://tetrahedraverse.com