71008

Java: Trigonometry and double inaccuracy causing NaN

Question:

I have a distance formula using latitude and longitude:

distance = EARTH_MILES_RADIUS * Math.acos(Math.sin(lat1 / RADIAN_CONV) * Math.sin(lat2 / RADIAN_CONV) + Math.cos(lat1 / RADIAN_CONV) * Math.cos(lat2 / RADIAN_CONV) * Math.cos((lng2 - lng1) / RADIAN_CONV));

lat1,lng1,lat2,lng2 are double primitives. They come to me as double primitives and there is nothing I can do about it.

The problem is that when I have a pair of longitude or latitudes that are the same the formula sometimes returns NaN. I believe this is because I am taking the arc cosine of a number very slightly greater than 1, when in fact it should be exactly 1. I would probably have problems if the points were antipodal as well, where they might be slightly less than -1.

How can I best fix this problem?

Answer1:

If you are indeed calculating great circle distance as I think you are, you should use the Vincenty formula instead of what you have.

<a href="http://en.wikipedia.org/wiki/Great-circle_distance" rel="nofollow">http://en.wikipedia.org/wiki/Great-circle_distance</a>

Answer2:

Check for two very close (ideally equal) values in your code. For example:

boolean doubleApproxEqual(double a, double b) { double PRECISION = 0.000001; if (Math.abs(a-b) < PRECISION) //not sure what the name of the function is //cannot be bothered to check return true; return false; }

if you get a True, do cos(1.0) or whatever

Answer3:

Simply checking for equality was enough to fix my problem:

if (lat1 == lat2 && lng1 == lng2) ...

Recommend

  • Best place to coerce/convert to the right type in Python
  • How to produce X values of a stretched graph?
  • Google Maps Coordinates Incorrect [duplicate]
  • Shortest path using php [closed]
  • Transform XML into a data frame
  • Nested for loops in R using foreach function and doParallel library
  • MySQL Function to calculate distance between two latitudes and longitudes [closed]
  • Dividing the length by sin(x) always results in a negative answer
  • D3.JS - how do I add gridlines to my pie chart
  • Loop over JSON data to create d3 pie charts
  • disabling ARC for .h files iphonesdk
  • Why can't I draw a complete circle with arc() in Three.js?
  • Does Angular assign itself to `window.angular` globally, when loaded as CommonJS module?
  • Where to get the .java files of a netbeans project?
  • Deploying pre-encrypted configuration files to a production environment
  • Each Radiobutton for each form or 1 Form for all radiobuttons?
  • What does a hyphen at end of a term mean
  • OpenCV Python: Draw minAreaRect ( RotatedRect not implemented)
  • d3.js: why is d3.geo.path() giving NaN?
  • How do I change the kernel/python version for iPython?
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Classic ASP URL Rewriting
  • Deleting empty Series out of Graph (with VBA)
  • Efficient User-Agent Regex to find Safari in Python
  • Use sed with regex and (
  • How to draw a line dynamically in android [duplicate]
  • Android Studio Can't Find tools.jar
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Is there a javascript serializer for JSON.Net?
  • How reduce the height of an mschart by breaking up the y-axis
  • Java applet as stand-alone Windows application?
  • Fill an image in a square container while keeping aspect ratio
  • Where to put my custom functions in Wordpress?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Numpy divide by zero. Why?
  • Traverse Array and Display in markup
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs