Overflow issue

wierd problem

Page 1 of 1

7 Replies - 832 Views - Last Post: 01 September 2008 - 08:29 AM Rate Topic: -----

#1 Arblique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-August 08

Overflow issue

Posted 31 August 2008 - 05:05 AM

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...
Private Sub Form_Load()

	Dim t As Long

	t = 4 * 10000

End Sub



...however this...

Private Sub Form_Load()

	Dim t As Long

	t = 40000

End Sub



...seems to work fine. I'm stumped! :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: Overflow issue

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Overflow issue

Posted 31 August 2008 - 05:31 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 Arblique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-August 08

Re: Overflow issue

Posted 31 August 2008 - 07:20 AM

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

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:

Cheers

This post has been edited by Arblique: 31 August 2008 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 Arblique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-August 08

Re: Overflow issue

Posted 31 August 2008 - 10:15 AM

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.



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...

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 :-

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.
Was This Post Helpful? 0
  • +
  • -

#5 Ken Halter  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 35
  • Joined: 18-November 07

Re: Overflow issue

Posted 31 August 2008 - 10:16 AM

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)
Was This Post Helpful? 0
  • +
  • -

#6 Arblique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-August 08

Re: Overflow issue

Posted 31 August 2008 - 05:18 PM

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. ;)

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: 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!
Was This Post Helpful? 0
  • +
  • -

#7 Ken Halter  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 35
  • Joined: 18-November 07

Re: Overflow issue

Posted 31 August 2008 - 11:11 PM

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! :P

Weird floating point problem
http://groups.google...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.
Was This Post Helpful? 0
  • +
  • -

#8 Arblique  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-August 08

Re: Overflow issue

Posted 01 September 2008 - 08:29 AM

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:






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: 57

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1