oVERFLOW Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 10-08-2008, 03:24 AM 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 Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer. Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 10-13-2008, 10:51 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. Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer. Clippy Member Posts: 82 Threads: 8 Joined: Jul 2008 10-14-2008, 03:22 AM 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 Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 10-14-2008, 05:15 AM [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 Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 10-14-2008, 10:13 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!Â  Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer. Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 10-14-2008, 11:17 PM (This post was last modified: 10-14-2008, 11:51 PM by Ralph.) 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, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer. Moneo Posting Freak Posts: 1,956 Threads: 65 Joined: Jun 2003 10-15-2008, 06:04 AM 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 Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 10-15-2008, 08:02 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. Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer. Clippy Member Posts: 82 Threads: 8 Joined: Jul 2008 10-15-2008, 10:15 PM (This post was last modified: 10-15-2008, 10:53 PM by Clippy.) 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 Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip Ralph Senior Member Posts: 544 Threads: 27 Joined: Jan 2005 10-16-2008, 02:30 AM Ted: You did it! I went to the Help, Contents, Data Types, and I found: Â  &Â  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! Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer. « Next Oldest | Next Newest »