Greater precision in two-point distance problem

Asked

Viewed 103 times

1

I’m trying to solve a minimal tree-generating problem, where I try to find the minimum way to connect all of us. The nodes represent objects with position defined by x, y (integer numbers) in space, so I find the distance between one and the other through the following passage:

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

As I mentioned above, x and y are integers and the end result should be a double with the sum of the path that travels the shortest distance to join us all.

What is happening is that my results are always giving similar values, however, not exact to what the system requires and my answer is not accepted.

Ex:

Program output: 98.00 expected exit: 98.02

How can I improve the accuracy in the final numbers?

1 answer

0

I don’t see why accuracy is incorrect, the only thing wrong seems to me the lack of a "t" in sqrt

I tested the code below and it worked perfectly.

#include <stdio.h>
#include <math.h>

main() {
    double dist;
    int x1 = 10, x2 = 20, y1 = 30, y2= 50;

    dist = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
            printf("%.2lf", dist);
    return 0;
}

The exit is

22.36

You can see the test online here.

  • If not, post your full code so you can analyze it better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.