Overflow when translating from C++ HELP!!

  • Thread starter Thread starter hunting
  • Start date Start date
H

hunting

Hello everyone,

I am having issues translating something from C++ to VBA due to
overflow.

As soon as I get to the end of this function, the routine dies. It
calculates a value, and I can get down to the last line in the function
(End Function) and everything is fine. As soon as I hit F8 one more
time, it chokes.

I got the function below from vbAccelerator ... it is supposed to be
able to do unsigned left and right shifts in VBA while avoiding
overflow. If there is something wrong with the function, what is it? If
not, am I passing a bad argument? Can anyone help? The m_lPower2(i)
variable is just equal to 2^i for i = 0 to 31.

Public Function RShift(ByVal lThis As Long, ByVal lBits As Long) As Long

If (lBits <= 0) Then
RShift = lThis
ElseIf (lBits > 63) Then
' .. error ...
ElseIf (lBits > 31) Then
RShift = 0
Else
If (lThis And m_lPower2(31 - lBits)) = m_lPower2(31 - lBits) Then
RShift = (lThis And (m_lPower2(31 - lBits) - 1)) *
m_lPower2(lBits) Or m_lPower2(31)
Else
RShift = (lThis And (m_lPower2(31 - lBits) - 1)) *
m_lPower2(lBits)
End If
End If

End Function
 
What is the m_lPower2 function doing? Perhaps you could provide
the code for that function.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Chip,

Thanks so much for being willing to take a look at this. The m_lPower2()
variables are set in a Sub that is called prior to the code I gave
earlier.

Here it is. Any help you can provide would be thoroughly appreciated.

Best regards.

Public Sub InitPowers()

m_lPower2(0) = &H1&
m_lPower2(1) = &H2&
m_lPower2(2) = &H4&
m_lPower2(3) = &H8&
m_lPower2(4) = &H10&
m_lPower2(5) = &H20&
m_lPower2(6) = &H40&
m_lPower2(7) = &H80&
m_lPower2(8) = &H100&
m_lPower2(9) = &H200&
m_lPower2(10) = &H400&
m_lPower2(11) = &H800&
m_lPower2(12) = &H1000&
m_lPower2(13) = &H2000&
m_lPower2(14) = &H4000&
m_lPower2(15) = &H8000&
m_lPower2(16) = &H10000
m_lPower2(17) = &H20000
m_lPower2(18) = &H40000
m_lPower2(19) = &H80000
m_lPower2(20) = &H100000
m_lPower2(21) = &H200000
m_lPower2(22) = &H400000
m_lPower2(23) = &H800000
m_lPower2(24) = &H1000000
m_lPower2(25) = &H2000000
m_lPower2(26) = &H4000000
m_lPower2(27) = &H8000000
m_lPower2(28) = &H10000000
m_lPower2(29) = &H20000000
m_lPower2(30) = &H40000000
m_lPower2(31) = &H80000000

End Sub
 
The code works fine for me (Excel2003). I can't get an overflow
error unless the m_lPower2 array is declared as Integer.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Chip,

Again, thank you for taking time with this. Would you field one more
question for me?

Perhaps the error is generated immediately after the function
calculates, and then returns to the main subroutine?

It looks like this:

mt(0) = (s And &HFFFFFFFF)
For mti = 1 To N - 1
mt(mti) = 1812433253 * (mt(mti - 1) Xor (RShift(mt(mti - 1), 30)) +
mti)
Next mti
mt(mti) = &HFFFFFFFF

Best regards,

Nick
 
The XOR operator can only handle signed Longs, and your
expression 'mt(mti - 1)' is greater than the maximum value of a
Long (I get 1.33E+18). If you are using XOR, you are limited to
Longs.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Chip,

Thanks again. So this is the point at which overflow occurs.

Would you be able to provide me with any references to a workaround? Or
any tips to accomplish this operation?

Best,

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top