Qbasicnews.com
September 20, 2021, 02:52:01 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Very, very, very weird error  (Read 1981 times)
paleozord
Member
*
Posts: 57


« on: June 26, 2006, 09:53:37 PM »

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]
Logged
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #1 on: June 26, 2006, 10:57:17 PM »

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
Logged
paleozord
Member
*
Posts: 57


« Reply #2 on: June 26, 2006, 11:35:44 PM »

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!  Shocked

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.
Logged
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #3 on: June 27, 2006, 09:29:20 AM »

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 &.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #4 on: June 27, 2006, 08:37:42 PM »

Quote from: "paleozord"

......
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


*****
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!