Microsoft KB Archive/39097

From BetaArchive Wiki
Knowledge Base

FIX: Bad Compiled Floating Point w/ Static Arrays & 68020 Code

Article ID: 39097

Article Last Modified on 1/8/2003


  • Microsoft QuickBasic Compiler for Macintosh 1.0

This article was previously published under Q39097


The compiler in QuickBasic version 1.00 for Macintosh has a problem optimizing FOR...NEXT loops that contain single- or double-precision static arrays when you compile with the "Generate 68020 code" option. Incorrect floating point results can occur.

Microsoft has confirmed this to be a bug in version 1.00. This problem is corrected in QuickBasic version 1.00b for Macintosh .

You can avoid this problem by doing any of the following:

  1. Compile without the "Generate 68020 code" option.
  2. Use only dynamic arrays (avoid static arrays) in FOR...NEXT loops. (For example, don't compile with the "Make All Arrays Static" option if you have single- or double-precision arrays in FOR...NEXT loops.)

The problem does not occur in the QuickBasic interpreter environment (since it does not take advantage of the 68020 chip).


With the "Generate 68020 code" compiler option and an array defined as static, the compiled QuickBasic program below gives bad floating-point results at run time in assignments to the temporary variable and array elements in the FOR...NEXT loop.

The sample compiled program gives the CORRECT results if you compile WITHOUT the "Generate 68020 code" option, or if you make the array dynamic, or if you use Array(I)=SQR(A) instead of assigning the square root to the intermediate variable X.

This problem applies only to QuickBasic version 1.00, and does not apply to the older Microsoft Basic Compiler version 1.00 for Macintosh, which does not offer an option to take special advantage of the 68020 chip. (The 68020 chip is the processor in the Macintosh II computer.)

In the example below, when a temporary variable accepts a value returned by the SQR() function and is then assigned to a static array element in the FOR...NEXT loop, some array elements are incorrect; more elements are bad after exiting the loop. The following conditions must be present to produce this problem:

  1. The program must be compiled.
  2. The "Generate 68020 Code" compiler option must be selected.
  3. The "Make All Arrays Static" compiler option must be selected.
  4. A temporary variable must be used (as shown) for the return value of the SQR() function before assigning it to the array element.

The following code example demonstrates the problem:

Im$ = "###.########"
FOR I = 1 TO 3

    X = SQR(A)
    Array(I) = X
    ' Array(I)=SQR(A) ' Workaround: Add this & delete above 2 lines.
    PRINT USING Im$;  I, Array(I)

PRINT array(1)
PRINT array(2)
PRINT array(3)

The compiled program displays the following INCORRECT output:

   1.00000000000  1.00000000000
   2.00000000000  0.00000000000
   3.00000000000  1.73205089569


In the QuickBasic interpreter, the following CORRECT output displays:

   1.00000000000  1.00000000000
   2.00000000000  1.41421353816
   3.00000000000  1.73205089569


Additional query words: MQuickB

Keywords: kbbug kbfix KB39097