[Nut-upsdev] [PATCH] Fix logic error in rhino driver

Stuart D. Gathman stuart at bmsi.com
Fri Oct 14 16:02:01 UTC 2011


On Fri, 14 Oct 2011, Thomas Jarosch wrote:

> The if() statement always evaluated to true.
> Detected by "cppcheck", not tested on real hardware.

> --- a/drivers/rhino.c
> +++ b/drivers/rhino.c
> @@ -187,7 +187,7 @@ AutonomyCalc( int ia ) /* all models */
>     {
>       currin = ( UtilPowerOut + ConstInt ) *1.0 / Vin;
>       auton = ( ( ( AmpH *1.0 / currin ) * 60 * ( ( BattVoltage - VbatMin ) * 1.0 /( VbatNom - VbatMin ) ) * FM ) + FA );
> -      if(  ( BattVoltage > 129 ) || ( BattVoltage < 144 ) )
> +      if(  ( BattVoltage > 129 ) && ( BattVoltage < 144 ) )
> 	result = 133;
>       else
> 	result = (int) auton;

Good catch on the bad logic, however, I thought the original intent would
have been:

       if(  ( BattVoltage < 129 ) || ( BattVoltage > 144 ) )
 	result = 133;
       else
 	result = (int) auton;

The idea being that the auton calculation was only valid within the 
defined range.  Also, I've found that range checks are more readable when
coded this way:

       if(  ( 129 < BattVoltage ) && ( BattVoltage < 144 ) )
 	result = (int) auton;
       else
 	result = 133;

(or the other way round if the auton calculation is actually only 
valid outside the range).

--
 	      Stuart D. Gathman <stuart at bmsi.com>
     Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flammis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.



More information about the Nut-upsdev mailing list