Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'OverFlow' error with string conversion. 2

Status
Not open for further replies.

robdon

Programmer
May 21, 2001
252
ES
Hi,

Very occasionally I get a runtime error, 'Overflow' from the following statement.

strTime = "Total Load Time:" + Str$(timeGetTime - stTot)

I am setting stTot to timeGetTime at the beginning of an operation, to time something, and its a global DIM as a Long.

The DEFINE for the timeGetTime is:

Public Declare Function timeGetTime Lib "winmm.dll" () As Long

I cant understand why this line would get an Overflow, since everything is 'Long'.

The operation normally takes around 10 secounds, so the difference (ie timeGetTime - stTot) should only be a small number anyway.

Thanks,

Rob D.

ProIV Resource Centre
 
>since everything is 'Long'

Well - not really ...

The problem is that timeGetTime actually returns A DWORD which is an unsigned long, whilst VB's Long type is signed.

Here's a hack: declare the everything as Currency, including the API call and multiply the results by 10000

 
To elaborate it further...

When the result of timeGetTime function returns a value less than &H80000000, VB treats the result as a positive long integer.

Range for positive numbers (including 0) is 0 to &H7FFFFFFF (2147483647).

But as the result for this unsigned DWORD crosses this value, VB treats the resultant number as negative, because the MSB is now set.

Range for negative numbers is &H80000000 (-2147483648) to &HFFFFFFFF (-1).

Transition from &H7FFFFFFF to &H80000000 for DWORD (unsigned long) is smooth, i.e. the difference between these two numbers is simply 1 (&H80000000 - &H7FFFFFFF = 1).

But for signed numbers, the story is different.

&H80000000 - &H7FFFFFFF = -2147483648 - 2147483647 = -4294967295

As you see, the result of subtraction is not only wrong, but also too large to fit in a VB Long integer, thus giving the overflow error.

Therefore, this situation will only occur if your operation starts at timeGetTime < &H80000000 and ends at timeGetTime >= &H80000000. This critical value (&H80000000 milliseconds) corresponds to 24 days 20 hours 31 minutes 23 seconds. This means you will only, very occasionally, encounter this error, if your operation started before this duration and ended after this duration since the Windows was started.

So the workaround is to use the Currency hack as strongm suggested.

Another option is to convert your signed values to unsigned values and perform the subtraction afterwards, as shown below.
___
[tt]
Dim dStart As Double, dEnd As Double, strTime As String

dStart = timeGetTime
'operation goes here...
dEnd = timeGetTime

'convert signed values to unsigned
If dStart < 0 Then dStart = dStart + 2 ^ 32
If dEnd < 0 Then dEnd = dEnd + 2 ^ 32

'subtract
strTime = "Total Load Time: " & (dEnd - dStart)[/tt]
 
Why not just use DateDiff()?

-David
2006 & 2007 Microsoft Most Valuable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
to quote:

The operation normally takes around 10 secounds, so the difference (ie timeGetTime - stTot) should only be a small number anyway.

-David
2006 & 2007 Microsoft Most Valuable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
Yep, I need to be more acurate than 1 second, in some instances.

I use TimeGetTime() alot to do timings.

Thanks,

Rob.

ProIV Resource Centre
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top