Strange result when adding a negative number to a positive number equaling zero RRS feed

  • Question

  • Ok, this one is totally baffling me.   I have a routine in my MS Access VBA process that runs a loop adding amounts to a variable.

    I used the typical Variable1 = Variable1 + Variable2  (where Variable2 is a new value as I run through a recordset).

    This process works fine with thousands of records that I am processing.   Variable1 totals an account value then resets when it goes to the next account.    

    The problem is...... if Variable1 happens to be a negative number (say -4.53) and Variable2 is the exact opposite postive number (4.53),

    The result should be zero.    This doesn't happen.   Instead, the result is a strange -8.88178419700125E-16.

    Again, the actual statement that this bombs looks like the following with the actual variables:

    TempCheckAmount = TempCheckAmount + AmountPaid

    (where TempCheckAmount = -4.53 and AmountPaid = 4.53)

    Both TempCheckAmount and AmountPaid are Dim as Double

    Any clues?

    Tuesday, January 29, 2013 5:57 PM

All replies

  • The numbers are only showing up with 2 decimals but there are many more.  Try rounding or fixing the variables to 2 digits before adding them.  When the data is put into the database does it only have 2 decimals or are there more?

    To check this use a debug.print to see the whole value being added.

    Hope this helps.

    Tuesday, January 29, 2013 6:34 PM