Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/38274

From BetaArchive Wiki
Knowledge Base

Single Precision "Overflow" when Nearing Divide By Zero;10E-38

Article ID: 38274

Article Last Modified on 11/21/2006

This article was previously published under Q38274


The code example below produces an "OVERFLOW" error at n = 38 for QuickBasic Versions 3.00, 4.00, and 4.00b, and Basic Compiler Versions 6.00 and 6.00b for MS-DOS and OS/2. The overflow occurs as you divide by ever larger numbers and approach the limits of the negative exponent for single precision. The program may overflow at different places in other versions of Basic. To work around the "Overflow" error, use at least one double precision variable or constant in the expression before assigning to the variable.


The following code example gives an "Overflow" error at n = 38:

FOR n = 1 TO 100
 x# = 1 / 10 ^ n
 PRINT n, x#

In the above program, the expression 1/10^n is optimized to use single precision, since the most precise argument in the expression is n, which defaults to single precision.

To avoid the overflow of the negative single precision exponent, change n to double precision (n#). This forces the expression 1/10^n# to be stored in a double precision temporary storage area before being assigned to x#:

FOR n# = 1 TO 100  ' This program runs fine from n#=1 through 100.
 x# = 1 / 10 ^ n#
 PRINT n#, x#

Additional query words: QuickBas BasicCom

Keywords: KB38274