Click to See Complete Forum and Search --> : Double comparison


sripriyaunni
August 1st, 2001, 02:40 AM
Hi,

We have been facing some wierd problems while comparing variables declared as double in VB 5. The compare between the variables were failing because one of them was getting internally converted into finer decimals. Eg: We had variables A and B, both double types containinbg dollar amounts of 100.23 each (one was a sum of 50.23, 30 and 20 and taken from a collection) and the other was a screen input from a text box. The sum variable (say B) was being internally converted as 10.2299... and so the compare was failing.
Also, this doesnt happen everytime and also was not occurring for everyone (it was kind of machine specific).
So , to summarise, we would like to know the following:
1. Exact behaviour of doubles while comparison
2. How does difference in machines affect the double comparison
3. What are the chances of mismatches occurring
4. Workarounds, if any. (Some of the work arounds we have identified currently:
1. Try repeating it after sometimes, the compares may go through !!
2. Change the code to check for not more than 2 decimal precisions,
while comparing values
3. Change the data type of these variables to currency type)

5. We would also like to get a list of known VB bugs if available.
Thanks,
Sripriya

deghost
August 1st, 2001, 02:46 PM
I can only suggest you to try these things:
1. try converting double to double (yes, that IS stupid but may work) i.e. a=CDbl(a)
2. try making a percision level like this:
instead of: if a=b... do if Abs(a-b)>0.00001 ...
who knows, may work...

----------
The @host is everywhere!
----------

deghost
August 1st, 2001, 02:48 PM
correction, i ment : if Abs(a-b)<0.00001... (instead of >)

----------
The @host is everywhere!
----------