Forum Discussion

p_mariet's avatar
p_mariet
Occasional Contributor
11 years ago

Testcomplete subsctraction problem VBScript

Hello,



I'm having a hard time with a simple substraction in Testcomplete (I know that sounds a bit stupid...).



Here is an example:




Dim one, two


 


one = (79.10 - 67.80)


two = 11.3


 


If one = two then


 


  Call Log.Message("yes")


 


Else


 


  Call Log.Error("no")


  


End if



Here, one and two are identical (both = 11.3), they are also assigned to the Double type. But when I execute this part I get the "no" message returned even thought they are the same.



They are somehow not identical for Testcomplete but I have no idea why. Has anyone encountered a similar problem?



Thank you

  • Working with float types is always a bottleneck in scripting language.



    I am not an expert in this and can't explain why exactly this example works like this, but I've found a solution for you




    Dim one, two


     


    one = Round(79.10 - 67.80, 2)


    two = Round(11.30, 2)


     

  • karkadil's avatar
    karkadil
    Valued Contributor
    Working with float types is always a bottleneck in scripting language.



    I am not an expert in this and can't explain why exactly this example works like this, but I've found a solution for you




    Dim one, two


     


    one = Round(79.10 - 67.80, 2)


    two = Round(11.30, 2)


     

  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Double is not precise or some non-sense like that which is one of many reasons I use JScript :).

    You can use this:






    Dim one, two


     


    one = (79.10 - 67.80)


    two = 11.3


     


    If cstr(one) = cstr(two) then


     msgbox("yes")


      


     


    Else


     


     msgbox("no")


      


    End if





    Btw this is not a Test Complete issue, if you save that exact code to a VBS file(replacing your logs with message boxes) it does the same thing.

  • hlalumiere's avatar
    hlalumiere
    Regular Contributor
    The reason is that FLOATs and DOUBLEs cannot properly express integer numbers. See http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems . They are meant to represent fractions, not integers,



    An alternative would be to use the DECIMAL format, which internally is stored as an unpacked BCD, which solves the issues above. Of course it does not have the precision of a DOUBLE...
  • p_mariet's avatar
    p_mariet
    Occasional Contributor


    Thank you guys! 



    I'm going to stick to the Round solution, applys best to my script.