I am doing pathfinding on a 2D grid.
I need to calculate distance as one of my heuristics.
Furthermore I need to return the closest spot if the full path is not found.
Calculating the exact distance to a double precision seems like unnecessary overhead. Is there any fast approximation I can use, which will still be accurate enough to meet my needs? (within rounding accuracy of 1)
By the way, path lengths are typically only around 5-30 nodes, so using a more accurate function at the end wouldn’t be worth it.
In this case you could skip the square root operation in the distance calculation, i.e. compare squared distances using just
dy * dy + dx * dx.This works since a2 < b2 if and only if a < b for two arbitrary distances a and b.
In a 2D grid this would be implemented purely with integers.
If you need non-integer values, I’d probably go with
doubles until that proves to be a bottleneck.