CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2001
    Posts
    47

    Why does overflow!?

    This is bugging the hell out of me!

    Does someone have any idea why this is chunk of code is overflowing on the 5th line!? If I remove the '1800' the overflow shifts to line 7.


    iCellFrames = iCellFrames + iShiftFrames
    iHours = Int(((iCellFrames / 60) / 60) / 30)
    iCellFrames = iCellFrames - (108000 * iHours)
    iMinutes = Int((iCellFrames / 60) / 30)
    iCellFrames = iCellFrames - (1800 * iMinutes)
    iSeconds = Int(iCellFrames / 30)
    iCellFrames = iCellFrames - (30 * iSeconds)




    I used several MsgBox inserts to check the values as it was stepping through and everything was right where it should be! I don't understand why I am getting overflow on such a horribly simple math problem!!

    iCellFrames and iShiftFrames are 'Long'
    iHours, iMinutes, iSeconds are 'Integer'

    Many thanks for any help.

    Hoag


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Why does overflow!?

    What are the values of iCellFrames and IshiftFrames at entry to the routine?

    I would put a

    Debug.print iCellFrames - (1800 * iMinutes)



    just before the failing instruction to see what is going on.
    The computed value is out side the range of a Long.

    John G

  3. #3
    Join Date
    Jun 2001
    Posts
    47

    Re: Why does overflow!?

    but that shouldn't be possible... being outside the range of a Long.

    iCellFrames is a Long and is perfectly fine up until that point. I am then taking that value and *subtracting* from it (and it does remain above 0). Unless VB requires a Long to be above a certain number (which would be just plain stupid), this statement should go out of any bounds.

    Here are a list of variable values as the block exicutes for a given value...

    line 1 (iCellFrames): 154888
    line 2 (iHours): 1
    line 3 (iCellFrames): 46888
    line 4 (iMinutes): 26



  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Why does overflow!?

    If you give me the starting values requested, I will run the routine on my computer and check out the results.


    John G

  5. #5
    Join Date
    Jun 2001
    Posts
    47

    Re: Why does overflow!?

    you can set iShiftFrames to 108000 and iCellFrames to 46888 -- that will burn it. You can also set iShiftFrames to 1, and it will kill it as well.


  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Why does overflow!?

    '
    (1800 * iMinutes) is producing your error.
    Change iMinutes to a long and your problem will go away.
    1800 * 26 = 46800 which exceeds the range of an Integer

    John G

  7. #7
    Join Date
    Jun 2001
    Posts
    47

    Re: Why does overflow!?

    Sure enough, many thanks for the pointer!

    Although I'm ont clear as to why VB is trying to put (1800 * iMinutes) into an Integer value, and not just doing the math. But it works now, and I know another quirk of VB... so thanks again!

    Nick


  8. #8
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Why does overflow!?

    Well 1800 will fit into an integer. An integer * integer results in an integer value also. This is why the overflow error. A long * integer results in a long. So in order to calculate this equation, either the 1800 must be declared as a long or iMinutes must be declared as a long. Since iMinutes has already be declared as an integer, the proper correction would be to type 1800 as a long. To do this you just add the type declaration character. & denotes a long, so you would put (1800& * iMinutes).

    David Paulson


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured