{"slug": "fast-great-circle-distance-calculation-in-cuda-c", "title": "Fast Great-Circle Distance Calculation in CUDA C++", "summary": "NVIDIA developer Norbert Juffa contributed a CUDA C++ implementation of the Haversine formula for fast great-circle distance calculations, leveraging the sinpi() and cospi() functions for improved performance in geospatial applications.", "body_md": "This post demonstrates the practical utility of CUDA’s `sinpi()`\n\nand `cospi()`\n\nfunctions in the context of distance calculations on earth. With the advent of location-aware and geospatial applications and geographical information systems (GIS), these distance computations have become commonplace.\n\nWikipedia [defines a great circle](https://en.wikipedia.org/wiki/Great_circle) as\n\nA great circle, also known as an orthodrome or Riemannian circle, of a sphere is the intersection of the sphere and a plane which passes through the center point of the sphere.\n\nFor almost any pair of points on the surface of a sphere, the [shortest](https://developer.nvidia.com/discover/shortest-path-problem) (surface) distance between these points is the path along the great circle between them. If you have ever flown from Europe to the west coast of North America and wondered why you passed over Greenland, your flight most likely followed a great circle path in order to conserve fuel.\n\nFollowing is the code for a C function, `haversine()`\n\n, which computes the great-circle distance of two points on earth (or another sphere), using the Haversine formula. People differ on their philosophy as to the “correct” radius when assuming a spherical earth (given that earth is not a sphere; [Wikipedia provides some guidance on this matter](https://en.wikipedia.org/wiki/Great-circle_distance#Radius_for_spherical_Earth)). Therefore, the earth’s radius is an input to the function, which also allows trivial switching between kilometers or miles as units. The accuracy of the formula when computed in single-precision should generally be fully adequate for distance computations within a continent.\n\n```\n/* This function computes the great-circle distance of two points on earth\nusing the Haversine formula, assuming spherical shape of the planet. A\nwell-known numerical issue with the formula is reduced accuracy in the\ncase of near antipodal points.\n\nlat1, lon1: latitude and longitude of first point, in degrees [-90, +90]\nlat2, lon2: latitude and longitude of second point, in degrees [-180, +180]\nradius: radius of the earth in user-defined units, e.g. 6378.2 km or\n3963.2 miles\n\nreturns: distance of the two points, in the same units as radius\n\nReference: http://en.wikipedia.org/wiki/Great-circle_distance\n*/\n__device__ float haversine (float lat1, float lon1, \n                            float lat2, float lon2, float radius)\n{\n  float dlat, dlon, c1, c2, d1, d2, a, c, t;\n\n  c1 = cospif (lat1 / 180.0f);\n  c2 = cospif (lat2 / 180.0f);\n  dlat = lat2 - lat1;\n  dlon = lon2 - lon1;\n  d1 = sinpif (dlat / 360.0f);\n  d2 = sinpif (dlon / 360.0f);\n  t = d2 * d2 * c1 * c2;\n  a = d1 * d1 + t;\n  c = 2.0f * asinf (fminf (1.0f, sqrtf (a)));\n  return radius * c;\n}\n```\n\nThanks to Norbert Juffa for contributing this code.", "url": "https://wpnews.pro/news/fast-great-circle-distance-calculation-in-cuda-c", "canonical_source": "https://developer.nvidia.com/blog/fast-great-circle-distance-calculation-cuda-c/", "published_at": "2026-06-20 14:49:04+00:00", "updated_at": "2026-06-20 15:07:19.896288+00:00", "lang": "en", "topics": ["developer-tools", "ai-infrastructure"], "entities": ["NVIDIA", "Norbert Juffa", "CUDA", "Haversine formula"], "alternates": {"html": "https://wpnews.pro/news/fast-great-circle-distance-calculation-in-cuda-c", "markdown": "https://wpnews.pro/news/fast-great-circle-distance-calculation-in-cuda-c.md", "text": "https://wpnews.pro/news/fast-great-circle-distance-calculation-in-cuda-c.txt", "jsonld": "https://wpnews.pro/news/fast-great-circle-distance-calculation-in-cuda-c.jsonld"}}