Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to approximate Euclidean distance on the integer plane, without overflow?

I'm working on a platform that has only integer arithmetic. The application uses geographic information, and I'm representing points by (x, y) coordinates where x and y are distances measured in meters. As an approximation, I want to compute the Euclidean distance between two points. But to do this I have to square distances, and with 32-bit integers, the largest distance I can represent is 32 kilometers. Not good. My needs are more on the order of 1000 kilometers. But I'd like to be able to resolve distances on a scale smaller than 30 meters.

Hence my question: how can I compute Euclidean distance, using only integer arithmetic, without overflow, on distances whose squares don't fit in a single word?

ETA: I would like to be able to compute distances, but I might settle for being able to compare them.

like image 323
Norman Ramsey Avatar asked Feb 18 '16 23:02

Norman Ramsey


People also ask

How do you calculate Euclidean distance manually?

The Euclidean distance formula is used to find the distance between two points on a plane. This formula says the distance between two points (x1 1 , y1 1 ) and (x2 2 , y2 2 ) is d = √[(x2 – x1)2 + (y2 – y1)2].

What is an alternative form of Euclidean distance?

Haversine distance is the distance between two points on a sphere given their longitudes and latitudes. It is very similar to Euclidean distance in that it calculates the shortest line between two points.

How do you calculate Euclidean distance for data?

Euclidean distance is calculated as the square root of the sum of the squared differences between the two vectors.


1 Answers

Perhaps comparing the octagonal distance approximation would be sufficient?

Slightly more up to date is this article on fast approximate distance functions.

like image 182
whybird Avatar answered Oct 24 '22 11:10

whybird