From: torsten Date: Fri, 21 Aug 2009 12:10:50 +0000 (+0000) Subject: Avoid NAN due to floating point rounding errors X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e782adef94813852ad40cf6316336991378c4654;p=simgear.git Avoid NAN due to floating point rounding errors --- diff --git a/simgear/math/SGGeodesy.cxx b/simgear/math/SGGeodesy.cxx index e42ce1e3..9971829a 100644 --- a/simgear/math/SGGeodesy.cxx +++ b/simgear/math/SGGeodesy.cxx @@ -84,6 +84,14 @@ SGGeodesy::SGCartToGeod(const SGVec3& cart, SGGeod& geod) double q = Z*Z*(1-e2)*ra2; double r = 1/6.0*(p+q-e4); double s = e4*p*q/(4*r*r*r); +/* + s*(2+s) is negative for s = [-2..0] + slightly negative values for s due to floating point rounding errors + cause nan for sqrt(s*(2+s)) + We can probably clamp the resulting parable to positive numbers +*/ + if( s >= -2.0 && s <= 0.0 ) + s = 0.0; double t = pow(1+s+sqrt(s*(2+s)), 1/3.0); double u = r*(1+t+1/t); double v = sqrt(u*u+e4*q);