why does this overflow? (1 Viewer)

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:38
Joined
Sep 12, 2006
Messages
15,662
out of interest, why would this overflow? Is there an implicit integer conversion going on?

Code:
Sub show()
Dim l As Long

    l = 256 * 256
[COLOR="Red"]   ' note
   ' l = 256
   ' l= l * 256  'does not overflow[/COLOR]    
   MsgBox (CStr(l))

End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 12:38
Joined
Aug 30, 2003
Messages
36,128
According to help, yes. It recommends

l = CLng(256) * 256

which works for me (your original code also failed for me).
 
Last edited:

MarkK

bit cruncher
Local time
Today, 12:38
Joined
Mar 17, 2004
Messages
8,186
Yeah, this too ...
Code:
? 32767+1
My bet is that it's integer math whose result exceeds the upper limit of the datatype. It doesn't matter that the assignment is made to a long because the overflow happens before that.
And if you assign to a long variable first it's not integer math anymore.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:38
Joined
Sep 12, 2006
Messages
15,662
thanks for the thoughts.

It was a curious result.
 

DJkarl

Registered User.
Local time
Today, 14:38
Joined
Mar 16, 2007
Messages
1,028
VBA in not too bright when it comes to math. It sees two integers and even though you are putting the results into a long, it is trying to do integer math. If you use Clng or preface your numbers with a & symbol it will not give an overflow error, also if you picked a number outside the integer datatype range it would also work, 32768 * 256 for example would not generate an overflow error.
 

datAdrenaline

AWF VIP
Local time
Today, 14:38
Joined
Jun 23, 2008
Messages
697
This has to do with the type of the operands. VBA will imply the datatype of numerics into an Integer, Long, or Double if no datatype is specified. When you perform operations upon the operands, VBA will assume the result to be of a datatype that matches the "heaviest" datatype.

So ...

? VarType(256)
2 'Integer

? 256 * 256 => VBA sees Integer * Integer, thus assumes the result to be Integer and allocates accordingly

? VarType(15*15)
2 'Integer

----

You will get the same error raised with ...

? 32768 * 32768 * 32768
'Overflow

? VarType(32768)
3 'Long Integer

So VBA expects the result to be Long Integer as well.

----

What I have done to force VBA to think like me, without calling one of the coercion functions is to declare the type of the literal ...

? 256& * 256& '& is a type declaration character for Long Integer
65536

Or if you are good with a Double you can just add the decimal ...

? 256.0 * 256

But if you write your VBA that way, the VBA editor will change 256.0 to 256#

Another solution is to coerce all of your operands to a Variant/SubType, then VBA will choose the appropriate datatype to fit the result ..

? CVar(256) * CVar(256)
65536

? VarType(CVar(256) * CVar(256))
3 'Long Integer

Also, note that not all operators use the "result datatype must match the 'heaviest' operand datatype" philosophy. I beleive it is just addition, subtraction, and multiplication. Division and raising to a power always return Double.
 

MarkK

bit cruncher
Local time
Today, 12:38
Joined
Mar 17, 2004
Messages
8,186
As an aside, are you familliar with the typename() function ...
Code:
? typename(CreateObject("Scripting.FileSystemObject"))
FileSystemObject

? typename(1&)
Long
... which returns the name of the type?
Mark
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:38
Joined
Feb 28, 2001
Messages
27,231
To my understanding, admittedly limited,...

The rule is that the expression analyzer tries to generate its result in a variable of the longest type in the line unless there is clearly some indicator of the desired result.

l = 255 + 1 'can be done with two byte integers on the right-hand side, so allocates a byte - that promptly overflows.

l = 256 + 1 ' works because 256 clearly does not fit into a byte so a word is allocated.

l = 32767 + 1 'can be done with a word integer and a byte on the RHS, so allocates a word - that prompt overflows.

l = 32768 + 1 'works because 32768 requires a long integer and a byte on the RHS.

I recall seeing this rule in print a long time ago but it would take me a while to remember where I saw it. I'll poke around, but if anyone wants to look elsewhere, I think the topic is simply called "expression evaluation."
 

datAdrenaline

AWF VIP
Local time
Today, 14:38
Joined
Jun 23, 2008
Messages
697
Hello Doc! ...

Just a point of note ..

l = 255 + 1

Will not raise the overflow error because literals are cast as 2 byte integers or higher.

However, to illustrate you point, the following WILL raise an overflow error...

l = CByte(255) + CByte(1)
 

Users who are viewing this thread

Top Bottom