Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 132,360 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,255 people online right now. Registration is fast and FREE... Join Now!




Overflow issue

 
Reply to this topicStart new topic

Overflow issue, wierd problem

Arblique
post 31 Aug, 2008 - 04:05 AM
Post #1


New D.I.C Head

*
Joined: 31 Aug, 2008
Posts: 10



Thanked 1 times
My Contributions


Hi all,

I'm writing a fairly hefty modular synthesizer in vb6 (silly!) and have come across and interesting problem. I'm using a long integer to reference all the little machines. Having succesfully got 3 machines working nicely together I tried to put a forth in and got an overflow.


Now, correct me if I'm wrong but a long integer should be able to handle a number up to 2,147,483,647 so can anyone guess why VB6 is overflowing at 40,000?

An example...
CODE

Private Sub Form_Load()

    Dim t As Long

    t = 4 * 10000

End Sub


...however this...

CODE

Private Sub Form_Load()

    Dim t As Long

    t = 40000

End Sub


...seems to work fine. I'm stumped! blink.gif
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 31 Aug, 2008 - 04:31 AM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 118 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Is that the actual code that is causing the issue? If not can you post the exact code, it could be something other than what you think it is that is causing the issue but we wont ever know if we can see the actual code.
User is offlineProfile CardPM

Go to the top of the page

Arblique
post 31 Aug, 2008 - 06:20 AM
Post #3


New D.I.C Head

*
Joined: 31 Aug, 2008
Posts: 10



Thanked 1 times
My Contributions


The thing is, that pretty much IS the code causing the problem.

CODE

Public Function mouseOver(Button As Integer, x As Integer, y As Integer) As Long
    Dim mID As Integer, iID As Integer, oID As Integer
    Dim yp As Integer, j As Integer
    Dim t As Long

'FIRST CHUNK OF CODE
    If x >= mvarvduXPos And x <= mvarvduXPos + mvarvduWidth And y >= mvarvduYPos And y <= mvarvduYPos + mvarvduHeight Then
        mID = mvarmchID
    End If

'SECOND CHUNK OF CODE
    If mvarmchNumIns > 0 Then
        For j = 1 To mvarmchNumIns
            yp = (mvarvduHeight / (mvarmchNumIns + 1)) * j
            If x >= mvarvduXPos + 3 And x <= mvarvduXPos + 9 And y >= mvarvduYPos + yp - 3 And y <= mvarvduYPos + yp + 3 Then
                iID = j
            End If
        Next
    End If

'THIRD CHUNK OF CODE
    If mvarmchNumOuts > 0 Then
        For j = 1 To mvarmchNumOuts
            yp = (mvarvduHeight / (mvarmchNumOuts + 1)) * j
            If x >= mvarvduXPos + mvarvduWidth - 9 And x <= mvarvduXPos + mvarvduWidth - 3 And y >= mvarvduYPos + yp - 3 And y <= mvarvduYPos + yp + 3 Then
                oID = j
            End If
        Next
    End If

'HERE IS THE PROBLEM


    t = (mID * 10000) + (oID * 100) + (iID)

    mouseOver = t
    
        
End Function



Ok what's happening is this...

First chunk of code returns the Machine ID that the mouse is hovering over.
Second chunk of code returns the Input Nub ID of the relevant machine (0 if not over nub)
Third chunk of code return the Output Nub ID...

Essentially the function returns a long integer which has encoded in it all three bits of mouse-over info.
Say the mouse is over machine 4 (the one with the problem) and also over input nub 3, the result should look like this -> 40300

40300 is a number that should never cause a long integer to overflow. A normal integer yes. But not a long one!

2,147,483,647 blink.gif

Cheers

This post has been edited by Arblique: 31 Aug, 2008 - 07:02 AM
User is offlineProfile CardPM

Go to the top of the page

Arblique
post 31 Aug, 2008 - 09:15 AM
Post #4


New D.I.C Head

*
Joined: 31 Aug, 2008
Posts: 10



Thanked 1 times
My Contributions


Additional.

Further chin scratching and experimentation has shed some light.

Using the following code, I have determined that there is a little bug (maybe) in VB's interpreter.



CODE

Dim t As Long
Dim t2 As Long
Private Sub Form_Load()
    Me.Show
    Do
        DoEvents
        t = 4 * t2
        Text1.Text = t & "," & t2
        t2 = t2 + 1
    Loop
End Sub


I get t2 over 10000 and multiply by four and it works, BUT...

CODE

Dim t As Long
Dim t2 As Long
Private Sub Form_Load()
    Me.Show
    Do
        DoEvents
        t = 4 * 10000
        Text1.Text = t & "," & t2
        t2 = t2 + 1
    Loop
End Sub


...still doesn't. ???!!!!


Hey ho, bug fixed thusly :-

CODE

Public Function mouseOver(Button As Integer, x As Integer, y As Integer) As Long
    Dim mID As Integer, iID As Integer, oID As Integer
    Dim yp As Integer, j As Integer
    Dim t As Long
    Dim a As Long

'FIRST CHUNK OF CODE
    If x >= mvarvduXPos And x <= mvarvduXPos + mvarvduWidth And y >= mvarvduYPos And y <= mvarvduYPos + mvarvduHeight Then
        mID = mvarmchID
    End If

'SECOND CHUNK OF CODE
    If mvarmchNumIns > 0 Then
        For j = 1 To mvarmchNumIns
            yp = (mvarvduHeight / (mvarmchNumIns + 1)) * j
            If x >= mvarvduXPos + 3 And x <= mvarvduXPos + 9 And y >= mvarvduYPos + yp - 3 And y <= mvarvduYPos + yp + 3 Then
                iID = j
            End If
        Next
    End If

'THIRD CHUNK OF CODE
    If mvarmchNumOuts > 0 Then
        For j = 1 To mvarmchNumOuts
            yp = (mvarvduHeight / (mvarmchNumOuts + 1)) * j
            If x >= mvarvduXPos + mvarvduWidth - 9 And x <= mvarvduXPos + mvarvduWidth - 3 And y >= mvarvduYPos + yp - 3 And y <= mvarvduYPos + yp + 3 Then
                oID = j
            End If
        Next
    End If

'HERE IS THE PROBLEM

    a=10000
    t = (mID * a) + (oID * 100) + (iID)

    mouseOver = t
    
        
End Function


Strange.
User is offlineProfile CardPM

Go to the top of the page

Ken Halter
post 31 Aug, 2008 - 09:16 AM
Post #5


New D.I.C Head

*
Joined: 18 Nov, 2007
Posts: 35



Thanked 5 times
My Contributions


The help topic for this 'issue' is actually fairly decent... but slightly misleading.

What's happening is... since VB *always* evaluates the right side of an equals sign before even attempting to figure out if it's a Long/Int/Float, it's the right side that's over-flowing.

When you get that error and click the Help button, you'll see....

'=========
You attempt to use a number in a calculation, and that number is coerced into an integer, but the result is larger than an integer. For example:
Dim x As Long
x = 2000 * 365 ' Error: Overflow
To work around this situation, type the number, like this:

Dim x As Long
x = CLng(2000) * 365
'=========

...but, that's not the only way to get around the problem...

When dealing with hard-coded numbers, add the symbol for the resulting variable type. I added the '&' below and it works every time.

Private Sub Command1_Click()
Debug.Print 20150& * 2 'should be 40300, and doesn't overflow
Debug.Print 20150 * 2 'should be 40300, but overflows
End Sub

So... for your problem, this should work fine...

t = (mID * 10000&) + (oID * 100&) + (iID)
User is offlineProfile CardPM

Go to the top of the page

Arblique
post 31 Aug, 2008 - 04:18 PM
Post #6


New D.I.C Head

*
Joined: 31 Aug, 2008
Posts: 10



Thanked 1 times
My Contributions


Cheers man!

I already (kind of...) worked through the problem by attaching another variable to do the multiply but in the interest of looking all big and cool I'll edit my code to match yours. wink2.gif

I have to say that during (27-6=21) 21 years messing about with BASIC (ZX -> BBC -> GW -> Q -> FAST -> POWER -> ETC. ETC) I don't think I have ever had that kind of problem. Even when porting an open source ray tracer from C to VB3!

I thank you again for this "English" explanation primarily because I'm about to start doing the REALLY HARD bit of the engine. At last count I had 14 "Generators"/"Affectors" mixing down to 1 output and making a really nice squelchy noise wub.gif and am about to start coding things like lo-pass/band-pass/hi-pass filters and delay lines and stuff like that which will inevitably have a lot of hellish mathematical complication.

Watch this space... ...I WILL need help again!



Or just go mad....................




....Thanks again!
User is offlineProfile CardPM

Go to the top of the page

Ken Halter
post 31 Aug, 2008 - 10:11 PM
Post #7


New D.I.C Head

*
Joined: 18 Nov, 2007
Posts: 35



Thanked 5 times
My Contributions


HA! :-)... Well, if you'll be working with floating point numbers, too... here's an old post of mine that contains a bit of advice, as well as a few links to documents on the subject. Who knows. you may actually survive the project without going mad! tongue.gif

Weird floating point problem
http://groups.google.com.my/group/microsof...4b7abbe3645bd1b

The bottom line with floats is... try and avoid using "="... test for "<". ">" or "<>" instead. If you poke around the info above, you'll see why... basically, kumputrs is dumb.
User is offlineProfile CardPM

Go to the top of the page

Arblique
post 1 Sep, 2008 - 07:29 AM
Post #8


New D.I.C Head

*
Joined: 31 Aug, 2008
Posts: 10



Thanked 1 times
My Contributions


A fair set of points in that link. Nice one.

Have already used the (number)& notation again - overflow when writing to the wave buffer ( -1>i>1, output=i*32767&) - and have converted all my wire "voltages" to floating point so I think it was worth reading that other thread.

On with the filters - i think - crazy.gif






Ooh, in case anyone's interested I've attached a WAV file of it in action!


Attached File(s)
Attached File  dodgy.wav ( 86.71k ) Number of downloads: 7
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:46AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month