Problem comparing double values

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello all, I have a strange problem that I would appreciate any help
with. I have an application that involves a comparison of two numbers,
one of which is made up of the sum of the values in an array. The code
is as follows:

Private Sub CompareNumbers()
Dim intIndex As Integer
Dim arNumbers(5) As Double
Dim dblTotal As Double
Dim dblComparison As Double

arNumbers(0) = 16843.2
arNumbers(1) = 40348.41
arNumbers(2) = 371910.18
arNumbers(3) = 148183.27
arNumbers(4) = 29183.96
arNumbers(5) = 143530.98

For intIndex = 0 To 5
dblTotal = dblTotal + arNumbers(intIndex)
Next

dblComparison = 750000
If dblComparison = dblTotal Then
MsgBox "Array Total:" & Chr$(9) & dblTotal & vbCr & "Comparison:" &
Chr$(9) & dblComparison & vbCr & "Numbers match"
Else
MsgBox "Array Total:" & Chr$(9) & dblTotal & vbCr & "Comparison:" &
Chr$(9) & dblComparison & vbCr & "No match"
End If
End Sub

Notice the oddity? The numbers are apparently identical and yet
according to the code, they do not match. What is even more bizzare is
that if I reduce the total of the numbers in the array to 597000, made
up of the following:
'' arNumbers(0) = 57843.2
'' arNumbers(1) = 44348.41
'' arNumbers(2) = 371910.18
'' arNumbers(3) = 50183.27
'' arNumbers(4) = 29183.96
'' arNumbers(5) = 43530.98
the comparison suceeded.

I don't think I have ever come up against anything so strange or so
frustrating; if anyone can shed any light on this it would be greatly
appreciated.

Thanks in advance

Chris

*** Sent via Developersdex http://www.developersdex.com ***
.



Relevant Pages

  • Re: Problem comparing double values
    ... > Private Sub CompareNumbers() ... > Dim arNumbersAs Double ... > Dim dblComparison As Double ... Dim dblComparison As Variant ...
    (microsoft.public.vb.general.discussion)
  • Re: Problems with result sets from stored procedures on a IBM AS400 DB
    ... > I got a strange problem while using VB6 and using stored procedures with a ... > Dim cn As ADODB.Connection ... > Dim cmd As ADODB.Command ... > If I am using the VB.Net the ado connection (without the provider in the ...
    (microsoft.public.vb.database.ado)
  • Problem with Datatable Insert
    ... I am experiencing a strange problem when inserting rows in a datatable ... Private Sub Insert_Row(ByVal sender As Object, ... Dim tbl As DataTable = New DataTable ... cell, the cell value reverts back to 0. ...
    (microsoft.public.dotnet.languages.vb)
  • VB, MDAC and progress database problem
    ... If the field contains a value ending with a zero, ... I've tried the same things with a C++ program, without this strange problem ... Dim Connection As Object ... Set Connection = CreateObject ...
    (microsoft.public.vb.database.ado)
  • VB and Progress database with ADO
    ... If the field contains a value ending with a zero, ... I've tried the same things with a C++ program, without this strange problem ... Dim Connection As Object ... Set Connection = CreateObject ...
    (microsoft.public.office.developer.vba)