What am I missing about LONG integers?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
jejump
Coder
Posts: 11
Joined: Wed Jun 25, 2014 7:54 am

What am I missing about LONG integers?

Post by jejump »

Hello everyone. I'm brand new to the site and could use a little help with a programming problem.

Help me understand something... I'm booted up with WIN98SE command prompt environment and running Quick Basic 7.1.

I try this code and get an Overflow error:

DIM IntegerNumber AS LONG
IntegerNumber = 128 * 256 '(The answer is 32,768. Should easily fit within LONG type)
LOCATE 1,1
PRINT IntegerNumber
END


If I change the multiplication to 127 *256, that works fine. If I replace the multiplication with IntegerNumber = 32768 (or larger) that's okay too. :shock: This happens not only with fixed values, but variable multiplication as well.

Comments?

Thanks,

John
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: What am I missing about LONG integers?

Post by burger2227 »

It works fine in QB64 as it does not have such overflow problems. Try this:

Code: Select all

DEFLNG A-Z 'make all variable values LONG
x = 128
y = 256

IntegerNum = x * y

Print IntegerNum

END
In multiplication one of the values must match the result. 32768 is a negative INTEGER value.

It will also work if you add the LONG suffix to one of the number values like 128& * 256
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
jejump
Coder
Posts: 11
Joined: Wed Jun 25, 2014 7:54 am

Re: What am I missing about LONG integers?

Post by jejump »

Thanks for clearing that up burger.

What I'm trying to do more specifically is compile a 24-bit integer from three 8-bit values. This is not the first time that I've needed to do this and have encountered this same problem. What I've seen in both occasions is that QBX will multiply the highest byte by 65536 with never an error, but multiplying the middle byte by 256 will result in overflow ONLY IF the middle multiplicand exceeds 127. The fix for the first project was to first multiply the middle byte by &H10000, then divide it by &H100 at the end of the arithmetic. This time around, I thought to ask the internet "What is going on?". Amazingly, I couldn't find much discussion on it.

So following your enlightenment, I am now multiplying my middle byte by &H100& instead of &H100. That seems to have fixed it!

Thanks again for the help.

John
jejump
Coder
Posts: 11
Joined: Wed Jun 25, 2014 7:54 am

Re: What am I missing about LONG integers?

Post by jejump »

Oh BTW,

I have downloaded QB64 and FreeBasic and have dabbled with both, but never taken on any large scale stuff. I use QBX and MASM to create dedicated controllers for various types of machinery. I heavily make use of PEEK, POKE, IN, and OUT statements--all of which unavailable to me on the aforementioned compilers. PLUS, nothing gets off the ground, up and running faster than a 16 bit OS and program running at modern processor speeds. :D

Jj
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: What am I missing about LONG integers?

Post by burger2227 »

Well I have 3 XP machines and one 98 SE, but the days of QB are numbered. Especially on 64 bit. I was considering changing one XP to a Windows 7, 32 bit, but I haven't found much success running QB on the NTVDM it supposedly has. I have one XP that looks like it may not last much longer...
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
jejump
Coder
Posts: 11
Joined: Wed Jun 25, 2014 7:54 am

Re: What am I missing about LONG integers?

Post by jejump »

Yes, compatible hardware availability will inevitably be the demise of the language. I guess when I can no longer obtain motherboards outfitted with PCI slots, serial ports, and parallel ports, I'll have to outfit any new electromechanical projects I take on with expensive PLCs, HMIs and write code in ladder. That'll be a sad day for me :(
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: What am I missing about LONG integers?

Post by burger2227 »

Well INP and OUT may work in with COM and LPT ports in QB64 using a special LIBRARY:

http://www.qb64.net/wiki/index.php/Port ... _Libraries" target="_blank

You will need to download the DLL file used by VB.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply