VBA promoting wtf's



  • I doing some VBA work and have just spent a few hours trying to track down an "overflow error" that was crashing an app I'm in the middle of modifying. Its been w while since I did any serious VBA work, and I'm tired .. so I'm kicking myself for missing something that amounts to the following:

    dim angle as double
    dim fraction as double
    dim index as integer

    index = 92
    fraction = 100.0
    angle = index * 360 * fraction / 200#

    Do you see it?

    I totally missed it and it was hard to pick up as as I could't get a debugger in on the actual code. After I had exhausted all other avenues and just printed out everything to an error log to see where the failure was occurring I only found it by mistake when I wanted to verify how VBA treated the # symbol - which should mark the number as a double. The top of my google search was INFO: How VB Interprets Numbers, Constants and Numeric Types. The bit from here that made em cry was:

    Visual Basic does not evaluate the entire expression to check for the size of the result, but instead continues to use a 2 byte temporary space for the calculation.
    In other words it does't know that it needs to promote types until it comes across a type different from what it had been using. Thus my puzzlement at why "angle" was causing an overflow is that 92 * 360 is greater than what a 2 byte int can hold.

    What a way to end a long tiring day.


  • @OzPeter said:

    I doing some VBA work and ...



  • Yes, it is hard to disagree witht he "ha ha" here.

    But anyway, waiting for a moment about that. When was VBA created? Ages ago 16 bits was a sane default size for integers, did VBA exist by the late 80's?



  • @Mcoder said:

    Yes, it is hard to disagree witht he "ha ha" here.

    Say what you want about the technology .. but the only "ha ha" I'm hearing is the laughing all the way to bank due to my full contract rates .. including the time spent on this WTF

    BTW you missed my point .. its not the integer size that I am really complaining about, but the mixed type calculation that lazily promoted to a double type rather than jump straight there.



  • @OzPeter said:

    the only "ha ha" I'm hearing is the laughing all the way to bank due to my full contract rates


    also the bankers are the one doing the real laughing


  • @OzPeter said:

    BTW you missed my point .. its not the integer size that I am really complaining about, but the mixed type calculation that lazily promoted to a double type rather than jump straight there.

    In which language is this not the case?



  • @Mcoder said:

    Ages ago 16 bits was a sane default size for integers, did VBA exist by the late 80's?
    I think it traces it's legacy back to some 16-bit version of Excel, and then later versions kept integer as 16-bit for backwards compatibility.



  • What result would you get from
    angle = fraction * 360 * index / 200#
    ?



  • @Medezark said:

    What result would you get from
    angle = fraction * 360 * index / 200#
    ?

    You get the answer that all my years of experience trained me to expect. And thats' what I basically did to solve the issue.



  • @ender said:

    @Mcoder said:
    Ages ago 16 bits was a sane default size for integers, did VBA exist by the late 80's?
    I think it traces it's legacy back to some 16-bit version of Excel, and then later versions kept integer as 16-bit for backwards compatibility.
    I'm quite happy with 16 bit ints as that was how they always were for me back in the day and actually a lot of stuff I am working on still has 16 bit ints (these 32 bit ints are way too big). And obviously VBA is still floating around!



  • Man I grew up on Mac Classic on 68k CPUs. You can take my 16-bit ints from my cold dead fingers! And my 24-bit pointers!


  • BINNED

    @immibis said:

    @OzPeter said:
    BTW you missed my point .. its not the integer size that I am really complaining about, but the mixed type calculation that lazily promoted to a double type rather than jump straight there.

    In which language is this not the case?

    I can think of a couple of languages offhand that wouldn't even allow the mixed type calculation.



  • @PedanticCurmudgeon said:

    @immibis said:
    @OzPeter said:
    BTW you missed my point .. its not the integer size that I am really complaining about, but the mixed type calculation that lazily promoted to a double type rather than jump straight there.

    In which language is this not the case?

    I can think of a couple of languages offhand that wouldn't even allow the mixed type calculation.
    Go does this, and I like it. No compiler warnings, just errors. If it’s worth mentioning, it’s worth failing the build for.


Log in to reply