🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.databases.ms-access
4 messages
4 total messages Started by Larry LeBlanc Thu, 15 Jul 1999 00:00
Rounding problems with Access
#333
Author: Larry LeBlanc
Date: Thu, 15 Jul 1999 00:00
80 lines
3404 bytes
Good day,

Can anyone explain to me why the following source code:

    ===================================
    Private Sub Command9_Click()

    Dim Looper As Single

    For Looper = 0 To 1 Step 0.1
        Debug.Print Looper
        Stop
    Next Looper

    End Sub
    ===================================

leads to the following results:

    ===================================
     0
     0.1
     0.2
     0.3
     0.4
     0.5
     0.6
     0.7
     0.8000001
     0.9000001
    ===================================

Why is Access having difficulties with such a basic program.  I am writing a
complex financial simulations (on the Canadian Mutual Fund Market), and what
I consider basic math is being completely mangled by Access.  For example,
when I run the following simplified code:

    ===================================
    Dim SharesToPurchase As Long
    Dim Bank As Currency
    Dim ClosingPrice As Single

    SharesToPurchase = Int(Bank / ClosingPrice)
    Debug.Print "Bank Balance Before Transaction = " & Str(Bank)
    Debug.Print "Closing Price = " & Str(ClosingPrice)
    Debug.Print "Shares To Purchase = " & Str(SharesToPurchase)
    Debug.Print "Transaction Amount = " & Str(SharesToPurchase *
ClosingPrice)
    Bank = Bank - (SharesToPurchase * ClosingPrice)
    Debug.Print "Bank Balance After Transaction = " & Str(Bank)
    Stop
    ===================================

I get the following results:

    ===================================
    Bank Balance Before Transaction =  1000000
    Closing Price =  15.06
    Shares To Purchase =  66401
    Transaction Amount =  999999.087862968
    Bank Balance After Transaction =  .912137031555176
    ===================================

Does anyone know of a Microsoft KnowledgeBase article that can help me with
this problem?  In addition to posting a follow-up to this problem, can you
please e-mail me directly (mailto:larry.leblanc@sympatico.ca) as I cannot
follow this newsgroup.

TIA,

Larry
--
===================================================================
Larry LeBlanc, CNE-3
Montreal, Quebec, CANADA
mailto:larry.leblanc@sympatico.ca     (flames directed to /dev/null)
-------------------------------------------------------------------
This message was compiled and sent using environmentally friendly
recycled electrons and was not tested on animals.
===================================================================
Re: Rounding problems with Access
#335
Author: Shamil Salakhetdinov
Date: Thu, 15 Jul 1999 00:00
103 lines
3344 bytes
Larry,

Use currency data type for financial calculations, don't mix currency with
single, write your own rounding function to properly round results when you
mix currency with single and double.

This  code:

    Dim Looper As Currency ' Single

    For Looper = 0 To 1 Step 0.1
        Debug.Print Looper
        Stop
    Next Looper

Results in:

 0
 0,1
 0,2
 0,3
 0,4
 0,5
 0,6
 0,7
 0,8
 0,9
 1

And this code:

    Dim SharesToPurchase As Long
    Dim Bank As Currency
    Dim ClosingPrice As Currency 'Single

    Bank = 1000000
    ClosingPrice = 15.06
    SharesToPurchase = 66401

    SharesToPurchase = Int(Bank / ClosingPrice)
    Debug.Print "Bank Balance Before Transaction = " & Str(Bank)
    Debug.Print "Closing Price = " & Str(ClosingPrice)
    Debug.Print "Shares To Purchase = " & Str(SharesToPurchase)
    Debug.Print "Transaction Amount = " & Str(SharesToPurchase *
ClosingPrice)
    Bank = Bank - (SharesToPurchase * ClosingPrice)
    Debug.Print "Bank Balance After Transaction = " & Str(Bank)

Produces the following results:

Bank Balance Before Transaction =  1000000
Closing Price =  15.06
Shares To Purchase =  66401
Transaction Amount =  999999.06
Bank Balance After Transaction =  .94

HTH,
Shamil

P.S. This rounding problem isn't only MS Access problem - this is VBA/VB
engines common problem/feature...

--
Shamil Salakhetdinov
DARTS Ltd., St.Petersburg, Russia
e-mail: shamil@marta.darts.spb.ru
Web: http://members.xoom.com/shamil_s

Larry LeBlanc <larry.leblanc@sympatico.ca> wrote in message
news:wQkj3.4$PZ3.247@198.235.216.4...
> Good day,
>
> Can anyone explain to me why the following source code:
>
>     ===================================
>     Private Sub Command9_Click()
>
>     Dim Looper As Single
>
>     For Looper = 0 To 1 Step 0.1
>         Debug.Print Looper
>         Stop
>     Next Looper
>
>     End Sub
>     ===================================
>
> leads to the following results:
>
>     ===================================
>      0
>      0.1
>      0.2
>      0.3
>      0.4
>      0.5
>      0.6
>      0.7
>      0.8000001
>      0.9000001
>     ===================================
>
<tail skipped>
Re: Rounding problems with Access
#337
Author: Tom van Stiphout
Date: Thu, 15 Jul 1999 00:00
94 lines
3797 bytes
On Thu, 15 Jul 1999 12:52:44 GMT, "Larry LeBlanc"
<larry.leblanc@sympatico.ca> wrote:


Check out KB Article Q111781 

I usually get ticked off by people who can post to the newsgroup, but
can't get back on to check results. However, you were using
environmentally safe electrons, which gives you bonus points.

-Tom. 

>Good day,
>
>Can anyone explain to me why the following source code:
>
>    ===================================
>    Private Sub Command9_Click()
>
>    Dim Looper As Single
>
>    For Looper = 0 To 1 Step 0.1
>        Debug.Print Looper
>        Stop
>    Next Looper
>
>    End Sub
>    ===================================
>
>leads to the following results:
>
>    ===================================
>     0
>     0.1
>     0.2
>     0.3
>     0.4
>     0.5
>     0.6
>     0.7
>     0.8000001
>     0.9000001
>    ===================================
>
>Why is Access having difficulties with such a basic program.  I am writing a
>complex financial simulations (on the Canadian Mutual Fund Market), and what
>I consider basic math is being completely mangled by Access.  For example,
>when I run the following simplified code:
>
>    ===================================
>    Dim SharesToPurchase As Long
>    Dim Bank As Currency
>    Dim ClosingPrice As Single
>
>    SharesToPurchase = Int(Bank / ClosingPrice)
>    Debug.Print "Bank Balance Before Transaction = " & Str(Bank)
>    Debug.Print "Closing Price = " & Str(ClosingPrice)
>    Debug.Print "Shares To Purchase = " & Str(SharesToPurchase)
>    Debug.Print "Transaction Amount = " & Str(SharesToPurchase *
>ClosingPrice)
>    Bank = Bank - (SharesToPurchase * ClosingPrice)
>    Debug.Print "Bank Balance After Transaction = " & Str(Bank)
>    Stop
>    ===================================
>
>I get the following results:
>
>    ===================================
>    Bank Balance Before Transaction =  1000000
>    Closing Price =  15.06
>    Shares To Purchase =  66401
>    Transaction Amount =  999999.087862968
>    Bank Balance After Transaction =  .912137031555176
>    ===================================
>
>Does anyone know of a Microsoft KnowledgeBase article that can help me with
>this problem?  In addition to posting a follow-up to this problem, can you
>please e-mail me directly (mailto:larry.leblanc@sympatico.ca) as I cannot
>follow this newsgroup.
>
>TIA,
>
>Larry
>--
>===================================================================
>Larry LeBlanc, CNE-3
>Montreal, Quebec, CANADA
>mailto:larry.leblanc@sympatico.ca     (flames directed to /dev/null)
>-------------------------------------------------------------------
>This message was compiled and sent using environmentally friendly
>recycled electrons and was not tested on animals.
>===================================================================
>
>
Re: Rounding problems with Access
#338
Author: Robert A. Bowman
Date: Thu, 15 Jul 1999 00:00
30 lines
1998 bytes
The problem is in your data typing.  Floating point data types (single, double)
will not give you the results you want.  Use Currency instead.

Cheers,

Bob Bowman

Larry LeBlanc wrote:

> Good day,
>
> Can anyone explain to me why the following source code:
>
> Does anyone know of a Microsoft KnowledgeBase article that can help me with
> this problem?  In addition to posting a follow-up to this problem, can you
> please e-mail me directly (mailto:larry.leblanc@sympatico.ca) as I cannot
> follow this newsgroup.
>
> TIA,
>
> Larry
> --
> ===================================================================
> Larry LeBlanc, CNE-3
> Montreal, Quebec, CANADA
> mailto:larry.leblanc@sympatico.ca     (flames directed to /dev/null)
> -------------------------------------------------------------------
> This message was compiled and sent using environmentally friendly
> recycled electrons and was not tested on animals.
> ===================================================================
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads