mercredi 29 août 2018

How to handle -nan output in c++

Let's say I have a function that calculates Euclidean distance between a pair of points. point and point_pair are two structs defined as:

struct point {
    int x, y;
}

and

struct point_pair {
    point a, b;
}

The following functions calculates the distance taking a pair of points as an input:

double calc_distance(point_pair pair)
{
  return (sqrt((pair.a.x - pair.b.x) * (pair.a.x - pair.b.x) + (pair.a.y - pair.b.y) * (pair.a.y - pair.b.y)));
}

The function works fine for small point pair values; but for a point pair such as:

651760491 595516649
716636914 955747792

The output is -nan

I'm not sure how to workaround this, is there something else I should be using in place of double?

Here is the entire code: https://pastebin.com/5XEr9bTD

Aucun commentaire:

Enregistrer un commentaire