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

G

gbvk

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
 
C

Christopher

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.
 
D

Dave Steffen

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.
 
M

Marcel Müller

Hi,

"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
 
J

John Brawley

Marcel Müller said:
Hi,



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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top