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
mcnuttlawAsked:
Who is Participating?
 
Malik1947Connect With a Mentor 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
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)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
 
mcnuttlawAuthor Commented:
The fields are Number (Standard, 2)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Number

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

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

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Connect With a Mentor Commented:
It basically sounds like rounding errors to me.

Possibly you could fix it with the Round function ?

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

I'll try the Round function.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
I'm pretty sure you are being struck by rounding errors - I've seen it before.
0
 
Malik1947Commented:
I think that DatabaseMX is correct like I mentioned in my first post.
0
 
mcnuttlawAuthor Commented:
Rounding to 2 decimals worked.

So the lesson here is to round whenever possible when doing calculations in VBA?
0
 
ozoCommented:
> 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
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
What are 'dyadic rationals' ?

mx
0
 
ozoCommented:
numbers with a power of 2 in the denominator.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
thank you.

Impressive list of Certs ozo ...

mx
0
All Courses

From novice to tech pro — start learning today.