Forum Discussion

leonk's avatar
leonk
New Contributor
15 years ago

Testing inequality



I have the following code to test if a reading is within +/-25% of the expected value.



function testWithinLimits()

{

   var expectedVal = 200;

   var ErrorInPercent = 25;

   var UpperLimit = expectedVal/1000 * ((100 + ErrorInPercent)/100);

   var LowerLimit = expectedVal/1000 * ((100 - ErrorInPercent)/100);



   var readVal = "0.15";

   var reading = StrToFloat(readVal);

   bResult1 = (reading <= UpperLimit);

   bResult2 = (reading >= LowerLimit);



   strMsg = FloatToStr(reading) + " <= " + FloatToStr(UpperLimit) + " is : " + bResult1;

   strMsg += "\r\n";

   strMsg += FloatToStr(reading) + " >= " + FloatToStr(LowerLimit) + " is : " + bResult2;  

   

   ShowMessage(strMsg);



}



The expected value is 0.2 so a range from 0.15 to 0.25 (inclusive) is allowed. My reading is 0.15 but the function shows a false result.



Have I missed a type conversion or made a serious error?



I'm using TestComplete v6.52.419.7.



Thanks, Leon.

2 Replies

  • leonk's avatar
    leonk
    New Contributor


    Stripping some of the extra code away gives me this...



    function theSame()

    {



       var expectedVal = 200;

       var ErrorInPercent = 25;

       var x = expectedVal/1000 *  (100-ErrorInPercent) / 100;

       var y = expectedVal/1000 * ((100-ErrorInPercent)/100);



       ShowMessage((x == y)?"True":"False");   



    }



    I am getting a False result though believe x and y should be equal if I have my operator precedence correct.
  • Hi Leon,



    The same behavior is observed in external JScript scripts (launched from JS files), so it is not related to TC. This can be a problem in the MS JScript engine or just a floating-point computation inaccuracy.