Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two variables with the same value are NOT equal 1

Status
Not open for further replies.

ClickHere

Programmer
Jul 13, 2002
43
0
0
US
Is there a logical explanation why the following code will not work?
I came across this problem in an app I'm working on where I have to increment a variable ('A') by .1 inside of a loop and If 'A'='B' then 'do something'. It works fine when 'B' is 1.1 or 1.2, but when it's 1.3 and above, it skips what it's suppose to do. According to Debug 'A'=1.3 and 'B'=1.3 but 'A' does not equal 'B'.

To try this simple test you'll need a Form with 2 Labels (Label1 & Label2) and a Command Button (Command1). Then click the button to add .1 to 'A'.

Option Explicit
Private A As Single
Private B As Single

Private Sub Command1_Click()

A = A + 0.1
Label1 = A
If A = B Then Beep

End Sub

Private Sub Form_Load()

A = 1
B = 1.3

Label1 = A
Label2 = B

End Sub

Any thoughts?
Thanks
 
Singles are stored as 'floating point numbers' - that means they are only stored approximately, not exactly. There will be times when an 'equals' test will not make your 1.1 + .1 + .1 exactly the same as 1.3

To get round this you can use an exact variable type (Integer, Long, Decimal) or (depending on your requirements) use something like:

If Abs(A - B) < .000001 Then

Look up Variable Types in VBHelp (or MSDN) for more details


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks alot johnwm. The Decimal data type has done the trick. Although using the name Decimal (Private A as Decimal) gives a compile error. I can however use the literal char for Decimal (Private A@). Seems to work great.

A star for you.
Thanks again.
 
i think johnwm meant double and not decimal [pc2]

good luck

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
oops.... or maybe not!!!

[blush]

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
ADoozer

I know johnwm meant Decimal and not Double - Double is still a floating point representation (just with a higher accuracy than Single)

I still wouldn't use an 'Equals' comparison with any of the FP types

as I said - Look up Variable Types in VBHelp (or MSDN) for more details!


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
sorry for the tripple post but:-

Note At this time the Decimal data type can only be used within a Variant, that is, you cannot declare a variable to be of type Decimal. You can, however, create a Variant whose subtype is Decimal using the CDec function

i think that was the point!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
[smile]Talk about collisions - We're falling all over each other here!


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
LOL it'll teach me to stay in my box with matters that dont concern me [blush][cry]

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
>the Decimal data

it's good to see that this is getting more famous...
 
Or, you could use the Currency/Money datatype. It's fixed-point.

Johnwm is correct - 1.1, when converted to binary, is stored as an approximation (you can't accurately represent 0.1 in binary). So any attempt to do equality comparisons on these values wouldn't work.

Chip H.
 
Or you could use Format:

If Format(a, &quot;0.0&quot;) = Format(b, &quot;0.0&quot;) Then
 
Thanks chiph and Robse. Those are good work-around ideas also. And I probably would have gone one of those routes had johnwm not opened my eyes to the Decimal data type. So far it's working slicker-than-cat-slobber. And he answered my main question 'WHY'. I mean, even a simple calculator application needs to handle .1's. It just stumped me when it wouldn't work. Live and learn.

Thanks
 
The Format function will just round the number.
It will still at times still result in differences.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top