# 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?

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

Commented:
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

Commented:
Number

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

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

mx
0

Commented:
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

Commented:
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

Commented:

mx
0

Commented:
numbers with a power of 2 in the denominator.
0

Commented:
thank you.

Impressive list of Certs ozo ...

mx
0