Qbasicnews.com

Full Version: oVERFLOW
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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.
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
[quote='Ralph link' dateline='1223418270']
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
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! 
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]
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


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.



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
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!
Pages: 1 2