Qbasicnews.com
February 23, 2020, 07:57:36 AM
 Pages: [1] 2
 Author Topic: oVERFLOW  (Read 9765 times)
Ralph
Ancient Guru

Posts: 544

 « on: October 07, 2008, 07:24:30 PM »

I'm having a problem understanding what is going on and how to correct my program.  When I run it, it gives me an OVERFLOW errror at line
A = 10 * 10 * 10 * 10 * 10
as though A had been defined as an integer.  Why is this, and how do I correct it?

Quote
'aaatest2
DEFLNG A-Z
CLS
'multiplication
R = 10 ^ 6
T1 = TIMER
FOR i = 1 TO R
A = 10 * 10 * 10 * 10 * 10
NEXT i
PRINT TIMER - T1

SYSTEM
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Ralph
Ancient Guru

Posts: 544

 « Reply #1 on: October 13, 2008, 02:51:36 PM »

After getting no answer to my question, today, I went back to my problem.  After somefiddling around, I found a solution, as follows:
A = 10& * 10 * 10 * 10 * 10 * 10
By just using one factor expressed as LONG by the use of the LONG symbol, &, I got rid of the OVERFLOW error.  The & can be used with any of the factors, but must be used before the multiplication exceeds the limit for integers, in the prsest case, on any ofthe first four factors (10,000).  So, it seems that QB does the multiplication in the order that the factors are presented, and, if it exceeds the integer limit BEFORE it reaches a LONG factor, it produces the OVERFLOW error.
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Clippy
Member

Posts: 84

 « Reply #2 on: October 13, 2008, 07:22:18 PM »

Confused there Ralph. Why were you using Single when Double or Long was what you needed in the first place?

By the code, A is DEF Single instead of a Long or Double value. Thankfully Single can use one million. But if you tried 10 ^ 6, then QB defines the result of exponentation as either Single or Double values. As explained recently by Ildurest on the N54 forum.

Ted
 Logged

Moneo
Na_th_an

Posts: 1971

 « Reply #3 on: October 13, 2008, 09:15:58 PM »

I'm having a problem understanding what is going on and how to correct my program.  When I run it, it gives me an OVERFLOW errror at line
A = 10 * 10 * 10 * 10 * 10
as though A had been defined as an integer.  Why is this, and how do I correct it?

Quote
'aaatest2
DEFLNG A-Z
CLS
'multiplication
R = 10 ^ 6
T1 = TIMER
FOR i = 1 TO R
A = 10 * 10 * 10 * 10 * 10
NEXT i
PRINT TIMER - T1

SYSTEM

Hi Ralph, don't feel bad, many of us have fallen into the same trap.
As you probably already figured out, the problem is the "tens" are set to integer by default.

Take a look at the Qbasic Help, in the section about "Type Conversion". This explains your problem, although not very clearly. You need to read it several times.

Regards..... Moneo
 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #4 on: October 14, 2008, 02:13:49 AM »

Ted:  I used DEFLNG, not DEFSNG, but, thanks for your comment.

Moneo:  I read the QB Help part on data types, so I understand how I was able to correct the answer and stop the error.  Your stating that QB automatically assigns SINGLE to any number explained even better why I had to assign a DOUBLE symbol to at least one of the constants.  Thanks for posting!
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Ralph
Ancient Guru

Posts: 544

 « Reply #5 on: October 14, 2008, 03:17:31 PM »

Code:
[code]
In thie program below, I get no OVERFLOW error on lines 3 and 4, but I do on line 5.  I would have thought that the line "a = 1000", since 1000 is automatically assigned data type SINGLE, would also assigne SINGLE status to a.  In that case, lines 3 and 4 would have given me the OVERFLOW error; but, since I didn't get that error, it seemed to me that "a" remains as defined, a LONG data typee.  That would explain why neither lines 3 nor 4 give an OVERFLOW.  Then, I added the line, "PRINT LEN(a)" just before line 2, and also just after line 2; and got the length of "a" as 4 both times, confirming that it remains a LONG data type, even though line 2 might have converted it to a SINGLE data type, I would have thought, in the sake of memory economy.  Well, even Microsoft isn;t logical all the time, I guess.   Inow  see the heavy use of the symbols %, &, !, and # as a cop-out to not have to remember all the rules about what happens to variable data types throughout mathematical operations!  Do you agree with me?

[code]
1 DEFLNG A-Z
2 a = 1000
3 b = a * a
4 b = 1000 * a
5 b = 1000 * 1000
[/code]

Edited some time later, by Ralph:
But, wait a while!  I just ran the above program, after removing line 1, and, I still get the length of a as 4!  And the same OVERFLOW for line 5.  Why?

The following program runs fine, but, if I reduce the first value in line 7 from 1,000,000,000 to 100,000,000, I get an OVERFLOW in this line.  It would seem that one billion is either a  LONG ineger or a DOUBLE precision variable, even though itis not so indicated, and that one hundred million is acting as either a "SHORT" integer or as a SINGLE precision variable.
Code:
1 CLS
2 PRINT LEN(a)
3 a = 100000
4 PRINT LEN(a)
5 b = a * a
6 b = 100000 * a
7 b = 1 000 000 000 * 1000000

So, is my above analysis correct?  If not, what is the explanation?  And, why is it that one hundred billion is not shown with its LONG or DOUBLE precision symbol?  Explanations, anyone?  [/code]
 « Last Edit: October 14, 2008, 03:51:49 PM by Ralph » Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Moneo
Na_th_an

Posts: 1971

 « Reply #6 on: October 14, 2008, 10:04:07 PM »

RALPH.
I pasted a few of your posts together so I can answer them all at once.

-----------------------------------------------------------------------
You wrote:
Moneo:  I read the QB Help part on data types, so I understand how I was able to correct the answer and stop the error.  Your stating that QB automatically assigns SINGLE to any number explained even better why I had to assign a DOUBLE symbol to at least one of the constants.

RALPH. I said that the constants of tens will be automatically converted to INTEGER,
because, being less than 32767, the value of 10 fits into an INTEGER.
If the value is greater than 32767, it will automatically be converted to LONG.

Had you used a number like 41234, this would be automatically be converted to LONG,
because it's greater than 32767 and less than 2147483647.

By the same token, if you had used a number like 12.34, it would automatically be converted to SINGLE, beause it needs single precision notation to express this decimal value.

Note: if the data type of the variable into which these data constants are being assigned is smaller that the data type needed, then the QuickBASIC compiler will issue a math overflow error.

Example: DEFINT A: A=33767.

--------------------------------------------------------------------------------
RALPH, YOU SAID:
1 DEFLNG A-Z
2 a = 1000
3 b = a * a
4 b = 1000 * a
5 b = 1000 * 1000

But, wait a while!  I just ran the above program, after removing line 1, and, I still get the length of a as 4!  And the same OVERFLOW for line 5.  Why?

Something is weird here. On QuicBASIC compiled, without removing line 1, I don't get an overflow on line 5. However, the answer is mathematically wrong. I suspect that it has to do with two numeric constants on the same line. Very strange!

Sorry, but I didn't have enough time to check the test with a billion.

One last question of yours: Why is it that one hundred billion is not shown with its LONG or DOUBLE precision symbol?

The data type suffix of a variable will never be modified by the compiler, regardless of what value you assign to it.

Regards..... Moneo

 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #7 on: October 15, 2008, 12:02:46 AM »

Moneo:

Thank you for your time and for your very good explanations.  As I am retired, I have a lot of time on my hands.

You wrote:
Quote
One last question of yours: Why is it that one hundred billion is not shown with its LONG or DOUBLE precision symbol?
The data type suffix of a variable will never be modified by the compiler, regardless of what value you assign to it.

Moneo, I don't quite understand your answer.  Let me rephrase  my questsion on this, as follows:
I entered two line:
a = 1000000000    '=10^9
b = 10000000000  '=10^10

QB left the first line as is, but changed the second line to:
b = 10000000000#
indicating a double precision number (not a long integer, by the way!) In fact, all lines with the second side = 1, followed by more than 9 zeroes, got the # symbol added, and none less than that did.
So, my question is, why doesn't QB add the # symbol to tany non-decimal number I enter that has more than the seven (or is it eight?) digits allowed for a SINGLE precision?  By the way, using LEN() gives 4 for both a and b, indicating either SINGLE precision or LONG integer, yet the symbol # is for DOUBLE precision???

But, I am starting to believe what I said earlier, that QB isn't 100% logical, so, perhaps it's better to just forget my question, or to consider it as trivial.

 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Clippy
Member

Posts: 84

 « Reply #8 on: October 15, 2008, 02:15:03 PM »

Qbasic always uses # when it convers large numbers! I have NEVER seen any other symbol added when entering values in the IDE.

Moneo does NOT use the IDE to program, so he has never seen the IDE change phenomina as he compiles code directly.

As to why only Double? How is QB supposed to know what type of value it is?
It only knows that the value is greater than 7 digit Singles. You have to tell it what you want by converting your answers somehow. Your routine defined all variables as Long, so LEN = 4.

Ralph we just went through this with the abcd * efg = hi ^ j equations. Your subtraction method with ABS() kept the numbers from overflowing.

As to why it waits until 10 digits, perhaps because Long can only be a low 10th number of 2.

Ted
 « Last Edit: October 15, 2008, 02:53:00 PM by Clippy » Logged

Ralph
Ancient Guru

Posts: 544

 « Reply #9 on: October 15, 2008, 06:30:58 PM »

Ted:

You did it! I went to the Help, Contents, Data Types, and I found:
<Long Integers>  &   2,147,483,647   to  -2,147,483,648
I counted the number of digits, and ... you guessd it, 10!  So, as soon as I have more than the value 2,145,48,647, which 10,000,000,000 is, it goes to the next possible representation, the Double.  Mystery clarified, thanks to you!
 Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Moneo
Na_th_an

Posts: 1971

 « Reply #10 on: October 15, 2008, 08:34:49 PM »

Ralph,

Your posts have the following line at the end:
"Ralph, using QuickBASIC 4.5 and Windows XP ...."
This led me to believe tha you use QuickBasic 4.5, and not QBasic,
yet it seems from your examples that you are in fact using QBasic.

I use QuickBASIC 4.5 only, and compile my text programs.
Ted, is right, not using QBasic and the IDE, I am totally unfamiliar with
the "features" whereby the code is actually modified and the data type suffix can be added to a variable.
I wonder if the HELP for QB discusses these code changes made by the IDE.

Sorry if some of my QuickBASIC 4.5 examples and information didn't conform to the QBasic that you're using. Ted really identified my incompatibility problem.

I looked up LEN(variable), and found that I never used this form of LEN(),
only the form like LEN(A\$) for strings.  I never needed it, but it seems that you did.

Good luck with your future endeavors.

Regards..... Moneo
 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #11 on: October 15, 2008, 09:16:47 PM »

Moneo:

As to what I use, I do use QuickBASIC 4.5  (I have never used QBasic.), with Windos XP,  Home Edition, all as I state in my "signature.  I purposely state that information, so that I don't have to explain to anybody each time.  Oh, and I run the file, qb.exe, which, I assume, is the IDE that Ted has explained you don't use.  I never use a compiler that is not the one the IDE uses.  It is so easy!  Yes, I do all programming in the IDE, which let's me know if I have made a syntax error, among other helpful things.  And,  can run parts of the code as I write it, to check it out as I compose it.  For us amateurs, it's a real life saver.

As to the Help listing the changes as a number increases, I don't remember ever reading it, but that is not to say it doesn't contain that information.  Help is a little gem of information on qb.

Oops!  I thought LEN(number) would return the number of bytes it occupied.  So, I just ran a program to "prove" that.  Here it is:
Code:
CLS
a(1) = 11!
a(2) = 1.1
a(3) = 111111111
a(4) = 111111111111111#
FOR i = 1 TO 4
PRINT a(i);
PRINT LEN(a(i))
NEXT i
And, what were the results?  ALL FOUR NUMBERS GAVE A LEN(a(i)) OF 4!!!  Heh!

 « Last Edit: October 15, 2008, 09:32:56 PM by Ralph » Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Moneo
Na_th_an

Posts: 1971

 « Reply #12 on: October 15, 2008, 10:06:58 PM »

Moneo:

As to what I use, I do use QuickBASIC 4.5  (I have never used QBasic.), with Windos XP,  Home Edition, all as I state in my "signature.  I purposely state that information, so that I don't have to explain to anybody each time.  Oh, and I run the file, qb.exe, which, I assume, is the IDE that Ted has explained you don't use.  I never use a compiler that is not the one the IDE uses.  It is so easy!  Yes, I do all programming in the IDE, which let's me know if I have made a syntax error, among other helpful things.  And,  can run parts of the code as I write it, to check it out as I compose it.  For us amateurs, it's a real life saver.

As to the Help listing the changes as a number increases, I don't remember ever reading it, but that is not to say it doesn't contain that information.  Help is a little gem of information on qb.
.....
Ralph,
Based on what you say, you are in fact using QBasic and not QuickBASIC 4.5, because you run QB.EXE, which is QBasic, which has the IDE as default, and you compile from the IDE.

QuickBASIC 4.5 and QBasic are almost the same. QuickBASIC 4.5 is a bit older, and has no IDE. You need to invoke the compiler from the MSDOS commandline or a batch file.

When using QuicjBASIC  4.5, you must compile since there is no such thing as running it in interpretive mode. However, your QBasic program can be run in interpretive mode, and sometimes the same program will not compile under QuickBASIC 4.5 because of memory limitations. Strange!

Curiously enough, the HELP in QBasic is basically the same in content as the QuickBASIC 4.5 manual, and the headings in this QBasic HELP say QuickBASIC 4.5.

I have a copy of QBasic and the HELP, which I keep for information. I did a scan of this HELP and found no information about the changing of data variable suffixes, or other changes that the IDE makes. As a matter of fact, I could find no reference to the IDE.

I suggest that you change your signature to say QBasic and not QuickBASIC 4.5, because this is really what you are using.

Regards..... Moneo

 Logged
Ralph
Ancient Guru

Posts: 544

 « Reply #13 on: October 16, 2008, 12:54:20 AM »

Moneo:

I just took out my file, which I called QuickBASIC, from my cobwebby bookstand.  It contains five 5 1/4" floppes, each titled, Microsoft QuickBASIC", farther identified as "Version 4.5".  I bought it, bassed on a mailing, which was addressed to QuickBASIC Version 4.0 users, which started with:
"Dear Microsft QuickBASIC user:
We're proud to announce new Microsft QuickBASIC version 4.5 ..."

I sent the purchase price on 10-30-1988.  Most of my QuickBASIC 4.5 application files are dated 9/28/1998. The file, QB.EXE, is dated 9/28/1988 and is 278,804 bytes, shown as 273 KB by Windows XP.

Opening QB.EXE with Wordpad, and doing a Find "Version", I read, "QuickBASIC Version 4.5"

Based on the above, wouldn't you agree with me that I DO have QuickBASIC Version 4.5?

By the way, I did not receive a manual for QuickBASIC Version 4.5.  When was th manual produced?  And, why would an application called QBasic have "QuickBASIC version 4.5" in its Help file?  I don't think Microsoft would have been so careless!
===============================================================
Another thing, relating to my previous post, in which I got 4 for the length of all four values of a(1), a(2), a(3) and a(4).  After thinking about it some more, I decided to write a new program, as follows:

EDITED ON 16-08, 8:26 AM CT
I HAVE CORRECTED THE CODE, AS FOLLOWS:
Code:
'DIM a AS INTEGER
DIM b AS LONG
DIM c AS SINGLE
DIM d AS DOUBLE

PRINT N(a)
PRINT N(b)
PRINT N(c)
PRINT N(d)

The results I got were 2, 4, 4, and 8.  Now, that makes sense!

PLEASE NOTE THAT I HAVE EDITED MY CODE, WHICH WAS WRONG, AS I HAD ALL DIMs as INTEGERS!  SORRY.
 « Last Edit: October 16, 2008, 10:28:45 AM by Ralph » Logged

Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Clippy
Member

Posts: 84

 « Reply #14 on: October 16, 2008, 02:25:44 AM »

Moneo, I can understand using the command line to compile, but why do you have such an adversion to the IDE? Plus you gotta get off that tequila lol. QB.EXE is Quickbasic 4.5.

Also, if anybody wants to try to LEN a number, you have to LEN a variable value of the number for it to work correctly. If the variable is a specific type then the following bytes are determined:

\$        String     1 byte per character
%       Integer   2 bytes
!         Single    4 bytes
&        Long      4 bytes
#        Double   8 bytes

Ted
 « Last Edit: October 16, 2008, 02:43:52 AM by Clippy » Logged

 Pages: [1] 2