Microsoft KB Archive/39097

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

Article ID: 39097

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft QuickBasic Compiler for Macintosh 1.0

-



This article was previously published under Q39097



SYMPTOMS
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 &quot;Generate 68020 code&quot; 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 &quot;Generate 68020 code&quot; option.
 * 2) Use only dynamic arrays (avoid static arrays) in FOR...NEXT loops. (For example, don't compile with the &quot;Make All Arrays Static&quot; 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).



MORE INFORMATION
With the &quot;Generate 68020 code&quot; 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 &quot;Generate 68020 code&quot; 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 &quot;Generate 68020 Code&quot; compiler option must be selected.
 * 3) The &quot;Make All Arrays Static&quot; 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: DEFDBL A-H,K-Z : DEFINT I,J Im$ = &quot;###.########&quot; DIM ARRAY(4) 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)

NEXT 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

1.000000001396983  0   1.73205089569092 In the QuickBasic interpreter, the following CORRECT output displays:   1.00000000000  1.00000000000 2.00000000000 1.41421353816   3.00000000000  1.73205089569

1  1.414213538169861   1.732050776481628

Additional query words: MQuickB

Keywords: kbbug kbfix KB39097

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.