# 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?
LVL 2
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
it looks like the numbers are a result of a calculation that is giving you a number like 1.0000000000001234

so even though the value at 2 decimal places is 1.00 the calculation is using the rest of it to give you the number
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Database Architect / Systems AnalystCommented:
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

0
Author Commented:
The fields are Number (Standard, 2)
0
Database Architect / Systems AnalystCommented:
Number

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

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

mx
0
Database Architect / Systems AnalystCommented:
It basically sounds like rounding errors to me.

Possibly you could fix it with the Round function ?

mx
0
Author Commented:
The fields are set to Double.

I'll try the Round function.
0
Database Architect / Systems AnalystCommented:
I'm pretty sure you are being struck by rounding errors - I've seen it before.
0
Commented:
I think that DatabaseMX is correct like I mentioned in my first post.
0
Author Commented:
Rounding to 2 decimals worked.

So the lesson here is to round whenever possible when doing calculations in VBA?
0
Commented:
> 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
0
Database Architect / Systems AnalystCommented:

mx
0
Commented:
numbers with a power of 2 in the denominator.
0
Database Architect / Systems AnalystCommented:
thank you.

Impressive list of Certs ozo ...

mx
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.