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

2. ChristopherGuest

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

and pow functions.

Christopher, Mar 12, 2008

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
4. Dave SteffenGuest

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
5. Marcel MüllerGuest

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

Marcel

Marcel Müller, Mar 12, 2008
6. John BrawleyGuest

"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);

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