Link to home
Start Free TrialLog in
Avatar of kartikviyer
kartikviyer

asked on

Problem in VB

Hi,

This is my piece of code. ReadINI is a function that returns a string.

Dim MM as Double
Dim A, B  As Double

MM = 1/25.4
A = Val(ReadINI("Material 1", "Thickness", gszInput))
B = P.getVal(1) * MM

if A <> B then
      "o/p not equal"
else
      "o/p equal"
end if


The values of A and B when i try to debug through my app show as 0.071 i.e. they are equal yet program enters the "o/p not equal" section of
the if statement.

Any ideas as to why it isnt seeing the two values as equal.

Thanks
Kartik
Avatar of mvidas
mvidas
Flag of United States of America image

Hi Kartik,

You could try dimming A as a double as well, the way you currently have it has A being declared of type variant (most likely the error):

 Dim A As Double, B As Double

If not, debug the two values using a longer decimal portion, see if that could be the issue:
 Debug.Print Format(A, "0.000000000000000")
 Debug.Print Format(B, "0.000000000000000")

Matt
Avatar of kartikviyer
kartikviyer

ASKER

Matt,

Tried both suggestions as recomended. It did not work.

When i use Debug.Print the o/p for A & B are

0.0710000000
0.0710000000

But it continues to see them as unequal.

Kartik
ASKER CERTIFIED SOLUTION
Avatar of mvidas
mvidas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Matt,

CDbl worked and it processed the If statement correctly.

Thanks for the help

Kartik
Generally, floating point numbers should not be compared for strict equality. This is because it is not possible to exactly represent all fractional numbers (in base 10) in floating point representation which is base 2. Rather, it is better to check for the difference between the two numbers to be less than some epsilon, which can be any arbirtarily small value, say 1E-6.

The only possible reason that I can think of for the inqeuality in the above case is that "val" and CDbl use different approaches to arriving at the value of the string which leads to some difference after many decimal places. Hence equality is not working.

You could try taking the difference between the two numbers and displaying it to see what it is.