Re: How to overcome very slight calculation error.



Welcome to the world of computers.

It turns out that one of the FIRST things a programmer will be taught is
that when a computer stores a "real" number (as opposed to a integer
number), the computer can only store a approximation of that number.

so, when you store a value such as 3 / 10, the resulting binary
representation of .3 is only a approximation of that result.

The following code if run in excel, word, or ms-access will produce the same
rounding errors:

Public Sub TestAdd()

Dim MyNumber As Single
Dim i As Integer

For i = 1 To 10
MyNumber = MyNumber + 1.01
Debug.Print MyNumber
Next i
End Sub

Here is the actual outpput of the above:

1.01
2.02
3.03
4.04
5.05
6.06
7.070001
8.080001
9.090001
10.1

You can see that after just 7 addtions..already rounding is occuring
and if we add the follwing line of code to the end of the above:

if MyNumber = 10.1 = True then

msgbox "the number is 10.1"

else

msgbox "the number is somthing else"

endif


The above will actuall produce:

the number is something else

So, what this means is that if you plan to use any financial calculations,
you can't store, or nor use "real" numbers, as they are a approximates. The
solution in this case is to use decimal number, or a data type of currency.
(these are stored as scaled integers).

So, if you are working with financial numbers, and don't want rounding
errors, then use currency.

further, you don't say what data type "c" is.

I just tried:


Dim c As Single

c = 0.175

Debug.Print c * 1325

the output was:

231.875

In fact, in the debug window, I just typed in:

? 0.175 * 1325
The reuslt is
231.875

However, if I type in:

? (0.175 * 1325) = (231.875 )
output is:
false

Howeer:
? ccur(0.175 * 1325.0) = ccur(231.875)
true

So, just use currency for these finical calculations. (they have a max of 4
digits after the decimal place, and are actually scaled integers).

If that data is from a table, you should convert that column to currency, or
perhaps decimal type.

I would also ensure that you always use a option explicit in your code....

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@xxxxxxx


.



Relevant Pages

  • Re: Anyone had different results between Access calculations and E
    ... Thanks, Albert. ... Dim MyNumber As Single ... If that is the case, then use a currency field, NOT ... because we have a native built in currency data type. ...
    (microsoft.public.access.reports)
  • Re: Accounts/Bookkeeping data type
    ... I'm never going to want to store currency amounts to more than 2 DP - it ... > It is best to define the amount fields in the tables as Data Type: ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Why the fuzzy math in Access?
    ... actually can't represent *exact* floating point numbers. ... Dim MyNumber As Single ... Fortunately, for financial applications, you can use the built-in currency ... or use a diffent data type. ...
    (microsoft.public.access.gettingstarted)
  • Re: Accounts/Bookkeeping data type
    ... It is best to define the amount fields in the tables as Data Type: Currency, ... Format: Standard, Decimal Places: 2 this will make form building a bit less ... store data containing numeric values: ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Why the fuzzy math in Access?
    ... One of the first courses you get in a computing class is abut how computers ... Dim MyNumber As Single ... Fortunately, for financial applications, you can use the built-in currency ... or use a diffent data type. ...
    (microsoft.public.access.gettingstarted)