Qbasicnews.com

Full Version: Very, very, very weird error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okay, maybe I've somehow been programming in QB for all these years without ever being aware of this quirk, but.. I try this code:

Code:
Dim a as long
a=69697 * 60

It works fine. Now I try this:

Code:
Dim a as long
a= 999 * 60

And I get an "overflow" error. Extremely bizarre. The number in the first example is bigger than the one in the second, yet the second causes an overflow erroe even thouugh the value is well within the range of a long integer.

Does anyone know what the hell is going on? QuickBasic 7.1 and VBDOS 1.0 both cause this error, and I presume QuickBasic 4.5 would as well.[/code]
This is actually normal behavior for QB regarding immediate values. If one of the operands is not a variable, you need to add a type suffix to "help" QB:

Code:
Dim a as long
a = 999& * 60
Wow, I don't know what's more strange. The fact that QB does that, or the fact that I've never run into it before after well-nigh two decades of QB programming! :o

I did a further test and found that QB balks if you attempt to use a number small enough to fall within integer range (32,767 or lower) in a calculation for a Long integer variable's value, but behaves fine if you make it even a tiny bit larger (32,768 or more).

Weird. You'd think as a computer language that it could just handle real numbers perfectly without any help. Anyway thanks for the pointer. Shows there are still QB quirks I don't know about even after all these many many years! :wink:

Much appreciated.
The larger numbers are automatically of type LONG, because they wouldn't fit in an INTEGER; therefore, you don't have to explicitly write the &.
Quote:......
Code:
Dim a as long
a=69697 * 60

It works fine. Now I try this:

Code:
Dim a as long
a= 999 * 60

And I get an "overflow" error.......
Yes, I've seen this kind of problem before. It's a Basic "feature". You were lucky to get an overflow error whch tells you something was wrong. I tried the second one with QuickBASIC 4.5, and got a negative result.

The first one works because 69697 is greater than an integer and is treated as the next higher precision above integer.

The second one won't work because both 999 and 60 are integers, BUT the product of the two of them will not fit into an integer. The compiler doesn't look ahead to see that the result is going to be assigned to a long variable. All it knows is that it was given two integers to multiply where the result doesn't fit into an integer. So it just does the best that it can, which in this case is not what we expect.

When using explicit values like this, the safest way is to do one of the following methods:

1) Append the type (%,& ! #) that corresponds to the varable type of the result to each explicit value.
Code:
Dim a as long
a= 999& * 60&

2) Create a variable using DIM with the type that corresponds to the result of the expression where it will be used, and then set this variable to the value.
Code:
Dim a as long
Dim v1 as long
Dim v2 as long
v1 = 999
v2 = 60
a= v1 * v2

*****