Pregunta

I'm running into a problem where my square of X is always becoming infinite leading to the resulting distance also being infinite, however I can't see anything wrong with my own maths:

// Claculate distance

xSqr = (x1 - x2) * (x1 - x2);
ySqr = (y1 - y2) * (y1 - y2);
zSqr = (z1 - z2) * (z1 - z2);

double mySqr = xSqr + ySqr + zSqr;

double myDistance = sqrt(mySqr);

When I run my program I get user input for each of the co-ordinates and then display the distance after I have run the calulation.

¿Fue útil?

Solución

If your inputs are single-precision float, then you should be fine if you force double-precision arithmetic:

xSqr = double(x1 - x2) * (x1 - x2);
//     ^^^^^^

If the inputs are already double-precision, and you don't have a larger floating-point type available, then you'll need to rearrange the Euclidean distance calculation to avoid overflow:

r = sqrt(x^2 + y^2 + z^2)
  = abs(x) * sqrt(1 + (y/x)^2 + (z/x)^2)

where x is the largest of the three coordinate distances.

In code, that might look something like:

double d[] = {abs(x1-x2), abs(y1-y2), abs(z1-z2)};
if (d[0] < d[1]) swap(d[0],d[1]);
if (d[0] < d[2]) swap(d[0],d[2]);
double distance = d[0] * sqrt(1.0 + d[1]/d[0] + d[2]/d[0]);

or alternatively, use hypot, which uses similar techniques to avoid overflow:

double distance = hypot(hypot(x1-x2,y1-y2),z1-z2);

although this may not be available in pre-2011 C++ libraries.

Otros consejos

Try this:

long double myDistance=sqrt(pow(x1-x2,2.0)+pow(y1-y2,2.0)+pow(z1-z2,2.0));

I figured out what was going on, I had copied and pasted the code for setting x1, y1 and z1 and forgot to change it to x2 y2 and z2, It's always the silliest of things with me :P thanks for the help anyway guys

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top