Link to home
Start Free TrialLog in
Avatar of mcnuttlaw
mcnuttlawFlag for United States of America

asked on

Math subtraction: 1 minus 1 = -2.91038304567337E-11

I have five unbound textboxes set to Standard, 2 decimal points.  

The first four are populated with a Select query.  
The fifth box sums the first four boxes for a Total.

The numbers appear correctly (amount and formatted) on the form as does the Total.  For redundancy and error checking (due to the db being a mission critical financial database), I do this math...

If Not (Box1 + Box2 + Box3 + Box4) = Box5 Then MsgBox "Error"

Results...

(Box1 + Box2 + Box3 + Box4) equals 175000
Box5 Total equals 175000
(Box1 + Box2 + Box3 + Box4) - Box5 should equal zero

However, in my case, the difference is not zero but is  -2.91038304567337 E-11.

Ideas?
ASKER CERTIFIED SOLUTION
Avatar of Malik1947
Malik1947

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
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
It's the new math ... not to worry :-)

" I have five unbound textboxes set to Standard, 2 decimal points.  "
That is strictly for formatting.  What are the data types of your fields?

mx

Avatar of mcnuttlaw

ASKER

The fields are Number (Standard, 2)
Number

Yes, but what is the Field SIze (single, double, etc) ?

Standard, 2 is only formatting - not that data type.

mx
SOLUTION
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
The fields are set to Double.

I'll try the Round function.
I'm pretty sure you are being struck by rounding errors - I've seen it before.
Avatar of Malik1947
Malik1947

I think that DatabaseMX is correct like I mentioned in my first post.
Rounding to 2 decimals worked.

So the lesson here is to round whenever possible when doing calculations in VBA?
> round whenever possible
In general, it's better to round as late as possible to avoid accumulation of round off errors.
and don't trust floating point equality comparisons, unless all all your intermediate results were exact integers or dyadic rationals
numbers with a power of 2 in the denominator.
thank you.

Impressive list of Certs ozo ...

mx