-2

I need to write a formula to determine the distance between two points in a plane in C++. ### Here is the formula: enter image description here

I wrote such a code for this, but where is my mistake and how should I write the code correctly?:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double x1, x2, y1, y2, distance;
    distance = sqrt((pow(x2-x1), 2) + pow((y2-y1), 2));
    return 0;
}
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 6
    The calculation seems correct to me. Though, I do notice you're neither getting input (setting any variable), nor are you outputting anything. Also, in C++, use `` instead of `` – ChrisMM Feb 01 '22 at 12:13
  • 1
    Your problem is that all of your variables are not initialized. – sagi Feb 01 '22 at 12:13
  • What is your intention here? As it is, your code has Undefined Behaviour (you use uninitialized variables) and what's worse it actually does nothing (no input, no output). Did you mean to read the data and print it? – freakish Feb 01 '22 at 12:18
  • 2
    Also you have a typo, use: `sqrt(pow((x2-x1), 2) + pow((y2-y1), 2))` – sagi Feb 01 '22 at 12:19
  • 1
    @sagi: Huh? -a * -a is the same as a * a. – Bathsheba Feb 01 '22 at 12:26
  • That's a comment to OP code, not your answer.. @Bathsheba – sagi Feb 01 '22 at 12:27
  • @sagi: Indeed, but `pow((y2-y1), 2)` is the same as `pow((y1-y2), 2)`. – Bathsheba Feb 01 '22 at 12:35
  • I haven't changed OP logic of `pow`, simply fixed a typo with the parenthesis. Look closer @Bathsheba – sagi Feb 01 '22 at 12:37
  • @sagi: Oh yes indeed you have. Drinks on me if you're ever in London. – Bathsheba Feb 01 '22 at 12:39
  • 1
    The code does the calculation correctly, but doesn't initialise any variables, read any variable, or output any results. That makes it hard to justify a claim of whether it works or not. Personally, I'd eliminate use of `pow()`. For example (assuming `x1`, `x2`, `y1`, and `y2` have useful values) `double dx = x2-x1, dy = y2-y1; distance = sqrt(dx*dx + dy*dy);` (and then output `distance`). – Peter Feb 01 '22 at 13:01

3 Answers3

1

Under IEEE754 (a floating point standard ubiquitious on desktop personal computers), std::sqrt is required to return the best representable result possible, but std::pow is not.

So

distance = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

is a better way. Make sure that all the domain variables are initialised prior to the evaluation else the behaviour of the program is undefined.

In order that your program has observable output, write

std::cout << distance;

having supplied appropriate values for x1, x2, y1, and y2.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    I think it's even better is to use `hypot(x2 - x1, y2 - y1)`. This has the potential of being faster and more accurate than using `sqrt` and two multiplications. – Nadav Har'El Feb 01 '22 at 12:43
  • @NadavHar'El Well, to my understanding, [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot) was designed to be accurate, rather than faster. – Bob__ Feb 01 '22 at 12:48
  • 1
    @Bob__ on second thought, I think you're right. I wrote a separate answer below describing all these versions and their advantages and disavantages. – Nadav Har'El Feb 01 '22 at 13:02
1

The specific example in your question has a several serious errors - among other things you have a syntax error (a parentheses at the wrong place causing the first pow to only have one parameter) and you're using variables that you never initialized - and then not even printing or returning the result...

With a slight correction, the formula you used is correct:

sqrt(pow((x2-x1), 2) + pow((y2-y1), 2))

As Bathsheba noted, if you want to square a number a faster and more accurate alternative to calling pow() is just to do multiplication:

sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

Another alternative is to replace the sqrt and two multiplications by one math function, hypot, designed to do exactly what you need:

hypot(x2-x1, y2-y1)

The jury is still out which of the above two options is better. hypot has the potential of being more accurate and also be able to survive overflows (when the distance squared overflows the floating point limits but the distance itself doesn't), but some report that it is slower than the sqrt-and-multiplication version (see When to use `std::hypot(x,y)` over `std::sqrt(x*x + y*y)`).

Nadav Har'El
  • 11,785
  • 1
  • 24
  • 45
  • 1
    There is a third solution that I like to use: represent points by `std::complex` and use `std::abs(z1 - z2)`. – Damien Feb 01 '22 at 15:24
0

I tested this. Below code is working fine

#include <iostream>

#include <math.h>

using namespace std;

double calculateDistance(double x1, double x2, double y1, double y2) {
  return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
}

int main() {
  double x1 = 5, x2 = 3, y1 = 5, y2 = 4;
  double distance = calculateDistance(x1, x2, y1, y2);
  cout << distance;
  return 0;
}
Ali Akbar
  • 21
  • 1
  • 4
  • Prefer `cmath` to `math.h` as the latter is deprecated in later C++ standards. – Bathsheba Feb 01 '22 at 12:25
  • the question is "Where is my mistake?" you should answer that – 463035818_is_not_an_ai Feb 01 '22 at 13:07
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 13:09