From c070d326e015bb37fad364e4d05d5f768b4a5b6e Mon Sep 17 00:00:00 2001 From: Peter Duerr Date: Fri, 4 Dec 2015 14:14:31 +0900 Subject: [PATCH] Avoid violation of acos' domain in map_projection * There are pathological cases (e.g., setpoint very close to reference for certain reference latitudes), where numerical errors lead to a sum larger than 1.0 passed to acos, resulting in NaN values. This should fix issue #2813 --- src/lib/geo/geo.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/geo/geo.c b/src/lib/geo/geo.c index fe0224d508..448fd97986 100644 --- a/src/lib/geo/geo.c +++ b/src/lib/geo/geo.c @@ -148,7 +148,13 @@ __EXPORT int map_projection_project(const struct map_projection_reference_s *ref double cos_lat = cos(lat_rad); double cos_d_lon = cos(lon_rad - ref->lon_rad); - double c = acos(ref->sin_lat * sin_lat + ref->cos_lat * cos_lat * cos_d_lon); + double arg = ref->sin_lat * sin_lat + ref->cos_lat * cos_lat * cos_d_lon; + if (arg > 1.0) { + arg = 1.0; + } else if (arg < -1.0) { + arg = -1.0; + } + double c = acos(arg); double k = (fabs(c) < DBL_EPSILON) ? 1.0 : (c / sin(c)); *x = k * (ref->cos_lat * sin_lat - ref->sin_lat * cos_lat * cos_d_lon) * CONSTANTS_RADIUS_OF_EARTH;