Results 1 to 4 of 4

Thread: Large Numbers

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Location
    Adelaide, Australia
    Posts
    24

    Angry

    Hi, I'm writing an application in which I need to log the start and end times of an event and the subsequent difference in time time measured in seconds. I have used text boxes for the times (1 for hour, 1 for min etc). Then when all is completed, I check all the boxes and work out the times.
    Ideally I just take the start units from the end, and if the result is less than zero, ie starts at 23:58:01 and ends at 00:12:34 then I effectively add 24hours to get the number. However, this doesn't even come into question because unless there is only a few mins difference and the start hour is less than the end, then I get overflow errors.

    **
    My assumption was that "integer" was < 32k (two bytes) and that "long" was <2b (four bytes).

    If this is the case, why do I get overflow errors when a "long" variable is assigned to 86k ??
    If long is not 4 bytes, how do deal with decently large numbers ? Does VB have an unsigned long or some other way of manipulating large numbers.


    Code Extract:
    lngSum = ((CLng(txtFH) - CLng(txtSH)) * 3600) + ((CLng(txtFM) - CLng(txtSM)) * 60) + (CLng(txtFS) - CLng(txtSS))
    lngDay = 24 * 3600
    If lngSum < 0 Then
    lngTotal = lngDay + lngSum
    Else
    lngTotal = lngSum
    End If

    MsgBox lngTotal 'Only here as a debug


    Thanks

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    perhaps Double would do it...?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Addicted Member GungaDin's Avatar
    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    146
    VB seems to get a bit confused about things like this.

    You'll notice that if instead of lngDay = 24 * 3600, you do lngDay = 86400 or lngDay = Clng(24) * Clng(3600) you won't get errors.


    [From Microsoft Help. Explanation of error]

    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
    [End of Microsoft Help]


    I think this basically means that VB does the multiplication and tries to store the result in an integer as both 2000 & 365 are integers. The conversion to a Long is not applied until after the multiplication has occurred. That's why setting one of the numbers to a Long will make VB store it in a long and hence remove your problems.

    Something like that anyway.


    Hope this helps,

    Nathan Liebke

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Location
    Adelaide, Australia
    Posts
    24

    Wink Solved !

    Thanks muchly, didn't think of that.

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