From: Torsten Dreyer Date: Sat, 20 Nov 2010 10:07:24 +0000 (+0100) Subject: Avoid division by zero in exponential filters X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=495a23a80a172f4c0fbda3f087d1469f7d5f734b;p=flightgear.git Avoid division by zero in exponential filters --- diff --git a/src/Autopilot/digitalfilter.cxx b/src/Autopilot/digitalfilter.cxx index 57bca13c8..45087a79a 100644 --- a/src/Autopilot/digitalfilter.cxx +++ b/src/Autopilot/digitalfilter.cxx @@ -265,9 +265,14 @@ void ExponentialFilterImplementation::initialize( double output ) double ExponentialFilterImplementation::compute( double dt, double input ) { input = GainFilterImplementation::compute( dt, input ); + double tf = _TfInput.get_value(); double output_0; - double alpha = 1 / ((_TfInput.get_value()/dt) + 1); + + // avoid negative filter times + // and div by zero if -tf == dt + + double alpha = tf > 0.0 ? 1 / ((tf/dt) + 1) : 1.0; if(_isSecondOrder) { output_0 = alpha * alpha * input +