Odd stuff - Overflow Error

RichO

Registered Yoozer
Local time
Today, 17:28
Joined
Jan 14, 2004
Messages
1,036
This has got me scratching my head. I have a calculation going on in a module that results in an overflow error. The calculation ends up multiplying a varable (value of 12) by 10000, resulting in 120000.

If I go:

Dim X As Long
X = 120000

No problem. But...

Dim X as Long
X = 12 * 10000

gives me an overflow error.

Msgbox 12 * 10000
also gives me the same error.

Me.Text2 = Me.Text0.Value * 10000
With a value of 12 in Text0 works just fine.

I've tried using Double as the variable type but still the same error.

Any ideas why this is happening? :confused:

Thanks
 
Last edited:
It's looking at two integers and producing a long. You either need to put one of them in a variable, or typedef one.


Any of these will work:
Code:
clng(12) * 10000
c = 12 * 10000

a = 12              'a must be defined as variant or larger than integer
c = a * 10000

b = 10000         'b must be defined as variant larger than integer
c = 12 * b

a = 12              'one of the two must be larger than the integer
b = 10000
c = a * b
 
Last edited:
Overflow (Error 6)

An overflow results when you try to make an assignment that exceeds the limitations of the target of the assignment. This error has the following causes and solutions:

...

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

My guess is that is has to do with the entire statement

x = 12 * 10000

because

Code:
Function tst()

Dim x As Long
x = 12
x = x * 10000
Debug.Print x

End Function

works just fine.

If I remember from my assembler days (long, long ago and a lot nearer than I like to contemplate), the code had to load a register with a number, multiply that number by some other number (memory address), and store the result in another address (at least three statements).

If the assembly code did not specifically make a call to the decimal floating point package before the multiplication operation, integers were implicit (and overflows common) because registers were at most 16 bits long (31 bits using two math registers & 32 bit words with the most significant bit for +/-).

I can almost remember using the register overflow error for the crude basis of a decimal floating point package, but I'm really pushing hard here...

Now it's just "x = 12 * 10000".

I'd guess that when you ask the processor to evaluate that statement, it evaluates "x = 12" before it sees the "* 10000" part, and assumes since the "assignment" part was with an integer, the rest of the statement will also be using integers...

IOW: I don't really know :D

but I think it's something like that...
 
Last edited:
OK that explains it. The variable I was multiplying by 10000 was an integer type.

Thanks for the help.
 
OK that explains it. The variable I was multiplying by 10000 was an integer type.

I think so...

Type in:

Function tst()
Dim X as Long
X = 12.0 * 10000
Debug.Print X
End Function

And watch what happens to the code after you run it. It should change to:

Function tst()
Dim X as Long
X = 12# * 10000
Debug.Print X
End Function
 
Yes, that's what happens. I thought # was to designate date data types.
 
DALeffler said:
I'd guess that when you ask the processor to evaluate that statement, it evaluates "x = 12" before it sees the "* 10000" part, and assumes since the "assignment" part was with an integer, the rest of the statement will also be using integers...
Did you not see my post?


BOTH values in the equation were of type integer, c was expecting an integer, but it got a double, which means overflow.


------------------------------
Example:
When you define variable "c" (Dim c As Variant) then c becomes a double when it is the result of a Single * Long

otherwise, when c overflows:
if c is long/single/date variant it becomes double variant
if c is a byte variant then it becomes an integer variant
if c is an integer variant then it becomes a long variant
------------------------------

In any case, if 12 or 10000 was put in a variant, you would have no problem.
 
Much like the help file says:
Considering RESULT = NUMBER1 * NUMBER2
The data type of RESULT is usually the same as that of the most precise expression. The order of precision, from least to most precise, is Byte, Integer, Long, Single, Currency, Double, and Decimal.

so:
Integer * Integer would make RESULT an Integer
Long * Integer would make RESULT a long
Long * Decimal would make RESULT a decimal
 
Apologies. You're right: I don't know what I was thinking...
 
No, it's sort of like your first post and much like C, or even assembly, as you may have said.

I just thought that I answered it well enough the first time.. and that sort of got disregarded.
 

Users who are viewing this thread

Back
Top Bottom