VB math prob

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henning

    #1

    VB math prob

    Hi grp
    What the ... is this. Have I missed something?

    Dim x as Long

    x = 192 * 256 '** overflow error ???

    x = &HC000 '** works ok

    The value is the same in both cases.

    /Henning


    --
    Time is present only to prevent everything from happening at once.
    Still it seems that everything happens at once.
    Then there must be a bug in time.
    To find the bug in time, isn't that what we all hope for.


  • Richard
    New Member
    • Nov 2005
    • 2

    #2
    VB math prob

    Tried this in Excel VBA and the same thing happens. Having looked at the help I quote:-

    [COLOR=DarkRed]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[/COLOR]

    It does work.

    Comment

    • Raoul Watson

      #3
      Re: VB math prob


      "Henning" <computer_hero@ coldmail.com> wrote in message
      news:436fdee6$0 $98292$57c3e1d3 @news3.bahnhof. se...[color=blue]
      > Hi grp
      > What the ... is this. Have I missed something?
      >
      > Dim x as Long
      >
      > x = 192 * 256 '** overflow error ???
      >
      > x = &HC000 '** works ok
      >
      > The value is the same in both cases.
      >[/color]

      No they are not the same. Had you done x = 49152 it would have worked as
      well.

      The problem is not the target (x as long) but rather an intermediary result.

      Unless specified, the intermediate result in VB will be INT and as we all
      know it, anything greater than 32767 would exceed int limit.

      So, this will generate an error

      dim a as integer
      dim b as integer
      dim x as long
      a = 192
      b = 256
      x = a * b

      Whereas this will not
      dim a as long
      dim b as long
      dim x as long
      a = 192
      b = 256
      x = a * b


      Comment

      Working...