From: oliskoli Date: Sat, 29 Apr 2006 16:41:30 +0000 (+0000) Subject: Check valid range for n before calling acos(n). Set errno always to zero. X-Git-Tag: archive/raspbian/1.10.0+ds-2+rpi1~1^2~12^2~11^2~2908 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=644c09b5ef6774831841c607a99199cfe0e0becf;p=gpsbabel.git Check valid range for n before calling acos(n). Set errno always to zero. --- diff --git a/gpsbabel/grtcirc.c b/gpsbabel/grtcirc.c index d2cd3a7fc..752e3f229 100644 --- a/gpsbabel/grtcirc.c +++ b/gpsbabel/grtcirc.c @@ -59,10 +59,18 @@ double radtometers( double rads ) { double gcdist( double lat1, double lon1, double lat2, double lon2 ) { double res; - res = acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2)); - if (errno == EDOM) { /* Math argument out of domain of function */ - errno = 0; - return 0; + + errno = 0; + + res = sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2); + if (res > 1.0) res = 1.0; + else if (res < -1.0) res = -1.0; + + res = acos(res); + + if ((isnan(res)) || (errno == EDOM)) { /* this should never happen: */ + errno = 0; /* Math argument out of domain of function, */ + return 0; /* or value returned is not a number */ } return res; }