mcnuttlaw
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Yes, but what is the Field SIze (single, double, etc) ?
Standard, 2 is only formatting - not that data type.
mx
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The fields are set to Double.
I'll try the Round function.
I'll try the Round function.
I'm pretty sure you are being struck by rounding errors - I've seen it before.
I think that DatabaseMX is correct like I mentioned in my first post.
ASKER
Rounding to 2 decimals worked.
So the lesson here is to round whenever possible when doing calculations in VBA?
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
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
What are 'dyadic rationals' ?
mx
mx
numbers with a power of 2 in the denominator.
thank you.
Impressive list of Certs ozo ...
mx
Impressive list of Certs ozo ...
mx
" 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